'System::String ^' to 'LPCWSTR'

前端 未结 4 2093
囚心锁ツ
囚心锁ツ 2020-12-17 19:45

I want to convert System::String ^ to LPCWSTR.

for

FindFirstFile(LPCWSTR,WIN32_FIND_DATA); 

Please help.

4条回答
  •  眼角桃花
    2020-12-17 20:25

    To convert a System::String ot LPCWSTR in C++/CLI you can you use the Marshal::StringToHGlobalAnsi function to convert managed strings to unmanaged strings.

    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);
    

提交回复
热议问题