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++的特性了。

相关推荐
灰子学技术6 小时前
go response.Body.close()导致连接异常处理
开发语言·后端·golang
二十雨辰7 小时前
[python]-AI大模型
开发语言·人工智能·python
Yvonne爱编码7 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚7 小时前
JAVA进阶之路——无奖问答挑战1
java·开发语言
你这个代码我看不懂7 小时前
@ConditionalOnProperty不直接使用松绑定规则
java·开发语言
pas1367 小时前
41-parse的实现原理&有限状态机
开发语言·前端·javascript
琹箐7 小时前
最大堆和最小堆 实现思路
java·开发语言·算法
Monly218 小时前
Java:修改打包配置文件
java·开发语言
我命由我123458 小时前
Android 广播 - 静态注册与动态注册对广播接收器实例创建的影响
android·java·开发语言·java-ee·android studio·android-studio·android runtime
island13148 小时前
CANN ops-nn 算子库深度解析:核心算子(如激活函数、归一化)的数值精度控制与内存高效实现
开发语言·人工智能·神经网络