OS crashes on calling Registry functions
Im excercising some registry functions for my automation. I am not too sure which function, but my OS crahses on reboot. (refuses to start).
I am using the following API's
RegReplaceKey, RegUnloadKey, CreateProcessAsUSer and CreateProcessAsToken.
Any help would be appriciated. I have logged in as Administrator.
I am also adding the code snippet here:
INT TestRegReplaceKeyA( VOID )
{
LONG ReturnVal = 0;
HKEY KeyHandle = NULL;
CHAR filePath[STD_BUFFER_SIZE] = {0};
CHAR fullFilePath[STD_BUFFER_SIZE] = {0};
CHAR oldfilePath[STD_BUFFER_SIZE] = {0};
CHAR fullOldFilePath[STD_BUFFER_SIZE] = {0};
HRESULT Result = E_FAIL;
HANDLE handle = NULL;
printf("Test 220 - RegReplaceKeyA\n");
// Create the filePath name for the new file to be created
Result = StringCchCopyA(filePath,
ARRAYSIZE( filePath ),
"%programfiles%\\DELETEThis2201.reg" );
if ( FAILED( Result ) )
{
printf( "Error 220, unable to create filePath name.\n" );
ReturnVal = 1;
goto cleanup;
}
if ( ExpandEnvironmentStringsA(filePath, fullFilePath, ARRAYSIZE( fullFilePath ) ) == 0 )
{
printf( "Error 220, unable to create fullFilePath name.\n" );
ReturnVal = 1;
goto cleanup;
}
// Create the filePath name for the new file to be created for the OldRegistryValues
Result = StringCchCopyA( oldfilePath,
ARRAYSIZE( oldfilePath),
"%programfiles%\\DELETEThisOld2201.reg" );
if ( FAILED( Result ) )
{
printf( "Error 220, unable to create filePath name.\n" );
ReturnVal = 1;
goto cleanup;
}
if ( ExpandEnvironmentStringsA(oldfilePath, fullOldFilePath, ARRAYSIZE( fullOldFilePath ) ) == 0 )
{
printf( "Error 220, unable to create fullFilePath name.\n" );
ReturnVal = 1;
goto cleanup;
}
// Create a new key under HKLM
Result = RegCreateKeyA( HKEY_LOCAL_MACHINE,
"SOFTWARE\\DELETEThisKey220",
&KeyHandle);
if ( FAILED(Result) )
{
printf( "Error 220, unable to create a key under HKLM Software." );
ReturnVal = 1;
goto cleanup;
}
SetPrivileges(SE_BACKUP_NAME, 1);
SetPrivileges(SE_RESTORE_NAME, 1);
// Create the backup file
Result = RegSaveKeyA( KeyHandle,
fullFilePath,
NULL);
if ( FAILED(Result) )
{
DWORD lastErr = GetLastError();
ReturnVal = 1;
printf( "Error 220 - %s. %s",
"Unable to save the specified key and all of its subkeys and values to a new file."
"Error Code = %d\n", lastErr );
goto cleanup;
}
//RegCloseKey(KeyHandle);
SetPrivileges(SE_RESTORE_NAME, TRUE);
// Replace the key using API RegReplaceKey
Result = RegReplaceKeyA( HKEY_LOCAL_MACHINE,
"SOFTWARE\\DELETEThisKey220",
fullFilePath,
fullOldFilePath );
if ( FAILED(Result) )
{
printf( "Error 220, unable to retrieve a handle to the key" );
ReturnVal = 1;
goto cleanup;
}
DeleteFileA( fullOldFilePath);
DeleteFileA( fullFilePath);
cleanup:
SetPrivileges(SE_BACKUP_NAME, FALSE);
SetPrivileges(SE_RESTORE_NAME, FALSE);
if ( NULL != KeyHandle )
{
RegCloseKey( KeyHandle );
}
return ReturnVal;
}
INT TestRegUnLoadKeyA( VOID )
{
LONG ReturnVal = 0;
HKEY KeyHandle = NULL;
CHAR CompName[STD_BUFFER_SIZE] = {0};
DWORD CNSize = ARRAYSIZE( CompName );
CHAR filePath[STD_BUFFER_SIZE] = {0};
CHAR fullFilePath[STD_BUFFER_SIZE] = {0};
HRESULT Result = E_FAIL;
printf("Test 224 - RegUnLoadKeyA\n");
if ( 0 == GetComputerNameExA( ComputerNameNetBIOS,
CompName,
&CNSize ) )
{
DWORD lastErr = GetLastError();
ReturnVal = 1;
printf( "Error 224 - Error in getting computer name. Error Code = %d\n", lastErr );
goto cleanup;
}
// Create a new key under HKLM
Result = RegConnectRegistryA( CompName,
HKEY_LOCAL_MACHINE,
&KeyHandle);
if ( FAILED( Result ))
{
printf( "Error 224, unable to connect to HKLM." );
ReturnVal = 1;
goto cleanup;
}
// Close the key
Result = RegCloseKey( KeyHandle );
if( FAILED( Result ))
{
printf( "Error 224, unable to close a key under HKLM Software." );
ReturnVal = 1;
goto cleanup;
}
// Create a new key under HKLM
SetPrivileges(SE_BACKUP_NAME, 1);
SetPrivileges(SE_RESTORE_NAME, 1);
Result = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
L"SOFTWARE\\DELETEThisKey224",
0,
NULL,
REG_OPTION_BACKUP_RESTORE,
KEY_ALL_ACCESS,
NULL,
&KeyHandle,
NULL );
if ( FAILED( Result ))
{
printf( "Error 224, unable to create a key under HKLM Software." );
ReturnVal = 1;
goto cleanup;
}
// Create the filePath name for the new file to be created
Result = StringCchCopyA(filePath,
ARRAYSIZE( filePath ),
"%programfiles%\\DELETEThis224.reg" );
if ( FAILED( Result ) )
{
printf( "Error 224, unable to create filePath name.\n" );
ReturnVal = 1;
goto cleanup;
}
if ( ExpandEnvironmentStringsA(filePath, fullFilePath, ARRAYSIZE( fullFilePath ) ) == 0 )
{
printf( "Error 224, unable to create fullFilePath name.\n" );
ReturnVal = 1;
goto cleanup;
}
SetPrivileges(SE_BACKUP_NAME, 1);
SetPrivileges(SE_RESTORE_NAME, 1);
// Create the backup file
Result = RegSaveKeyA( KeyHandle,
fullFilePath,
NULL);
if ( FAILED( Result ))
{
DWORD lastErr = GetLastError();
printf( "Error 224 - %s. %s",
"Unable to save the specified key and all of its subkeys and values to a new file."
"Error Code = %d\n", lastErr );
ReturnVal = 1;
goto cleanup;
}
RegCloseKey(KeyHandle);
SetPrivileges(SE_RESTORE_NAME, 1);
SetPrivileges( SE_TCB_NAME , 1 );
// Load the key
Result = RegLoadKeyA( HKEY_LOCAL_MACHINE,
"SOFTWARE\\DELETEThisKey2241",
fullFilePath);
if ( FAILED( Result ))
{
printf( "Error 224 - Unable to load the specified key." );
ReturnVal = 1;
goto cleanup;
}
//RegFlushKey( HKEY_LOCAL_MACHINE);
/*if ( NULL != KeyHandle )
{
RegCloseKey( KeyHandle );
}*/
// Unload the key using API RegUnLoadKey
Result = RegUnLoadKeyA( HKEY_LOCAL_MACHINE,
"SOFTWARE\\DELETEThisKey2241"
);
if ( FAILED( Result ))
{
printf( "Error 224, unable to retrieve a handle to the key." );
ReturnVal = 1;
goto cleanup;
}
cleanup:
SetPrivileges(SE_BACKUP_NAME, 0);
SetPrivileges(SE_RESTORE_NAME, 0);
if ( NULL != KeyHandle )
{
RegCloseKey( KeyHandle );
}
return ReturnVal;
}

