Thread: Registry manipulation

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    60

    Registry manipulation

    Hi,
    I was wondering if there is a way to change some registry variables through some C code (under WIN XP).
    Kind of things that a program does during installation (?)
    Any help/links?
    Last edited by tezcatlipooca; 04-28-2007 at 03:16 PM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    http://msdn.microsoft.com/
    'n Search for registry or something...

    If your going to get into windows programming, that site will be your best friend.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Example of registry manipulation on remote computer. To manipulate registry on local computer, set szRemoteComputerName to NULL.

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    #pragma comment(lib, "advapi32.lib")
    char szRegistryKey[128] = "SOFTWARE\\MyTest";
    
    BOOL DeleteRegistryKey(char *ComputerName)
    {
        BOOL bReturn = TRUE;
        HKEY hKeyRemote;    
        HKEY hKey;
    
        char szInputComputerName[48];
        if(ComputerName[0] != '\0')
            sprintf(szInputComputerName, "\\\\%s", ComputerName);
        else
            strcpy(szInputComputerName,"");
        if( RegConnectRegistry(szInputComputerName, HKEY_LOCAL_MACHINE, &hKeyRemote)
            != ERROR_SUCCESS)
        {
            bReturn = FALSE;
        }
        if(RegDeleteKey(hKeyRemote, szRegistryKey) != ERROR_SUCCESS)
        {
            bReturn = FALSE;
        }
        RegCloseKey(hKeyRemote); 
        return bReturn;
    }
    
    BOOL UpdateRegistry(char *ComputerName, char *ComPort, DWORD *dwRS232Timeout,DWORD *dwDebugMode, DWORD *dwServerPort, DWORD *dwLogFileSize)
    {
        HKEY hKeyRemote;    
        HKEY hKey;
        LPDWORD lpdwDisp = 0;
        char szInputComputerName[48];
    
        BOOL bUpdateSuccessful = TRUE;
        memset(szInputComputerName, 0, sizeof szInputComputerName);
        if(ComputerName[0] != '\0')
            sprintf(szInputComputerName, "\\\\%s", ComputerName);
        else
            strcpy(szInputComputerName,"");
    
        if( RegConnectRegistry(szInputComputerName, HKEY_LOCAL_MACHINE, &hKeyRemote)
            != ERROR_SUCCESS)
        {
            return FALSE;
        }
        if( RegCreateKeyEx( hKeyRemote, szRegistryKey, 0L,NULL,
            REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,lpdwDisp))
        {
            bUpdateSuccessful = FALSE;
        }
        else
        {
            if (RegSetValueEx(hKey, "ComPort", 0, REG_SZ,(LPBYTE) ComPort,(DWORD)
                lstrlen(ComPort)+1))
            {
                bUpdateSuccessful = FALSE;
            }
            if (RegSetValueEx (hKey, "RS232Timeout", 0L, REG_DWORD,(CONST BYTE*)
                &dwRS232Timeout, sizeof(DWORD)))
            {
                bUpdateSuccessful = FALSE;
            }
            if (RegSetValueEx (hKey, "DebugMode", 0L, REG_DWORD,(CONST BYTE*)
                &dwDebugMode, sizeof(DWORD)))
            {
                bUpdateSuccessful = FALSE;
            }   
            if (RegSetValueEx (hKey, "ServerPort", 0L, REG_DWORD,(CONST BYTE*)
                &dwServerPort, sizeof(DWORD)))
            {
                bUpdateSuccessful = FALSE;
            } 
            if (RegSetValueEx (hKey, "LogFileSize", 0L, REG_DWORD,(CONST BYTE*)
                &dwLogFileSize, sizeof(DWORD)))
            {
                bUpdateSuccessful = FALSE;
            } 
        }
        RegCloseKey(hKey); 
        RegCloseKey(hKeyRemote); 
        return bUpdateSuccessful;
    }
    
    BOOL ReadRegistry(char *ComputerName,char *ComPort, DWORD &dwRS232Timeout, DWORD &dwDebugMode, DWORD &dwServerPort, DWORD &dwLogFileSize)
    {
    
        HKEY hKeyRemote, hKey;
        char szComPort[6] = {0};
        char szInputComputerName[64] = {0};
        BOOL bReturn = TRUE;
        DWORD datasize = 0;
        DWORD type = 0;
        if(ComputerName[0] != '\0')
            sprintf(szInputComputerName, "\\\\%s", ComputerName);
        else
            strcpy(szInputComputerName,"");
    
        if( RegConnectRegistry(szInputComputerName, HKEY_LOCAL_MACHINE, &hKeyRemote)
            != ERROR_SUCCESS)
        {
            return FALSE;
        }
        if(RegOpenKeyEx (hKeyRemote, szRegistryKey, 0, KEY_QUERY_VALUE,
            &hKey) == ERROR_SUCCESS)
        {
            datasize = sizeof (szComPort);
            if (RegQueryValueEx(hKey, "ComPort",NULL, NULL, (unsigned char *)&szComPort,
                (unsigned long *)&datasize) != ERROR_SUCCESS)
                bReturn = FALSE;
            else 
                strcpy(ComPort , szComPort);
            datasize = sizeof(DWORD);
            if(RegQueryValueEx(hKey, "RS232Timeout",  0L, &type, (LPBYTE)&dwRS232Timeout,
                &datasize) != ERROR_SUCCESS)
            {
                dwRS232Timeout  = 0;
                bReturn = FALSE;
            }
            if(RegQueryValueEx(hKey, "DebugMode",  0L, &type, (LPBYTE)&dwDebugMode,
                &datasize) != ERROR_SUCCESS)
            {
                dwDebugMode = 0;  
                bReturn = FALSE; 
            }
            if(RegQueryValueEx(hKey, "ServerPort",  0L, &type, (LPBYTE)&dwServerPort,
                &datasize) != ERROR_SUCCESS)
            {
                dwServerPort = 0;  
                bReturn = FALSE; 
            }
            if(RegQueryValueEx(hKey, "LogFileSize",  0L, &type, (LPBYTE)&dwLogFileSize,
                &datasize) != ERROR_SUCCESS)
            {
                dwLogFileSize = 0;
                bReturn = FALSE; 
            }
            RegCloseKey(hKey);     
            RegCloseKey(hKeyRemote);
        }
        else bReturn = FALSE;
        return bReturn;
    }
    
    int main(void)
    {
        char szRemoteComputerName[] = {"NET0"};
        char szComPort[6] = {0};
        DWORD dwRs232Timeout = 0; 
        DWORD dwServerPort = 0;
        DWORD dwLogFileSize = 0;
        DWORD dwDebugMode = 0 ;
        if(UpdateRegistry(szRemoteComputerName, "COM4", (DWORD *)4000, (DWORD *)1,(DWORD *)4390,(DWORD *)100 ) == TRUE)
            printf("update successful\n");
        else
            printf("update failed\n");
    
        if(  ReadRegistry(szRemoteComputerName,szComPort, dwRs232Timeout, dwDebugMode,dwServerPort, dwLogFileSize) == FALSE)
            printf("Read failed\n");
        else
            printf("Read successful,  Comport = %s RS232Timeout = %d DebugMode = %d  ServerPort = %d LogSize = %d\n", szComPort,dwRs232Timeout, dwDebugMode,dwServerPort, dwLogFileSize );
        if(DeleteRegistryKey(szRemoteComputerName) == TRUE)
            printf("deletion successful\n");
        else
            printf("deletion failed\n");
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    The following has also been useful.
    Code:
    http://hawk-ware.awardspace.com/tutorials/cppreg.html
    Thanks for the answers..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Registry, Regedit
    By franse in forum C++ Programming
    Replies: 21
    Last Post: 01-29-2009, 09:57 AM
  2. Registry
    By gvector1 in forum C# Programming
    Replies: 0
    Last Post: 07-30-2003, 04:02 PM
  3. Registry Manipulation in C++
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 06-02-2002, 10:18 PM
  4. Registry Manipulation in C++
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 06-02-2002, 09:00 PM
  5. Registry Access
    By ExDigit in forum Windows Programming
    Replies: 3
    Last Post: 01-04-2002, 04:02 AM