Click to See Complete Forum and Search --> : Using RegSetValueEx to set a value in the registry


skalek
November 26th, 2005, 01:52 AM
I am having trouble getting my program to set data to a value using RegSetValueEx.

My problem definitely has to do with my lack of C knowledge and assigning the the correct data to the RegSetValueEx function..specifically the 5th argument which requires a byte (?). Can anyone steer me in the right direction?

Thanks

#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
HKEY hKey;
DWORD dwType;
DWORD dwLen;
unsigned char szBuf[255] = {0};
char *homepage = "http://www.google.com";

if (RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Internet Explorer\\Main", 0, KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS)
return 1;

if (RegQueryValueEx(hKey, "Start Page", 0, &dwType, szBuf, &dwLen) != ERROR_SUCCESS)
return 1;

printf("Value is %s\n", szBuf);

if(RegSetValueEx(hKey,"Start Page",0,REG_SZ, (LPBYTE) homepage, sizeof(homepage) != ERROR_SUCCESS))
printf("Error in setting value\n");
else
printf("Value Set\n");


if (RegQueryValueEx(hKey, "Start Page", 0, &dwType, szBuf, &dwLen) != ERROR_SUCCESS)
return 1;

printf("Value is %s\n", szBuf);

RegCloseKey(hKey);

return 0;
}



Everything works, except for my ability to set the Start Page value to my specified home page.

stober
November 26th, 2005, 04:22 AM
sizeof(homepage) will return 4 because homepage is a pointer. What you want is strlen(homepage).

skalek
November 26th, 2005, 11:05 AM
What about my 5th argument? The function declarion states it needs a data type of const BYTE* lpData. I am not exactly sure how to go about assigning a string to that type of variable.

This is the part of C that I am just too clueless on.

Marc G
November 26th, 2005, 12:19 PM
[ moved thread ]

stober
November 26th, 2005, 12:19 PM
if(RegSetValueEx(hKey,"Start Page",0,REG_SZ, (LPBYTE) homepage,
strlen(homepage)+1) != ERROR_SUCCESS)

The above line is wrong in your post, closing parentheses is in the wrong place.

Marc G
November 26th, 2005, 12:24 PM
What about my 5th argument? The function declarion states it needs a data type of const BYTE* lpData. I am not exactly sure how to go about assigning a string to that type of variable.

This is the part of C that I am just too clueless on.
Your 5th argument is ok BYTE is equivalent to char. The problem is your sizeof(homepage) like stober said.

skalek
November 26th, 2005, 04:19 PM
Thanks all...that parenthesis was messing things up as well. Appreciate the help for pointing me in the right direction.