C#中哪种数据类型与C ++中的LPCWSTR等效? [英] Which data type in C# equivalent with LPCWSTR in C++?
本文介绍了C#中哪种数据类型与C ++中的LPCWSTR等效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个C ++的dll文件bilt,其中一个包含一个带参数的函数是LPCWSTR,我想在C#中调用这个函数,但是我不知道C#中哪个数据类型等同于那个数据类型,请帮帮我解决这个问题,谢谢。
I have a dll file bilt by C++, in that one contains a function with parameter is LPCWSTR, i want to call this function in C#, but I dont know which data type in C# equivalent with that data type, please help me solve this problem, thanks.
推荐答案
使用P / Invoke,您可以指定用于字符串类型的映射。这里解释了C ++代码,但同样适用于C#。你只需在声明上放置属性就可以告诉编译器如何转换类型。
你可以将它添加到字符串参数:[MarshalAs(导入函数时,UnmanagedType :: LPArray)]
。
http://msdn.microsoft.com/en-us/library/s97shtze(v = vs.110).aspx [ ^ ]
您应该能够在文档或网络上找到更多信息。
With P/Invoke, you can specify the mapping to use for string type. This is explained here for C++ code but the same apply to C#. You simply put attribute on the declaration to tell the compiler how to convert types.
You would add that to the string parameter :[MarshalAs(UnmanagedType::LPArray)]
when importing functions.
http://msdn.microsoft.com/en-us/library/s97shtze(v=vs.110).aspx[^]
You should be able to find more information in documentation or on the web.
尝试使用Marshal.StringToHGlobalAnsi [ ^ ]
将托管字符串转换为非托管字符串string(返回IntPtr
指向已转换的字符串)。
然后更改导入声明以使用IntPtr:
Try using Marshal.StringToHGlobalAnsi[^]
to convert managed string to unmanaged string (returnsIntPtr
pointing to converted string).
Then change import declaration to use IntPtr:
[DllImport("abc.dll")] private static extern long CreateLink(IntPtr lpszPathObj, string lpszPathLink, IntPtr lpszDesc);
我不确定第二个参数是否也应该转换。
这里有一篇文章可能有所帮助:
创建和解析shell链接 [ ^ ]
这里还有一个iterop,不需要C ++ dll:
http://www.msjogren.net/dotnet/eng/samples/dotnet_shelllink.asp [ ^ ]
Try this:
void CManagedClass::SetStr( String ^ theString ) {
CUnmanagedClass m_Impl;
pin_ptr<const WCHAR> str = PtrToStringChars(theString);
m_Impl.SetString( str );
}
这篇关于C#中哪种数据类型与C ++中的LPCWSTR等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文