NSIS可以和C/C++混合编程,就是通过NSIS脚本加载C/C++的动态库。
1.首先创建动态库如下:
// Demo.h文件
#ifdef DEMO_EXPORTS
#define DEMO_API extern "C" __declspec(dllexport)
#else
#define DEMO_API extern "C" __declspec(dllimport)
#endif
DEMO_API int AddNumber(int a, int b);
// Demo.cpp源文件
#include "Demo.h"
DEMO_API int AddNumber(int a, int b)
{
return a + b;
}
编译之后,生成动态链接库Demo.dll
2.NSIS脚本调用
首先将生成的Demo.dll拷贝在目录NSIS的安装目录Plugins下,编写的NSIS脚本如下:
OutFile "UseDLL.exe"
ShowInstDetails show
Section ""
push "100"
push "200"
pop $0
pop $1
Demo::AddNumber $0 $1
pop $2
DetailPrint "$0+$1=$2"
SectionEnd
点击生成的安装包UseDLL.exe如下: