使用回调从Inno Setup调用C#DLL

Bon*_*ngo 5 c# dll inno-setup callback dllexport

我有一个正在运行的Inno Setup脚本,其中我使用Sherlock Software的innocallback.dll.

这个DLL包装了我的一个过程,以便它可以传递给C#DLL.

我不想使用这个DLL,我想直接调用我导出的C#方法并将回调过程传递给它.

我的问题是:

如何将我的Inno安装程序(@mycallback)传递给我的C#DLL,以便我可以将它用作我的delegate/ UnmanagedFunctionPointer

正如我所说的这段代码有效,但我想尽量少用外部DLL.

这是我的代码:

Inno安装脚本

type
  TTimerProc=procedure();
  TProgressCallback=procedure(progress:Integer);
    
function WrapProgressProc(callback:TProgressCallback; paramcount:integer):longword;
  external 'wrapcallback@files:innocallback.dll stdcall';

function Test(callback:longword): String;
  external 'Test@files:ExposeTestLibrary.dll stdcall';

var
  endProgram : Boolean;

procedure mycallback(progress:Integer);
begin
  MsgBox(IntToStr(progress), mbInformation, MB_OK); 
  if progress > 15 then
  begin
    endProgram := True;
  end
end;
  
function InitializeSetup:boolean;
var
  progCallBack   : longword;
  callback       : longword;
  msg            : longword;
  msg2           : widestring;
begin
  endProgram := False;
  progCallBack:= WrapProgressProc(@mycallback,1); //Our proc has 1 arguments
  Test(progCallBack);
  result:=true;
end;
Run Code Online (Sandbox Code Playgroud)

这是我的C#代码

public class TestClass
{
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    public delegate void ReportProgress(uint progress);

    public static ReportProgress m_reportProgess;
    static uint m_iProgress;
    
    [DllExport("Test", CallingConvention = CallingConvention.StdCall)]
    static int Test(ReportProgress rProg)
    {
        m_iProgress = 0;
        m_reportProgess = rProg;
        System.Timers.Timer pTimer = new System.Timers.Timer();
        pTimer.Elapsed += aTimer_Elapsed;
        pTimer.Interval = 1000;
        pTimer.Enabled = true;
        GC.KeepAlive(pTimer);
        return 0;
    }

    static void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        m_iProgress++;
        m_reportProgess(m_iProgress);
    }
}
Run Code Online (Sandbox Code Playgroud)

TLa*_*ama 5

此答案不再适用于 Inno Setup 6。有关最新解决方案,请参阅我的 (@MartinPrikryl) 答案。

无法放弃包装 InnoCallback 库的使用,因为您根本无法在 Inno Setup 中使用您选择的调用约定定义回调过程,也无法使用register调用约定(特定于 Delphi 编译器的约定)定义回调函数你的 C# 库。

由于此限制,您必须使用外部库,它将 Inno Setup 中的回调方法包装到具有您的库可以使用的调用约定的函数中(InnoCallbackstdcall用于此目的)。

因此,如果您使用支持 Delphiregister调用约定的语言编写库,那么您所要求的将是可能的。出于好奇,在 Delphi 中你可以这样写:

library MyLib;

type
  TMyCallback = procedure(IntParam: Integer; StrParam: WideString) of object;

procedure CallMeBack(Callback: TMyCallback); stdcall;
begin
  Callback(123, 'Hello!');
end;

exports
  CallMeBack;

begin
end.
Run Code Online (Sandbox Code Playgroud)

然后在 Inno Setup 中(没有任何包装库):

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Files]
Source: "MyLib.dll"; Flags: dontcopy
Run Code Online (Sandbox Code Playgroud)
[Code]
type
  TMyCallback = procedure(IntParam: Integer; StrParam: WideString);

procedure CallMeBack(Callback: TMyCallback);
  external 'CallMeBack@files:mylib.dll stdcall';

procedure MyCallback(IntParam: Integer; StrParam: WideString);
begin
  MsgBox(Format('IntParam: %d; StrParam: %s', [IntParam, StrParam]),
    mbInformation, MB_OK);
end;

procedure InitializeWizard;
begin
  CallMeBack(@MyCallback);
end;
Run Code Online (Sandbox Code Playgroud)