我想要将System::String ^转换为LPCWSTR。
为
FindFirstFile(LPCWSTR,WIN32_FIND_DATA); 请帮帮忙。
发布于 2009-06-30 11:25:20
在C++/CLI中执行此操作的最简单方法是使用pin_ptr
#include <vcclr.h>
void CallFindFirstFile(System::String^ s)
{
WIN32_FIND_DATA data;
pin_ptr<const wchar_t> wname = PtrToStringChars(s);
FindFirstFile(wname, &data);
}发布于 2009-06-30 10:56:36
要在C++/CLI中转换System::String ot LPCWSTR,您可以使用Marshal::StringToHGlobalAnsi函数将托管字符串转换为非托管字符串。
System::String ^str = "Hello World";
IntPtr ptr = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str);
HANDLE hFind = FindFirstFile((LPCSTR)ptr.ToPointer(), data);
System::Runtime::InteropServices::Marshal::FreeHGlobal(ptr);发布于 2009-06-30 10:48:52
您需要使用P/Invoke。请查看此链接:http://www.pinvoke.net/default.aspx/kernel32/FindFirstFile.html
只需添加DllImport本机函数签名:
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
static extern IntPtr FindFirstFile
(string lpFileName, out WIN32_FIND_DATA lpFindFileData);CLR将自动执行托管到本机类型的封送处理。
编辑我刚知道你在用C++/CLI.在这种情况下,您还可以使用implicit P/Invoke,这是一个只有C++支持的特性(与C#和VB.NET相对)。本文展示了几个示例:
How to: Convert Between Various String Types in C++/CLI
https://stackoverflow.com/questions/1062962
复制相似问题