简体   繁体   中英

How to convert LPWSTR* (or WCHAR*) to LPWSTR

I'm having a difficulty in converting a value to LPWSTR. I'm getting a registry value, and trying to return the result as LPWSTR. It appears the registry call using RegQueryValueExW works with a variety of types going in to store the result, but I can't cast any of them back to LPWSTR.

LPWSTR value;
HKEY hKey;

long result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"RegEntry1", 0, ACCESS|KEY_WOW64_32KEY, &hKey); 
if (result == ERROR_SUCCESS)
{
   //WCHAR buffer[512];
   //TCHAR buffer[512];
   LPWSTR buffer[512];
   DWORD bufferSize = sizeof(buffer); 
   ULONG queryVal = 0; 

   queryVal = RegQueryValueExW(hKey, L"Path", 0, NULL, (LPBYTE)buffer, &bufferSize); 
   if (queryVal == ERROR_SUCCESS)
   { 
      //Access violation error here; I need some type of conversion.
      value = buffer;
   }
}

No posts that I've read on here so far have led me to an answer. C++ is not my primary dev language.

UPDATE: None of the proposed answers worked for me. I found an alternative way to do what I needed.

Your buffer variable is declaring an array of 512 wchar_t* pointers when it should be declaring an array of 512 wchar_t characters instead. The first commented-out line of code is the correct code to use:

WCHAR buffer[512]; 
DWORD bufferSize = sizeof(buffer);  

ULONG queryVal = RegQueryValueExW(hKey, L"Path", 0, NULL, (LPBYTE)buffer, &bufferSize);  
if (queryVal == ERROR_SUCCESS) 
{  
    //...
} 

Keep in mind that the buffer will not be null-terminated if the Registry value was not stored with its own null-terminator, so you should allocate some extra space for your own null terminator, just in case:

WCHAR buffer[512+1]; 
DWORD bufferSize = (sizeof(buffer) - sizeof(WCHAR));  

LONG queryVal = RegQueryValueExW(hKey, L"Path", 0, NULL, (LPBYTE)buffer, &bufferSize);  
if (queryVal == ERROR_SUCCESS) 
{  
    buffer[bufferSize / sizeof(WCHAR)] = 0;
    //...
} 

Alternatively, use RegGetValue() instead, which handles the null terminator for you:

WCHAR buffer[512+1];
DWORD bufferSize = sizeof(buffer);  

LONG queryVal = RegGetValueW(hKey, NULL, L"Path", RRF_RT_REG_SZ | RRF_RT_REG_EXPAND_SZ, NULL, buffer, &bufferSize);
if (queryVal == ERROR_SUCCESS) 
{  
    //...
} 

You don't want a buffer of LPWSTR , you want a buffer of wchar_t . A pointer to that will be LPWSTR as it's a typedef for wchar_t * .

These two lines from WinNT.h are relevant:

typedef wchar_t WCHAR;    // wc,   16-bit UNICODE character

typedef __nullterminated WCHAR *NWPSTR, *LPWSTR, *PWSTR;

Edit: I suspect the problem is with the part of the code you haven't shown us. Are you returning value from a function? If so then the problem is that you're returning a pointer to a buffer that has gone out of scope and been destroyed. I would return a std::wstring or CString instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM