我在 C# 中有一个函数,我使用Unmanaged Exports从 C++ 调用它。传递的字符串在 C# 中被正确接收,但在 C++ 中返回的字符串显示为 5073480 之类的数字。这里可能有什么问题?我需要在 C++ 中取回字符串。以下是代码
C++ 代码:
using Testing = wchar_t*(__stdcall *) (wchar_t* name);
int _tmain(int argc, _TCHAR* argv[])
{
HMODULE mod = LoadLibraryA("CSharp.dll");
Testing performTest = reinterpret_cast<Testing>(GetProcAddress(mod, "testing"));
wchar_t* d_str = L"JS";
wchar_t* result = performTest(d_str);
std::printf("Result from c#: %d\n", result);
getchar();
return 0;
}
C#代码:
[DllExport(ExportName = "testing", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static string PassStringInOut([MarshalAs(UnmanagedType.LPWStr)]string name)
{
Console.WriteLine("Received string is: "+name);
return string.Format("Hello from .NET assembly, {0}!", name);
}