[C++][Windows] char* to LPCWSTR?

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

[C++][Windows] char* to LPCWSTR?

Post by ayu »

This has been giving me a headache all night ...

Now, this functions wants a LPCWSTR in unicode (the magic L)

Code: Select all

//Open registry key
retVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCWSTR)L"SYSTEM\\some\\path", 0, KEY_READ, &phkResult);
And that works fine, but in my case I have created a function that returns a LPCWSTR (alternatively a wchar_t/wchar).This is because I need to grab some string from the system, create the key and return it. This is where my issue is ... this doesn't work (assuming that regKey got the returned LPCWSTR value)

Code: Select all

//Open registry key
retVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, regKey, 0, KEY_READ, &phkResult);
I think the problem lies in the conversion between the data types.

I have one variable

Code: Select all

LPCWSTR test = L"SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces\\";
And then I have the structure that has the string data that I need, of the type char*

Code: Select all

char *test2= pAddresses->AdapterName;
I need to combine these two and put test2 at the end of test, then return a LPCWSTR.

But for some reason I simply can't get this to work. It works if I use wstring and wsstream to stream them back to a wstring, then assign it with to a LPCWSTR and use it, but if I put that same code in a function and return it, it doesn't work anymore.

Does anyone have an idea? :?
"The best place to hide a tree, is in a forest"

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Re: [C++][Windows] char* to LPCWSTR?

Post by leetnigga »

cats wrote:It works if I use wstring and wsstream to stream them back to a wstring, then assign it with to a LPCWSTR and use it, but if I put that same code in a function and return it, it doesn't work anymore.
Putting them both in a wstring like that and converting to a LPCWSTR using .c_str() seems like the best idea to me.

What doesn't work when you use code like that in a function?


Post Reply