我有一个原生的C++(我认为)应用程序,可以配置为加载某个DLL并调用一个函数.此函数返回int,接收四个参数,应至少更改其中一个并返回一个值.根据手册,这个函数应该在C++中定义为:
__declspec( dllexport ) int funcName(const char* par1, const char* par2, char* par3, char* par4);
Run Code Online (Sandbox Code Playgroud)
但是,根据需求,此功能应在C#中实现.我使用Unmanaged Exports允许我使用:
[DllExport("funcName", CallingConvention.Cdecl)]
public static unsafe int funcName(string par1, string par2, string par3, string par4) {
// sample for par3 only when using StringBuilder instead of string
// but is similar with other types
par3 = new StringBuilder(256);
par3.Append("12345");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
参数1和2工作正常(我可以使用消息框收到的值),但是我已尝试(至少)以下作为第3和第4个参数:
char* =>
=> string
=> StringBuilder
=> char[]
Run Code Online (Sandbox Code Playgroud)
所有这些都与[In], [Out], [In,Out], out, ref.
函数应更改par3和par4的值,并将整数返回给调用应用程序.应用程序读取par3的值(它实际上是一个表示为字符串的整数)并将其写入日志文件.检查日志文件后,该值不是funcName(...)中设置的值.
最常见的值是""(空),然而使用时只char[]用out或 …