我有一个 C# DLL,它使用以下方式公开一个函数不受管理的出口 https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports它由 Inno Setup Pascal 脚本直接调用。该函数需要返回一个字符串给Inno Setup。我的问题是我怎样才能做到这一点?
我的首选方法是将一个缓冲区从 Inno Setup 传递到 C# 函数,该函数将返回该缓冲区内的字符串。我想出了这个代码:
C# 函数:
[DllExport("Test", CallingConvention = CallingConvention.StdCall)]
static int Test([Out, MarshalAs(UnmanagedType.LPWStr)] out string strout)
{
strout = "teststr";
return strout.Length;
}
Inno安装脚本:
function Test(var res: String):Integer; external 'Test@files:testdll.dll stdcall';
procedure test1;
var
Res: String;
l: Integer;
begin
SetLength(Res,256);
l := Test(Res);
{ Uncommenting the following line causes an exception }
{ SetLength(Res,l); }
Log('"Res"');
end;
当我运行这段代码时Res
变量为空(我在日志中看到“”)
如何从这个 DLL 返回一个字符串?
请注意,我使用的是 Inno Setup 的 Unicode 版本。我也不想使用 COM 来调用这个函数,也不想在 DLL 中分配缓冲区并将其返回给 Inno Setup。
我建议你使用BSTR http://msdn.microsoft.com/en-us/library/windows/desktop/ms221069%28v=vs.85%29.aspxtype,用于互操作函数调用的数据类型。在 C# 方面,您可以将字符串编组为UnmanagedType.BStr
键入并在 Inno Setup 端您可以使用WideString
,它兼容BSTR http://msdn.microsoft.com/en-us/library/windows/desktop/ms221069%28v=vs.85%29.aspx类型。所以你的代码将更改为这样(另请参阅Marshalling sample https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports#TOC-Marshalling-sample非托管导出文档的章节):
[DllExport("Test", CallingConvention = CallingConvention.StdCall)]
static int Test([MarshalAs(UnmanagedType.BStr)] out string strout)
{
strout = "teststr";
return 0; // indicates success
}
在 Inno Setup 方面使用WideString
对此:
[Code]
function Test(out strout: WideString): Integer;
external 'Test@files:testdll.dll stdcall';
procedure CallTest;
var
retval: Integer;
str: WideString;
begin
retval := Test(str);
{ test retval for success }
Log(str);
end;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)