convert wchar_t* to LPSTR {aka char*}

Aug 18, 2016 at 4:33pm
Hello,


wchar_t buffer[50];
//wchar_t buffer[50] = {0};

GetModuleBaseName(hProcess, 0, buffer, 50);

The compile error I get is :-
cannot convert 'wchar_t*' to 'LPSTR {aka char*}' for argument '3' to 'DWORD GetModuleBaseNameA(HANDLE, HMODULE, LPSTR, DWORD)' GetModuleBaseName(hProcess, 0, buffer, 50);

Can someone suggest a solution?

Regards
Aug 18, 2016 at 4:49pm
1
2
3
char buffer[50];

GetModuleBaseName(hProcess, 0, buffer, 50);

1
2
3
wchar_t buffer[50];

GetModuleBaseNameW(hProcess, 0, buffer, 50);
Aug 18, 2016 at 6:19pm
1
2
  TCHAR buffer[50] = {0};
  GetModuleBaseName(hProcess, 0, buffer, 50);
Aug 19, 2016 at 8:40am
Hello,

Thanks for your replies, gives me something to play with.

Regards
Aug 19, 2016 at 1:34pm
if your prefer to write your own conversions rather than using libraries, heres something that will work on any platform.

//pass in wchar pointer and wchar length

1
2
3
4
5
6
7
char * buff = new char[wcharLength+1];//alloc space for conversion plus 1, null terminate
buff[wcharLength] = 0;
for ( ;; )
{
	*buff++ = (wCharBuff) *wCharBuff;
	if(!*wCharBuff++)break;
}


forgot to mention this will only display properly if your using non unicode, if you need that and your using windows you could use WideCharToMultiByte
Last edited on Aug 19, 2016 at 1:39pm
Topic archived. No new replies allowed.