如何用WCHAR*在消息框中显示c#中的C函数
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()
的第一个字符的地址。这里它显示了另一个问题,正如anderas所说,您正在返回一个无效的指针,因为HWStr.c_str()
返回指向std::wstring
对象的当前值的指针,因此只要wstring
有效,它就有效,因此当方法Test_EchoString
结束其执行时,它就不再有效(因为HWStr
被销毁)。
1)第一个是分配你想在堆中返回的内存,并在以后的另一个调用中释放它:
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' ';
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);
}
相关文章:
- 从数据库实时显示QT c++中的数据
- C 和 C++ 中开关语句的案例标签的常量值,但显示不同的行为
- 在模板中显示参数的类型
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- 链表中的显示功能无法正常工作
- 显示数组中的学生 ID 和最高分
- 如何在 MFC 中显示文件的所有行
- 访问和打印元组中的数据,并使用 C++14 使用模板函数显示数据
- 在链表中的第 n 位插入显示分割错误
- 将信息输入到下面显示的结构向量中的正确语法/格式是什么
- Windows 资源管理器中的图标在使用 resource.rc 时显示 2 个不同的图标
- 实例化模板时,我是否必须显式显示参数包中的类型?
- C++ 我的开关格式中的循环不允许我显示菜单选项或接受输入?
- SFML 中的字体.信息不显示在屏幕上.显示的不是信息,而是点
- 在C++中的SDL2窗口上显示.bmp时出现问题
- 为什么 Lisp 中 1000 阶乘的计算如此之快(并显示正确的结果)?
- 无法弄清楚如何使用 c++ 中的链表按从 a 到 z 的顺序显示字母
- 类中的一个变量显示,但另一个不显示
- 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++
- 控制 C++ 和显示中的输入