If you wanted to dump a lot of values into the registry on a Windows XP system, you could create a reg file and then use RegEdit to import the reg file into the registry. But on a Windows CE device that functionality doesn’t exist. There are some registry editors available for Windows CE that will import a reg file though. Every once in a while I need to do it and don’t have a registry editor to do it for me, so I wrote a little function to write to the registry using an array of data.
First I created a data structure to use for defining the registry changes:
typedef struct {
                TCHAR *ValueName;
                DWORD Type;
                DWORD DwordValue;
                TCHAR *StringValue;
} RegEntry ;
 
The ValueName is used for value names, Type is used for the value type (but there is no value type for registry keys, so I define:
#define REG_KEY                             22
 DwordValue and StringValue are used for the data values. Then I create an array of the data that defines the registry changes, in this case some changes for the printer driver defaults.
 
RegEntry RegEntries[] = {
                {TEXT("Printers"), REG_KEY, 0, 0 },
                                {TEXT("DefaultPrinter"), REG_SZ, 0, TEXT("PCL Inkjet") },
                {TEXT("Printers\\PCL Inkjet"), REG_KEY, 0, 0 },
                                {TEXT("Color"), REG_SZ, 0, TEXT("Color") },
                                {TEXT("Draft Quality"), REG_SZ, 0, TEXT("300") },
                                {TEXT("High Quality"), REG_SZ, 0, TEXT("600") },
                                {TEXT("Driver"), REG_SZ, 0, TEXT("pclhd.dll") },
                                {TEXT("Port"), REG_SZ, 0, TEXT("LPT1:") },
                {TEXT("Printers\\PCL Laser"), REG_KEY, 0, 0 },
                                {TEXT("Color"), REG_SZ, 0, TEXT("Color") },
                                {TEXT("Draft Quality"), REG_SZ, 0, TEXT("300") },
                                {TEXT("High Quality"), REG_SZ, 0, TEXT("600") },
                                {TEXT("Driver"), REG_SZ, 0, TEXT("pclhd.dll") },
                                {TEXT("Port"), REG_SZ, 0, TEXT("LPT1:") },
                {NULL, 0, 0, NULL}
};
 
Finally a function that loops through the data and creates keys and values in the registry.
 
void ModifyAddRegistry( RegEntry *RegEntries )
{
                HKEY hTargetKey = 0;
                DWORD dwDisposition;
                DWORD RetVal;
                DWORD Index=0;
                BYTE *Value;
                DWORD uSize;
 
                while( RegEntries[Index].ValueName != NULL )
                {
                                if( RegEntries[Index].Type == REG_KEY )
                                {
                                                if( hTargetKey != 0 )
                                                                RegCloseKey( hTargetKey );
 
                                                RetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                                                                                RegEntries[Index].ValueName,
                                                                                0,
                                                                                0,
                                                                                &hTargetKey
                                                                                );
                                                if( ERROR_SUCCESS != RetVal )
                                                {
                                                                hTargetKey = 0;
                                                                RetVal = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
                                                                                RegEntries[Index].ValueName,
                                                                                0,
                                                                                NULL,
                                                                                REG_OPTION_NON_VOLATILE,
                                                                                KEY_ALL_ACCESS ,
                                                                                NULL,
                                                                                &hTargetKey,
                                                                                &dwDisposition
                                                                                );
                                                }
                                }
                                else
                                {
                                                if( RegEntries[Index].Type == REG_DWORD )
                                                {
                                                                Value = (BYTE *)&RegEntries[Index].DwordValue;
                                                                uSize = sizeof( DWORD );
                                                }
                                                else
                                                {
                                                                Value = (BYTE *)RegEntries[Index].StringValue;
                                                                uSize = (wcslen( RegEntries[Index].StringValue ) + 1 ) * sizeof( TCHAR );
                                                }
 
                                                RetVal = RegSetValueEx(hTargetKey,
                                                                                RegEntries[Index].ValueName,
                                                                                0,
                                                                                RegEntries[Index].Type,
                                                                                (CONST BYTE *)Value,
                                                                                uSize
                                                                                );
                                }
                                Index++;
                }
                if( hTargetKey != 0 )
                                RegCloseKey( hTargetKey );
}
 
Copyright © 2009 – Bruce Eitman
All Rights Reserved