Code improvement suggestions
I'd just like some general advice on the code below. I wrote it in order to initialize a global configuration structure with values from the registry. It works fine, but I really wasn't very happy with how it turned out structure wise. Does anybody have any suggestions on how it could be improved in terms of formatting, optimising, reduncy reducing, ect.?
Code:
//Include Files////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include "config.h"
#include "debug.h"
#include "global_definitions.h"
#include "memory.h"
#include "numio.h"
#include "stringio.h"
///////////////////////////////////////////////////////////////////////////////////////////
//Global variables/////////////////////////////////////////////////////////////////////////
MYAPPSTATUS myappStatus;
///////////////////////////////////////////////////////////////////////////////////////////
//Functions prototypes/////////////////////////////////////////////////////////////////////
void InitMyappStatus(MYAPPSTATUS * myappStatus);
LONG CheckRegValue(HKEY hKey, LPTSTR regValue, DWORD dataType, void * configPointer, void * defValue, DWORD defValueSize, DWORD maxValueSize, BOOL notFoundIsError, BOOL * errorMB); //This function cannot check for datatype missmatches
///////////////////////////////////////////////////////////////////////////////////////////
//Function definitions/////////////////////////////////////////////////////////////////////
void InitMyappStatus(MYAPPSTATUS * myappStatus)
{
DWORD keyDisposition;
BOOL notFoundIsError;
BOOL errorMB = 1;
HKEY hKey;
BYTE defaultVal[MAX_PATH]; //Default values are limited to the size of MAX_PATH to make my life easier and since they should never need to be longer than this anyway
RegCreateKeyEx(
HKEY_CURRENT_USER,
myapp_DEFAULT_REGKEY,
0,
(LPTSTR) "",
REG_OPTION_NON_VOLATILE,
KEY_READ | KEY_WRITE,
(LPSECURITY_ATTRIBUTES) NULL,
&hKey,
&keyDisposition);
notFoundIsError = (keyDisposition == REG_OPENED_EXISTING_KEY);
* (BOOL *) &defaultVal = 0;
CheckRegValue(hKey, "Test", REG_BINARY, (void *) &myappStatus->test, (void *) &defaultVal, sizeof(BOOL), sizeof(BOOL), notFoundIsError, &errorMB);
if(myappStatus->test > 1 || myappStatus->test < 0) //Invalid value test
{
myappStatus->test = * (BOOL *) &defaultVal;
ErrorMessage("Registry value \"Test\" was invalid. Default value used", NULL, NULL, MB_ICONEXCLAMATION, 1);
}
return;
}
LONG CheckRegValue(HKEY hKey, LPTSTR regValue, DWORD dataType, void * configPointer, void * defValue, DWORD defValueSize, DWORD maxValueSize, BOOL notFoundIsError, BOOL * errorMB)
{
char * mergedString[2];
char * numString;
LONG error;
if(error = RegQueryValueEx(hKey, regValue, NULL, NULL, (LPBYTE) configPointer, &maxValueSize))
{
if(error == 2) //This is what's returned when the value is not found
{
if(notFoundIsError)
{
*errorMB = ( IDOK == ErrorMessage( (mergedString[0] = StringMerge("Failed to Read Registry Key:", regValue, "\0")) , (mergedString[1] = StringMerge("Couldn't find registry value: ", regValue, ". If you have not deleted any ", VERSION, " keys then you may have a corrupted registry. Press OK to continue or Cancel to continue and skip any subsequent registy errors.", "\0")) , NULL, MB_ICONEXCLAMATION | MB_OKCANCEL, *errorMB) );
free((void *) mergedString[0]);
free((void *) mergedString[1]);
}
else
{
ErrorMessage((mergedString[0] = StringMerge("The following registry value was successfully initialized: ", regValue, "\0")), NULL, NULL, MB_ICONINFORMATION, 0);;
free((void *) mergedString[0]);
}
}
else
{
*errorMB = ( IDOK == ErrorMessage( (mergedString[0] = StringMerge("Failed to Read Registry Key:", regValue, "\0")), (mergedString[1] = StringMerge("Failed to read registry value \"", regValue, "\". Error code: ", numString = UINTToString((UINT) error, 10, 0), ". Press OK to continue or Cancel to continue and skip any subsequent registy errors.", "\0")), NULL, MB_ICONEXCLAMATION | MB_OKCANCEL, *errorMB) );
free((void *) mergedString[0]);
free((void *) mergedString[1]);
free((void *) numString);
}
RegSetValueEx(hKey, regValue, 0, dataType, (CONST BYTE *) defValue, defValueSize);
memcpy(configPointer, defValue, defValueSize);
}
return error;
}
///////////////////////////////////////////////////////////////////////////////////////////