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

相关推荐
Azxcc02 小时前
c++ core guidelines解析--让接口易于使用
开发语言·c++
亭上秋和景清2 小时前
指针进阶: 回调函数
开发语言·前端·javascript
Vanranrr2 小时前
一个由非虚函数导致的隐藏Bug:窗口显示异常问题排查与解决
开发语言·bug
ULTRA??2 小时前
QT向量类实现GJK碰撞检测算法3d版本
c++·qt·算法
煤球王子2 小时前
学而时习之:C++ 中的文件处理
c++
天赐学c语言2 小时前
12.10 - 合并两个有序链表 && 对字节对齐的理解
数据结构·c++·leetcode·链表
曹牧2 小时前
Java:Jackson库序列化对象
java·开发语言·python
仰泳的熊猫2 小时前
1092 To Buy or Not to Buy
数据结构·c++·算法·pat考试
CSDN_RTKLIB2 小时前
解除vcpkg对VS的全局配置注入
c++