Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Registry modifications in C++

SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
Alright, my new program needs to be able to modify registry keys. The program basically turns enhancements in WinXP on or off that are there, but off by default. They are ALL stored in the registry though, and I am having problems at this point.
[code]
void ModifyHKLMKey(char *pSubKey, char *pValueName, DWORD dwValue, char *pValue)
{
HKEY hkTemp;
TCHAR tcBuffer[MAX_PATH], tcValue[MAX_PATH];

if((dwValue < 0) && (pValue == NULL))
{
MessageBox(wdMain.ReturnHandle(), "No data supplied for a registry change.", "WinPMP Warning", MB_OK | MB_ICONEXCLAMATION);
return;
}

memset(tcBuffer, '', sizeof(tcBuffer));
wsprintf(tcBuffer, "%s", pSubKey);
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, tcBuffer, NULL, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkTemp, NULL))
{
MessageBox(wdMain.ReturnHandle(), "Failed to open or create a registry key.", "WinPMP Warning", MB_OK | MB_ICONEXCLAMATION);
return;
}

memset(tcBuffer, '', sizeof(tcBuffer));
wsprintf(tcBuffer, "%s", pValueName);
if((dwValue < 0) && (pValue != NULL))
{
memset(tcValue, '', sizeof(tcValue));
wsprintf(tcValue, "%s", pValue);
if(RegSetValueEx(hkTemp, tcBuffer, NULL, REG_DWORD, (LPBYTE)&tcValue, (DWORD)((strlen(tcValue) + 1) * sizeof(TCHAR))))
MessageBox(wdMain.ReturnHandle(), "Failed to modify a registry value.", "WinPMP Warning", MB_OK | MB_ICONEXCLAMATION);
}
else if((dwValue >= 0) && (pValue == NULL))
{
if(RegSetValueEx(hkTemp, tcBuffer, NULL, REG_DWORD, (LPBYTE)&dwValue, sizeof(DWORD)))
MessageBox(wdMain.ReturnHandle(), "Failed to modify a registry value.", "WinPMP Warning", MB_OK | MB_ICONEXCLAMATION);
}
else
MessageBox(wdMain.ReturnHandle(), "Invalid values supplied to the registry.", "WinPMP Warning", MB_OK | MB_ICONEXCLAMATION);

RegCloseKey(hkTemp);
}
[/code]
That function is supposed to modify keys in "HKEY_LOCAL_MACHINE", but no matter what data I pass to it, I get the last messagebox you see down there, about invalid data. I call it like shown below at one point.
[code]
ModifyHKLMKey("System\CurrentControlSet\Control\Session Manager\Memory Management", "ClearPageFileAtShutdown", 0, NULL);
[/code]
This setting can be 0 or 1, and 1 causes XP to wipe the entire pagefile at shutdown to erase passwords and such for people in a high-security environment. However, stuff like this causes the problem. Ideas?

Oh and, for some reason, once the application is compiled, everything works except the close button in the upper-right, the small X. However, if you drag the window then click it, it works fine. After a message box pops up, the X stops working until the window is moved again. What on earth would cause such a strange anomoly?

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : Alright, my new program needs to be able to modify registry keys. The program basically turns enhancements in WinXP on or off that are there, but off by default. They are ALL stored in the registry though, and I am having problems at this point.
    : [code]
    : void ModifyHKLMKey(char *pSubKey, char *pValueName, DWORD dwValue, char *pValue)
    : {
    : HKEY hkTemp;
    : TCHAR tcBuffer[MAX_PATH], tcValue[MAX_PATH];
    :
    : if((dwValue < 0) && (pValue == NULL))
    : {
    : MessageBox(wdMain.ReturnHandle(), "No data supplied for a registry change.", "WinPMP Warning", MB_OK | MB_ICONEXCLAMATION);
    : return;
    : }
    :
    : memset(tcBuffer, '', sizeof(tcBuffer));
    : wsprintf(tcBuffer, "%s", pSubKey);
    : if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, tcBuffer, NULL, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkTemp, NULL))
    : {
    : MessageBox(wdMain.ReturnHandle(), "Failed to open or create a registry key.", "WinPMP Warning", MB_OK | MB_ICONEXCLAMATION);
    : return;
    : }
    :
    : memset(tcBuffer, '', sizeof(tcBuffer));
    : wsprintf(tcBuffer, "%s", pValueName);
    : if((dwValue < 0) && (pValue != NULL))
    : {
    : memset(tcValue, '', sizeof(tcValue));
    : wsprintf(tcValue, "%s", pValue);
    : if(RegSetValueEx(hkTemp, tcBuffer, NULL, REG_DWORD, (LPBYTE)&tcValue, (DWORD)((strlen(tcValue) + 1) * sizeof(TCHAR))))
    : MessageBox(wdMain.ReturnHandle(), "Failed to modify a registry value.", "WinPMP Warning", MB_OK | MB_ICONEXCLAMATION);
    : }
    : else if((dwValue >= 0) && (pValue == NULL))
    : {
    : if(RegSetValueEx(hkTemp, tcBuffer, NULL, REG_DWORD, (LPBYTE)&dwValue, sizeof(DWORD)))
    : MessageBox(wdMain.ReturnHandle(), "Failed to modify a registry value.", "WinPMP Warning", MB_OK | MB_ICONEXCLAMATION);
    : }
    : else
    : MessageBox(wdMain.ReturnHandle(), "Invalid values supplied to the registry.", "WinPMP Warning", MB_OK | MB_ICONEXCLAMATION);
    :
    : RegCloseKey(hkTemp);
    : }
    : [/code]
    : That function is supposed to modify keys in "HKEY_LOCAL_MACHINE", but no matter what data I pass to it, I get the last messagebox you see down there, about invalid data. I call it like shown below at one point.
    : [code]
    : ModifyHKLMKey("System\CurrentControlSet\Control\Session Manager\Memory Management", "ClearPageFileAtShutdown", 0, NULL);
    : [/code]
    : This setting can be 0 or 1, and 1 causes XP to wipe the entire pagefile at shutdown to erase passwords and such for people in a high-security environment. However, stuff like this causes the problem. Ideas?
    :
    : Oh and, for some reason, once the application is compiled, everything works except the close button in the upper-right, the small X. However, if you drag the window then click it, it works fine. After a message box pops up, the X stops working until the window is moved again. What on earth would cause such a strange anomoly?
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
    :
    [blue]XP has some protection on Registry. That means that some keys are meant to be changed only with XP interface and not from running program - probably, security thing, and you hit one of these keys. Also, it may be a level of running program - every process in XP has privileges, like if it can or cannot write to specific Registry key. Try to modify the privileges of your program before changing the key.

    Check some APIs here:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthz/security/privileges.asp[/blue]
  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    [b][red]This message was edited by Sephiroth at 2006-12-4 15:31:9[/red][/b][hr]
    [b][red]This message was edited by Sephiroth at 2006-12-4 13:2:49[/red][/b][hr]
    That's not the problem. The problem is that the program is interpretting the two values sent (the DWORD and the char in the function parameters) as being invalid for some reason.

    I think the problem is Microsoft defining multiple data types as the exact same one. According to Visual Studio 2005, "TCHAR" and "wchar_t" are both "char". However, the registry requires TCHAR, and for some unknown reason, I cannot go from "char" to "TCHAR".

    I figured it out. When using ANSI instead of UNICODE, TCHAR is char and everything on the MSDN about converting to wide goes down the tubes. It runs and now properly modifies 23 registry entries.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]





Sign In or Register to comment.