如何封送“Cstring”的类型在 .NET Compact Framework(C#) 中?
如何在.NET Compact Framework(C#) 中封送“Cstring”类型?
DLL名称:Test_Cstring.dll(操作系统为WinCE 5.0),源代码:
extern "C" __declspec(dllexport) int GetStringLen(CString str)
{
return str.GetLength();
}
我在.NET Compact Framework(C#)中编组的,例如:
[DllImport("Test_Cstring.dll", EntryPoint = "GetStringLen", SetLastError = true)]
public extern static int GetStringLen(string s);
private void Test_Cstring()
{
int len=-1;
len=GetStringLen("abcd");
MessageBox.Show("Length:"+len.ToString()); //result is -1,so PInvoke is unsuccessful!
}
.NET CF中的“GetStringLen”方法不成功! 如何编组这种类型的“Cstring”? 任何有关它的信息将不胜感激!
How to marshal the type of "Cstring" in .NET Compact Framework(C#)?
DLLname:Test_Cstring.dll(OS is WinCE 5.0),source code:
extern "C" __declspec(dllexport) int GetStringLen(CString str)
{
return str.GetLength();
}
I marshal that in .NET Compact Framework(C#),for example:
[DllImport("Test_Cstring.dll", EntryPoint = "GetStringLen", SetLastError = true)]
public extern static int GetStringLen(string s);
private void Test_Cstring()
{
int len=-1;
len=GetStringLen("abcd");
MessageBox.Show("Length:"+len.ToString()); //result is -1,so PInvoke is unsuccessful!
}
The Method of "GetStringLen" in .NET CF is unsuccessful!
How to marshal this type of "Cstring"?
Any information about it would be very appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法封送
CString
,因为它不是本机类型 - 它是包装char
数组的 C++ 类。您可以将
string
编组为char[]
,因为char[]
是本机类型。您需要将要 P/Invoke 的函数的参数作为基本类型,例如int
、bool
、char
或struct,但不是类。在此处阅读更多信息:
http://msdn.microsoft.com/en-us/ library/aa446536.aspx
为了调用以 CString 作为参数的函数,你可以这样做:
在上面的 P/Invoke 函数中,我们传入一个 System.String ,它可以封送至
char*/wchar_t*
。然后,非托管函数创建一个 CString 实例并使用它。默认情况下,
System.String
编组为char*
,因此请注意非托管版本采用的字符串类型。该版本使用TCHAR
,当用/UNICODE
编译时,它变成wchar_t
。这就是为什么您需要在DllImport
属性中指定CharSet=CharSet.Unicode
。You can't marshal
CString
as it's not a native type - it's a C++ class that wraps up achar
array.You can marshal
string
tochar[]
aschar[]
is a native type. You need to have the parameters to the function you want to P/Invoke into as basic types likeint
,bool
,char
orstruct
, but not classes. Read more here:http://msdn.microsoft.com/en-us/library/aa446536.aspx
In order to call functions that take CString as an argument you can do something like this:
In the above P/Invoke function we pass in a
System.String
which can marshal tochar*/wchar_t*
. The unmanaged function then creates a instance ofCString
and works with that.By default
System.String
is marshalled tochar*
, so be careful with what kind of string the unmanaged version takes. This version usesTCHAR
, which becomeswchar_t
when compiled with/UNICODE
. That's why you need to specifyCharSet=CharSet.Unicode
in theDllImport
attribute.您应该执行以下操作:
CString 实际上是 MFC 类型而不是本机类型。只需获取字符串并用本机方法将其转换为 CString 即可。
you should do the following:
The CString is actually an MFC type not a native type. Just grab the string and turn it into a CString in native method.