I am making some functions that use regcreatekeyex, regsetvalueex, ect. So I made them and tested them by making a command prompt program and had no problems. Then I tried using the in the form designer and the compiler keeps telling me it can't convert my parameters that are strings into LPCWSTR. So I add the (LPCWSTR) in front of the parameter to convert it. Well my project compiles but recreatekeyex and stuff never succeed. How can I fix this

LPCWSTR doesn't work with reg commands
Patrick in Canada
What is the registry type of the value you're querying, is it a REG_SZ, REG_BINARY, REG_EXPAND_SZ
Also, now that you've corrected most of the errors, can you backup, delete and recreate the registry key with your corrected program
John Holden
Did you actually check rgValue between the two lines I told you to check (After RegCloseKey(hk) but before return rgValue)
I don't think you performed what I asked (check rgValue INSIDE the function). Your latest reply indicates you've been checking rgValue from OUTSIDE the function.
You can't read rgValue from outside the function, because rgValue is a local variable, therefore only has meaning inside that function. In order to read it outside the function, you either have to make rgValue global, or place its on dynamic memory (which is what the malloc does).
On the second part: Don't use MessageBoxA, use MessageBoxW!
Ade Morgan
Mike Yamashita
Does the value look like garbage when you view it in Registry editor (regedit.exe)
StuartLodge
mike in nyc
I have a question: does rgValue look okay in between the lines:
RegCloseKey(hk);
/* Over here eg. Do a OutputDebugString(), or printf here... and does it come out okay */
return rgValue;
Evenstar
The following C++ code compiles and runs fine for me.
#include
<windows.h>#include <iostream>
#include <tchar.h>
wchar_t
*ReadKey(wchar_t *path, wchar_t *key){
HKEY hk;
wchar_t rgValue [1024];DWORD size;
DWORD Type; if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,path,0,KEY_QUERY_VALUE, &hk)
== ERROR_SUCCESS)
{
size=1023;
RegQueryValueEx( hk, key, NULL, &Type, (LPBYTE)rgValue, &size);
}
RegCloseKey(hk);
return rgValue;std::wcout << rgValue << std::endl;
}
void
WriteKey(wchar_t *path, wchar_t *name, wchar_t *value, int size){
HKEY hk;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCWSTR)path, 0, KEY_ALL_ACCESS, &hk))
{
exit(1);
} if (RegSetValueEx(hk, (LPCWSTR)name, 0, REG_EXPAND_SZ, (LPBYTE)value, size))
{
exit(1);
}
RegCloseKey(hk);
}
void
CreateKey(wchar_t *key){
HKEY hk;
if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, key, 0, NULL, REG_OPTION_NON_VOLATILE,DWORD dwHappened;
KEY_WRITE, NULL, &hk, &dwHappened))
{
}
RegCloseKey(hk);
}
int wmain(void){
CreateKey(L"SOFTWARE\\Foo");
WriteKey(L"SOFTWARE\\Foo", L"BAR", L"Data", 4 * sizeof(wchar_t));
ReadKey(L"SOFTWARE\\Foo", L"BAR");
return 0;
}
I do not get garbage out in rgValue
Mensana
Well that fixed up most of my functions the only one not working is ReadKey. When I use it it returns jibberish. This is the modified function.
"
wchar_t
* ReadKey(wchar_t* path, wchar_t* key){
HKEY hk;
wchar_t rgValue [1024];DWORD size;
DWORD Type;
if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,path,0,KEY_QUERY_VALUE, &hk) == ERROR_SUCCESS){
size=1023;
RegQueryValueEx( hk, key, NULL, &Type,(LPBYTE)rgValue,&size);
}
RegCloseKey(hk);
return
rgValue;}
"
Sahikon
Puckie Penn
by the way after messing with my function for a bit I managed to get the first letter from my key but notning else.
"
wchar_t
* ReadKey(wchar_t* path, wchar_t* key){
HKEY hk;
wchar_t *rgValue;DWORD size;
DWORD Type;
if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,path,0,KEY_QUERY_VALUE, &hk) == ERROR_SUCCESS){
RegQueryValueEx( hk, key, NULL, NULL,NULL,&size);
rgValue = (
wchar_t*)malloc(size);RegQueryValueEx( hk, key, NULL, NULL,(LPBYTE) rgValue,&size);
}
RegCloseKey(hk);
return
rgValue;}
"
vasko
Here is the code for my functions
"
char
* ReadKey(char* path, char* key){
HKEY hk;
char rgValue [1024];DWORD size;
DWORD Type;
if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,(LPCWSTR) path,0,KEY_QUERY_VALUE, &hk) == ERROR_SUCCESS){
size=1023;
RegQueryValueEx( hk,(LPCWSTR) key, NULL, &Type,(LPBYTE)rgValue,&size);
}
RegCloseKey(hk);
return
rgValue;}
void
WriteKey(char* path, char* name, char* value, int size){
HKEY hk;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,(LPCWSTR) path,0,KEY_ALL_ACCESS,&hk)){
exit(1);
}
if (RegSetValueEx(hk,(LPCWSTR) name, 0,REG_EXPAND_SZ, (LPBYTE)value,size)){
exit(1);
}
RegCloseKey(hk);
}
void CreateKey(char* key){
HKEY hk;
DWORD dwHappened;
if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,(LPCWSTR) key0, NULL, REG_OPTION_NON_VOLATILE,
KEY_WRITE, NULL, &hk, &dwHappened))
{
}
RegCloseKey(hk);
}
"
The_S_T_
James Walters
Problems similar to this have been asked before. You'll notice that the same error pops up with ::MessageBox().
First of all, note that you cannot convert a char* to TCHAR* by using a cast (Note: LPCTSTR is typedef'ed as a TCHAR pointer NOT a char pointer). You need to alter your function to use TCHARs instead of chars.
This is a good thread that gives the solution concisely (pay attention to the differences between Takashi's code and purplenite's code):
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=164510&SiteID=1
The reason it occurs is covered here:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=278234&SiteID=1
These threads may also be useful:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=279875&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=136598&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=268288&SiteID=1
Mikeq