如何封送“Cstring”的类型在 .NET Compact Framework(C#) 中?

发布于 2024-09-01 02:34:05 字数 672 浏览 7 评论 0原文

如何在.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深府石板幽径 2024-09-08 02:34:05

您无法封送 CString,因为它不是本机类型 - 它是包装 char 数组的 C++ 类。

您可以将 string 编组为 char[],因为 char[] 是本机类型。您需要将要 P/Invoke 的函数的参数作为基本类型,例如 intboolcharstruct,但不是类。在此处阅读更多信息:

http://msdn.microsoft.com/en-us/ library/aa446536.aspx

为了调用以 CString 作为参数的函数,你可以这样做:

//Compile with /UNICODE
extern "C" MFCINTEROP_API int GetStringLen(const TCHAR* str) {
  CString s(str);
  return s.GetLength();
  //Or call some other function taking CString as an argument
  //return CallOtherFunction(s);
}

[DllImport("YourDLL.dll", CharSet=CharSet.Unicode)]
public extern static int GetStringLen(string param);        

在上面的 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 a char array.

You can marshal string to char[] as char[] is a native type. You need to have the parameters to the function you want to P/Invoke into as basic types like int, bool, char or struct, 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:

//Compile with /UNICODE
extern "C" MFCINTEROP_API int GetStringLen(const TCHAR* str) {
  CString s(str);
  return s.GetLength();
  //Or call some other function taking CString as an argument
  //return CallOtherFunction(s);
}

[DllImport("YourDLL.dll", CharSet=CharSet.Unicode)]
public extern static int GetStringLen(string param);        

In the above P/Invoke function we pass in a System.String which can marshal to char*/wchar_t*. The unmanaged function then creates a instance of CString and works with that.

By default System.String is marshalled to char*, so be careful with what kind of string the unmanaged version takes. This version uses TCHAR, which becomes wchar_t when compiled with /UNICODE. That's why you need to specify CharSet=CharSet.Unicode in the DllImport attribute.

我不会写诗 2024-09-08 02:34:05

您应该执行以下操作:

extern "C" __declspec(dllexport) int GetStringLen(LPCTSTR  str)
{ 
   CString s(str);
   return s.GetLength();
}

CString 实际上是 MFC 类型而不是本机类型。只需获取字符串并用本机方法将其转换为 CString 即可。

you should do the following:

extern "C" __declspec(dllexport) int GetStringLen(LPCTSTR  str)
{ 
   CString s(str);
   return s.GetLength();
}

The CString is actually an MFC type not a native type. Just grab the string and turn it into a CString in native method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文