Registry doesn't work like it should
Hello guys (first post here).
I've been looking quite a while on the internet on the registry functions in Visual Studio 2010.
I've come across the functions RegOpenKeyEx and RegSetValueEx (the ones I need).
But; according to what I come across in EVERY forum; this should work:
Code:
HKEY hKey;
RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS, &hKey)
RegSetValueEx(hKey, "TEST", 0, REG_SZ, (LPBYTE) tbuf, strlen(tbuf) + 1
With tbuf just holding the path name of a program.
Pretty normal code.
The strange part is, in the first line
Code:
RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS, &hKey)
The program keeps complaining that in the "SOFTWARE...etc" part, the compiler says it is not of the time LPCWSTR. Seems logical to me, but in ALL examples I found on the internet, they were NOT getting this error message.
But even if I change the type to LPCWSTR (by just typing (LPCWSTR)"SOFTWARE... etc" I do not get an error anymore, but the program just does not work.
Can anyone explain why my compiler keeps complaining about that piece of string not being of the type LPCWSTR and ALL others don't get an error?
Thanks.
Re: Registry doesn't work like it should
Well hello,
I made the OpenKey function work, FINALLY.
BUT... I have a new problem, and I really don't know what is wrong.
Code:
DWORD dwType=0, dwDataSize=0, dwBufSize=0;
LONG lResult;
LPTSTR szVal;
LPTSTR * tmpRes;
tmpRes = &szVal;
// I let all the error checking out, so it isn't too much to read
RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_ALL_ACCESS, &hKey);
// THIS WORKS (I think), since hKey contains a value != NULL after this procedure
RegQueryValueEx(hKey, _T("a"), 0, &dwType, NULL, &dwDataSize);
// THIS DOES NOT WORK?????
// And yes, in Regedit.exe I manually created a value named "a" (type REG_SZ) with the value "testje!" ("test!" in english, the other is dutch).
// Please help me, I have read the MSDN documents until my eyes started hurting, as well as every forum post I could find about this function...........................
// dwDataSize always remains 0.............................
?? :(
Re: Registry doesn't work like it should
Quote:
Originally Posted by
smonjirez
Well hello,
I made the OpenKey function work, FINALLY.
BUT... I have a new problem, and I really don't know what is wrong.
Code:
DWORD dwType=0, dwDataSize=0, dwBufSize=0;
LONG lResult;
LPTSTR szVal;
LPTSTR * tmpRes;
tmpRes = &szVal;
// I let all the error checking out, so it isn't too much to read
RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_ALL_ACCESS, &hKey);
// THIS WORKS (I think), since hKey contains a value != NULL after this procedure
RegQueryValueEx(hKey, _T("a"), 0, &dwType, NULL, &dwDataSize);
// THIS DOES NOT WORK?????
// And yes, in Regedit.exe I manually created a value named "a" (type REG_SZ) with the value "testje!" ("test!" in english, the other is dutch).
// Please help me, I have read the MSDN documents until my eyes started hurting, as well as every forum post I could find about this function...........................
// dwDataSize always remains 0.............................
?? :(
Mind me, I don't know C++, but if I read the documentation correctly it should be something like
Code:
DWORD dwType=0, dwDataSize=0, dwBufSize=0;
LONG lResult;
LPTSTR szVal;
LPTSTR * tmpRes;
LPBYTE lpReturnVal;
tmpRes = &szVal;
RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_ALL_ACCESS, &hKey);
RegQueryValueEx(hKey, _T("a"), NULL, &dwType, &lpReturnVal, &dwDataSize);
Where lpReturnVal is the pointer to the retrieved data.
&dwType could also be NULL
The MSDN documentation also shows an example down the page.
http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
Welcome to VBF, by the way :wave:
Re: Registry doesn't work like it should
Correct.
What you are describing is the procedure for returning a pointer to the retrieved data.
But in this piece of code, the only goal was to determine the size of the returned data (got it from a script somewhere).
Anyways, even if I execute it like in your example, it doesn't work.
And thanks by the way :).
Re: Registry doesn't work like it should
What does dwDataSize return?
Re: Registry doesn't work like it should
It just returns 0.
Just tried another example I found on internet:
Note: this is AFTER the piece of code I used before to open the key in the first place:
Code:
HKEY hKey;
RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_SET_VALUE, &hKey);
Code:
if (RegSetValueEx(hKey,
_T(REGNAME_TO_WRITE),
0,
REG_SZ,
(const unsigned char *)tbuf,
strlen(tbuf)) == ERROR_SUCCESS) {
cout << "Success!\n";
Again, the program "works just fine", but it doesn't write a value in the registry at all.