从Inno Setup调用C#DLL回调 [英] Call C# DLL from Inno Setup with callback
问题描述
我有一个正在运行的Inno安装脚本,其中由Sherlock Software使用innocallback.dll。
I have a running Inno Setup script, wherein I use innocallback.dll by Sherlock Software.
这个DLL包含了我的一个程序,以便可以传递给一个C#DLL。
This DLL wraps a procedure of mine so that it can be passed to a C# DLL.
我不想使用这个DLL,我想直接调用我导出的C#方法,并传递给它回调过程。
I don't want to use this DLL, I want to call my exported C# method directly and pass to it the callback procedure.
我的问题是:
如何通过我的Inno安装程序( @mycallback
)到我的C#DLL,以便我可以使用它作为我的委托
/ UnmanagedFunctionPointer
?
How can I pass my Inno Setup procedure (@mycallback
) to my C# DLL so that I can use it as my delegate
/UnmanagedFunctionPointer
?
正如我所说的这个代码可行,但我想尽可能少的使用外部DLL。
As I said this code works, but I want to use as little external DLL's as possible.
这是我的代码:
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;
这是我的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);
}
}
推荐答案
没有办法放弃包装InnoCallback库的使用,因为您根本无法使用您在Inno Setup中选择的调用约定定义回调过程,也不能使用注册
调用约定(Delphi编译器特定的)在C#库中。
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.
由于此限制,您必须使用一个外部库,它将Inno的回调方法使用您的库可以使用的调用约定来设置函数(InnoCallback使用 stdcall
)。
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).
所以如果您以支持Delphi的注册
调用约定的语言编写您的库,那么您将要求的是可能的。出于好奇,在Delphi中,你可以写出例如:
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.
然后在Inno Setup中(没有任何包装库):
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;
这篇关于从Inno Setup调用C#DLL回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!