NSIS编写C/C++扩展

「Kurator·云原生实战派」主题征文,赢华为FreeBuds等好礼 10w+人浏览 256人参与

编写C扩展

exdll.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;
}

调用侧:

; 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

// 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

#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.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

helloworddm

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值