Call C# DLL from Inno Setup with callback

前端 未结 2 1814
长发绾君心
长发绾君心 2020-12-18 04:39

I have a running Inno Setup script, wherein I use innocallback.dll by Sherlock Software.

This DLL wraps a procedure of mine so that it can be passed to a C# DLL.

相关标签:
2条回答
  • 2020-12-18 04:54

    This answer is no longer valid with Inno Setup 6. See my (@MartinPrikryl) answer for up to date solution.

    There's no way to drop the usage of the wrapping InnoCallback library since you simply cannot define a callback procedure with a calling convention of your choice in Inno Setup, nor you can define a callback with the register calling convention (the one specific to Delphi compiler) in your C# library.

    Due to this limit you must use an external library, which wraps a callback method from Inno Setup into a function with a calling convention that your library can consume (InnoCallback uses stdcall for that).

    So, what you're asking for would be possible if you were writing your library in a language that supports Delphi's register calling convention. Out of curiosity, in Delphi you could write e.g.:

    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.
    

    And in Inno Setup then (without any wrapping library):

    [Setup]
    AppName=My Program
    AppVersion=1.5
    DefaultDirName={pf}\My Program
    
    [Files]
    Source: "MyLib.dll"; Flags: dontcopy
    
    [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;
    
    0 讨论(0)
  • 2020-12-18 05:04

    With Inno Setup 6, there's built-in CreateCallback function that serves the same purpose as WrapCallback function from InnoTools InnoCallback library.

    So you can now do:

    Test(CreateCallback(@mycallback));
    
    0 讨论(0)
提交回复
热议问题