[英]how to display in messagebox a C function from C# with a WCHAR*
我正在构建一个加载C ++库的C#应用程序。 我从该C ++ DLL调用函数。 我使用下面的功能来显示输入字符串。
C ++ DLL:
wchar_t* Test_EchoString( wchar_t *InputStr )
{
String HWStr = String( InputStr );
return HWStr.c_str();
}
C#代码:
[DllImport("testDll.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static extern int _Test_EchoString([MarshalAs(UnmanagedType.LPWStr)] string s);
private void echo_string_Click(object sender, RoutedEventArgs e)
{
string upn = "aaaaa";
_Test_EchoString(upn);
MessageBox.Show(_Test_EchoString(upn).ToString());
}
我进入消息框编号18666252,但我想从_Test_EchoString()
获得一个字符串。
您的代码中有两个问题:
在您的C#中,您将_Test_EchoString
定义为public static extern int _Test_EchoString
,因此在执行它时,返回值将是字符串HWStr.c_str()
的第一个字符的地址。 正如安德拉斯所说,这里显示了另一个问题,您正在返回一个无效的指针,因为HWStr.c_str()
将指针返回到std::wstring
对象的当前值,因此只要wstring
有效,它就有效,因此当方法Test_EchoString
结束执行时,它不再有效(因为HWStr
被销毁)。
有多种方法可以解决此问题,我将向您介绍以下两种方法:
1)第一种是在Heap中分配要返回的内存,并在以后通过另一个调用将其释放:
static wchar_t *Test_EchoStringResult;
extern "C" __declspec(dllexport) const wchar_t * Test_EchoStringNew(const wchar_t *InputStr)
{
std::wstring HWStr(InputStr);
HWStr += L" something";
Test_EchoStringResult = new wchar_t[HWStr.length() + sizeof(wchar_t)];
HWStr.copy(Test_EchoStringResult, HWStr.length());
Test_EchoStringResult[HWStr.length()] = L'\0';
return Test_EchoStringResult;
}
extern "C" __declspec(dllexport) void Test_EchoStringDelete()
{
delete[] Test_EchoStringResult;
}
这是C#中的用法:
[DllImport("testDll.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static extern IntPtr Test_EchoStringNew(string foo);
[DllImport("testDll.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static extern void Test_EchoStringDelete();
public void foo()
{
string result = Marshal.PtrToStringAuto(Test_EchoStringNew("test"));
MessageBox.Show(result.ToString());
Test_EchoStringDelete();
}
对我来说,这看起来很丑陋,所以我宁愿使用其他模式
2)将回调传递给C方法,并在HWStr
仍然有效时传递给该方法HWStr.c_str()
:
extern "C" __declspec(dllexport) void Test_EchoString(const wchar_t *InputStr, void (*callback)(const wchar_t*))
{
std::wstring HWStr(InputStr);
HWStr += L" something";
callback(HWStr.c_str());
}
这是C#的用法:
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public delegate void myCallback(string toShow);
[DllImport("testDll.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static extern void Test_EchoString(string foo, myCallback callback);
public void foo()
{
Test_EchoString("test", callback);
}
void callback(string toShow)
{
MessageBox.Show(toShow);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.