Csharp/C#教程:从具有非托管导出的C#DLL返回字符串到Inno安装传奇脚本共享


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

我有一个C#DLL,它使用Unmanaged Exports公开一个函数,它由Inno Setup Pascal脚本直接调用。 此函数需要将字符串返回给Inno Setup。 我的问题是如何实现这一目标?
我首选的方法是将Inno Setup中的缓冲区传递给C#函数,该函数将返回此缓冲区内的字符串。 我想出了这段代码:

C#function:

[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类型,它曾经是用于互操作函数调用的数据类型。 在C#端,您将字符串编组为UnmanagedType.BStr类型,在Inno Setup端,您将使用与BSTR类型兼容的WideString 。 因此,您的代码将更改为此(另请参阅Unmanaged Exports文档的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

上述就是C#学习教程:从具有非托管导出的C#DLL返回字符串到Inno安装脚本分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 [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; 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1020659.html

(0)
上一篇 2022年1月4日
下一篇 2022年1月4日

精彩推荐