NSIS编写C/C++扩展

编写C扩展

exdll.c

c 复制代码
#include <windows.h>
#include <nsis/pluginapi.h> // nsis plugin

#pragma 

HINSTANCE g_hInstance;
HWND g_hwndParent;

typedef void (*HELLO_FUNC)();


// To work with Unicode version of NSIS, please use TCHAR-type
// functions for accessing the variables and the stack.

void __declspec(dllexport) myFunction(HWND hwndParent, int string_size, 
                                      LPTSTR variables, stack_t **stacktop,
                                      extra_parameters *extra, ...)
{
	//初始化很重要
  EXDLL_INIT();
  g_hwndParent = hwndParent;


  // do your stuff here
  {
    LPTSTR msgbuf = (LPTSTR) GlobalAlloc(GPTR, (3 + string_size + 1) * sizeof(*msgbuf));
    if (msgbuf)
    {
      wsprintf(msgbuf, TEXT("$0=%s"), getuservariable(INST_0));
      MessageBox(g_hwndParent, msgbuf, TEXT("Message from example plugin"), MB_OK);
      GlobalFree(msgbuf);
    }
  }
}


BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
{
  g_hInstance = hInst;
  return TRUE;
}

调用侧:

cpp 复制代码
; rtest.nsi
;
; This script tests some advanced NSIS functions.

;--------------------------------

Name "rtest"
OutFile "rtest.exe"

ComponentText "Select tests!"
ShowInstDetails show

RequestExecutionLevel user

;--------------------------------
Section "Test MyPlugin"
  
  StrCpy $0 "file.dat"
  StrCpy $1 "read.txt"
  exdll::myFunction
SectionEnd

由于NSIS默认是不支持C++扩展的,因此可以采用动态加载dll的方式;

编写C++功能的dll

注意这里的extern C

cpp 复制代码
// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
#include <iostream>
#include <fstream>


extern "C"  __declspec(dllexport) void hello_cpp() {
    std::ofstream outFile("output.txt", std::ios::out);
    if (!outFile) {
        std::cerr << "Unable open file!" << std::endl;
        return;
    }

    outFile << "Hello from C++!" << std::endl;

    outFile.close();
}

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

编译为dll后在C侧动态加载dll

c 复制代码
#include <windows.h>
#include <nsis/pluginapi.h> // nsis plugin

#pragma 

HINSTANCE g_hInstance;
HWND g_hwndParent;

typedef void (*HELLO_FUNC)();


// To work with Unicode version of NSIS, please use TCHAR-type
// functions for accessing the variables and the stack.

void __declspec(dllexport) myFunction(HWND hwndParent, int string_size, 
                                      LPTSTR variables, stack_t **stacktop,
                                      extra_parameters *extra, ...)
{
  EXDLL_INIT();
  g_hwndParent = hwndParent;

  HMODULE hDll = LoadLibraryA("C:/Users/tians/Source/Repos/Dll1/Debug/Dll1.dll");
  if (!hDll) {
      return;
  }

  HELLO_FUNC hello = (HELLO_FUNC)GetProcAddress(hDll, "hello_cpp");
  if (!hello) {
      FreeLibrary(hDll);
      return;
  }

  hello();

  FreeLibrary(hDll);


  // note if you want parameters from the stack, pop them off in order.
  // i.e. if you are called via exdll::myFunction file.dat read.txt
  // calling popstring() the first time would give you file.dat,
  // and the second time would give you read.txt. 
  // you should empty the stack of your parameters, and ONLY your
  // parameters.

  // do your stuff here
  {
    LPTSTR msgbuf = (LPTSTR) GlobalAlloc(GPTR, (3 + string_size + 1) * sizeof(*msgbuf));
    if (msgbuf)
    {
      wsprintf(msgbuf, TEXT("$0=%s"), getuservariable(INST_0));
      MessageBox(g_hwndParent, msgbuf, TEXT("Message from example plugin"), MB_OK);
      GlobalFree(msgbuf);
    }
  }
}


BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
{
  g_hInstance = hInst;
  return TRUE;
}

这样就可以充分利用C++的特性了。

相关推荐
OliverH-yishuihan4 分钟前
在win10上借助WSL用VS2019开发跨平台项目实例
linux·c++·windows
yue0089 分钟前
C# 字符串倒序
开发语言·c#
强子感冒了17 分钟前
Java学习笔记:String、StringBuilder与StringBuffer
java·开发语言·笔记·学习
低保和光头哪个先来23 分钟前
场景6:对浏览器内核的理解
开发语言·前端·javascript·vue.js·前端框架
小北方城市网27 分钟前
Python + 前后端全栈进阶课程(共 10 节|完整版递进式|从技术深化→项目落地→就业进阶,无缝衔接基础课)
大数据·开发语言·网络·python·数据库架构
程序员JerrySUN32 分钟前
OP-TEE + YOLOv8:从“加密权重”到“内存中解密并推理”的完整实战记录
android·java·开发语言·redis·yolo·架构
阿里嘎多学长43 分钟前
2025-12-30 GitHub 热点项目精选
开发语言·程序员·github·代码托管
汉克老师1 小时前
GESP2025年12月认证C++二级真题与解析(编程题1 (环保能量球))
c++·gesp二级·gesp2级
郝学胜-神的一滴1 小时前
Linux进程与线程控制原语对比:双刃出鞘,各显锋芒
linux·服务器·开发语言·数据结构·c++·程序人生
小钟不想敲代码1 小时前
Python(一)
开发语言·python