Convert system string to LPCWSTR

closed account (Gy7oizwU)
Hey guys

What is the best way to convert from a system string to LPCWSTR? I am currenty using the below code but when i create a wchar_t* it adds funny characters onto the end and then my shellexecute doesnt work.

1
2
3
4
5
6
7
8
9
10
char* logOpen = (char*)(void*)Marshal::StringToHGlobalAnsi(open);
int num = lstrlenA(logOpen);
int len = MultiByteToWideChar(CP_ACP, 0, logOpen, num, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, logOpen, num, buf, len);
std::wstring r(buf);
delete[] buf; 
LPCWSTR result = r.c_str();
			
ShellExecute(NULL,L"open",result, NULL, NULL, SW_SHOWNORMAL);


Thanks
closed account (Gy7oizwU)
I got it to work. i simply had to extend the size of the string by 1.

int num = lstrlenA(logOpen) + 1;
Topic archived. No new replies allowed.