NSIS基础教程衿华版第二卷

在nsis中,是支持栈操作的.栈是nsis提供的一个用于变量管道操作方式.栈有很多功能与优点, 比如你可以将一个变量值存放到栈中,或者从栈中取出一个变量值.栈具有先进后出的特性(LIFO). 你可以将栈运用在函数或动态库之间的参数传递.你也可以用nsis已经默认的$0-$9和$R0-$R9这几个 栈存储变量.在nsis的栈操作有以下几个关键字:Pop(出栈),Push(进栈),Exch(交换栈顶元素).

Push "Value 1"    #Stack:"Value 1"
Push "Value 2"    #Stack:"Value 2"->"Value 1"
Pop $0            #Stack:"Value 1" #$0  ontains:"Value 2"
Push $0           #Stack:"Value 2"->"Value 1"
Push "Value 3"    #Stack:"Value 3"->"Value 2"->"Value 1"
Push "Value 4"    #Stack:"Value 4"->"Value 3"->"Value 2"->"Value 1"
Exch              #Stack:"Value 3"->"Value 4"->"Value 2"->"Value 1"
StrCpy $0 "Value X" 
Exch $0           #Stack:"Value X"->"Value 4"->"Value 2"->"Value 1"
#Exchange the top value with the variable("Value X").
#Now $0 contains "Value 3" 
Exch 3            #Stack:"Value 1"->Value 4"->"Value 2"->"Value X"
#Exchanges the top value with the fourth value(thus index 3).

NSIS栈操作简单实例

OutFile "stack.exe"
ShowInstDetails show
Section ""
    Push "Value 1"
    Push "Value 2"
    Pop $0
    Push $0
    Push "Value 3"
    Push "Value 4"
    Exch
    StrCpy $0 "Value X"
    Exch $0
    Exch 3

    Call Func1
SectionEnd

Function Func1
    Pop $0
    Pop $1
    Pop $2
    Pop $3
    DetailPrint "$0"
    DetailPrint "$1"
    DetailPrint "$2"
    DetailPrint "$3"
FunctionEnd

NSIS支持多国语言

语言文件在nsis安装目录下:ContribLanguage files. 并且你可以参考Exampleslanguages.nsi来完成多国语言的安装程序

OutFile "Language.exe"

LoadLanguageFile "${NSISDIR}ContribLanguage filesEnglish.nlf"
LoadLanguageFile "${NSISDIR}ContribLanguage filesSimpChinese.nlf"

Section ""

SectionEnd

Function .onInit
	;Language selection dialog
	Push ""
	Push ${LANG_ENGLISH}
	Push English
	Push ${LANG_SIMPCHINESE}
	Push "Simplified Chinese"

	Push A ; A means auto count languages
	       ; for the auto count to work the first empty push (Push "") must remain
	LangDLL::LangDialog "Installer Language" "Please select the language of the installer"

	Pop $LANGUAGE
	StrCmp $LANGUAGE "cancel" 0 +2
		Abort
FunctionEnd

NSIS如何使用DLL插件

插件DLL是一种可以为应用程序提供功能扩展的函数集.在nsis中扩展强制以.dll或.nsh来命名标记.动态库 支持C/C++/Delphi之类的工具开发插件.你可以使用标准的动态库,也可以使用基于nsis开发的动态库.基于 nsis的动态库可以方便的进行数据交换,但增加了程序的耦合度. 动态库.dll文件需要放到nsis安装目录下的Plugins目录,而.nsh文件要放在Include目录下,但有时候这样操 作并不是很方便,nsis为我们提供了一个更加便捷的方式:!AddPluginDir .(即增加当前目录为.dll文件的存放目录) 增加.nsh文件存放目录要使用!AddIncludeDir .即可,"."代表当前目录,这样nsis在编译的时候会首先查找当前目录 是否存在所需扩展文件,如果有会遍历它所有的函数找到所需函数段.

//直接用常规的做法扩展一个加法插件
extern "C"
_declspec(dllexport) int add(int a,int b)
{
	return a+b;
}

OutFile "UseDll.exe"
ShowInstDetails show
Section ""
    Push "200"
    Push "400"
    Pop $0
    Pop $1
System::Call "UseDll::add(i$0,i$1)i.r2"
DetailPrint "$0+$1=$2"
SectionEnd

NSIS实例创建一个动态库插件与NSIS脚本混合编程

#include "windows.h"
#include "../ExDLL/exdll.h"

int myatoi(char *s);

extern "C"
_declspec(dllexport) int mul(int a,int b)
{
	return a*b;
}

extern "C"
_declspec(dllexport) add(HWND hwndParent, int string_size,
                                char *variables, stack_t ** stacktop)
{
	int a,b;
	char temp[64];
	EXDLL_INIT();
	popstring(temp);
	a = myatoi(temp);
	popstring(temp);
	b = myatoi(temp);
	
	wsprintf(temp, "%d", a+b);
	pushstring(temp);

}

int myatoi(char *s)
{
	unsigned int v = 0;
	if (*s == '0' && (s[1] == 'x' || s[1] == 'X')) {
		s += 2;
		for (;;) {
			int c = *s++;
			if (c >= '0' && c <= '9')
				c -= '0';
			else if (c >= 'a' && c <= 'f')
				c -= 'a' - 10;
			else if (c >= 'A' && c <= 'F')
				c -= 'A' - 10;
			else
				break;
			v <<= 4;
			v += c;
		}
	} else if (*s == '0' && s[1] <= '7' && s[1] >= '0') {
		s++;
		for (;;) {
			int c = *s++;
			if (c >= '0' && c <= '7')
				c -= '0';
			else
				break;
			v <<= 3;
			v += c;
		}
	} else {
		int sign = 0;
		if (*s == '-') {
			s++;
			sign++;
		}
		for (;;) {
			int c = *s++ - '0';
			if (c < 0 || c > 9)
				break;
			v *= 10;
			v += c;
		}
		if (sign)
			return -(int) v;
	}
	return (int) v;
}
#NSIS测试代码
!AddPluginDir .
OutFile "UseDll.exe"
ShowInstDetails show
Section ""
    Push "200"
    Push "400"
    Pop $0
    Pop $1
System::Call "UseDll::mul(i$0,i$1)i.r2"
DetailPrint "$0*$1=$2"

UseDll::add $0 $1
Pop $2
DetailPrint "$0+$1=$2"
SectionEnd

展开阅读全文

页面更新:2024-03-08

标签:耦合度   变量   基础教程   函数   插件   实例   操作   功能   文件   目录   动态

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top