小编Oha*_*had的帖子

从具有非托管导出的C#DLL返回字符串到Inno安装脚本

我有一个C#DLL,它使用Unmanaged Exports公开一个函数,它由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;
}
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,Res变量为空(我在日志中看到"")

如何从此DLL返回一个字符串?

请注意,我使用的是Inno Setup的Unicode版本.我也不想使用COM调用此函数,也不想在DLL中分配缓冲区并将其返回给Inno Setup.

c# dll inno-setup unmanagedexports

4
推荐指数
1
解决办法
2589
查看次数

标签 统计

c# ×1

dll ×1

inno-setup ×1

unmanagedexports ×1