Vanilla C with Sprinkles – Windows Registry Fun!
checking the windows registry using vanilla C
Published by Ryan Boucher @ 11:55 pm
The registry functions do a lot of duplication but are primarily used for deployment validation so building objects of keys and enumerating these arrays is not necessary. Some of these leverage the file functions.
Does Registry Path Exist?
void CheckRegistryPathExists (const char* const Server, const char* const SubKey)
{
const char LineSeperators[] = "\n" ;
const char LinePartSeperators[] = "\t" ;
char* LineToken;
char* LinePartToken ;
char* NextPartToken ;
lr_set (Server, "__CheckRegistryValue_Server") ;
lr_set (SubKey, "__CheckRegistryValue_SubKey") ;
Run the REG Query command and pipe the results into a temporary file.
system
(
lr_get
(
"REG QUERY \"\\\\{__CheckRegistryValue_Server}\\{__CheckRegistryValue_SubKey}\" > {TempFile}"
)
) ;
Read temp file in and look for the exact string specified below. If there is no registry value found then it writes a specific error message out. We can use this to determine whether our value exists in the registry.
ReadFile (lr_get ("{TempFile}"), "RegExtract", "UTF-8") ;
if (strstr (lr_get ("{RegExtract}"), "Error: The system was unable to find the specified registry key or value") != '\0')
{
lr_error_message (lr_get ("The supplied registry path ({__CheckRegistryValue_Server}\\{__CheckRegistryValue_SubKey}) does not exist")) ;
}
}
Check Registry Value
void CheckRegistryValue (const char* Server, const char* SubKey, const char* Property, const char* ExpectedValue)
{
const char LineSeperators[] = "\n" ;
const char LinePartSeperators[] = "\t" ;
char* LineToken;
char* LinePartToken ;
char* NextPartToken ;
lr_set (Server, "__CheckRegistryValue_Server") ;
lr_set (SubKey, "__CheckRegistryValue_SubKey") ;
lr_set (Property, "__CheckRegistryValue_Property") ;
system
(
lr_get
(
"REG QUERY \"\\\\{__CheckRegistryValue_Server}\\{__CheckRegistryValue_SubKey}\" /v {__CheckRegistryValue_Property} > {TempFile}"
)
) ;
ReadFile (lr_get ("{TempFile}"), "RegExtract", "UTF-8") ;
There is a “standard” format for the REG query output.
//Format for Reg output
// blank line
// declaration
// blank line
// path
// property type value
Tokenise the string and then parse it to get the value.
LineToken = (char *)strtok(lr_get ("{RegExtract}"), LineSeperators); // Get the first token
if (!LineToken)
{
lr_error_message ("Registry Value Not Found (No tokens found in string!)") ;
return ;
}
while (LineToken != NULL)
{
// Find line that contains the property
if (strstr (LineToken, lr_get ("{__CheckRegistryValue_Property}")) > 0)
{
//The format of the line is "property[\t]type[\t]value"
LinePartToken = (char *)strtok(LineToken, LinePartSeperators); // Get the first token
if (!LinePartToken)
{
lr_error_message ("Registry Value Not Found (No tokens found in string!)") ;
return ;
}
while (LinePartToken != NULL)
{
NextPartToken = LinePartToken ;
LinePartToken = (char *) strtok (NULL, LinePartSeperators) ;
}
//we have the last token in 'Next' so use that
Trim (NextPartToken, "ActualValue") ;
TrimNewLine (lr_get ("{ActualValue}"), "ActualValue") ;
CompareStrings (lr_get ("{ActualValue}"), ExpectedValue) ;
return ;
}
LineToken = (char *) strtok (NULL, LineSeperators) ;
}
lr_error_message ("Registry Value Not Found") ;
}
|
|
Ryan Boucher is a Software Inquisitor and is passionate about it. You can find a whole raft of articles and anecdotes about software testing and other topics he gets excited about. |
| Tags |