I want to convert System::String ^
to LPCWSTR
.
for
FindFirstFile(LPCWSTR,WIN32_FIND_DATA);
Please help.
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);
The easiest way to do this in C++/CLI is to use 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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With