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?
Printable View
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?
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.
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;
}
The following has also been useful.
Thanks for the answers..Code:http://hawk-ware.awardspace.com/tutorials/cppreg.html