Code:
void CreateNewKey( char * keyName, HKEY lPredefinedKey ) {
long hKey;
unsigned long result;
RegCreateKeyEx( (HKEY)lPredefinedKey, keyName, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, (HKEY *)&hKey, &result );
RegCloseKey( (HKEY)hKey );
}
void SetKeyValue( char *keyName, char *valueName, BYTE *valueSetting, long valueType, long valueSize ) {
long hKey;
RegOpenKeyEx( HKEY_CLASSES_ROOT, keyName, 0, KEY_ALL_ACCESS, (HKEY *)&hKey );
RegSetValueEx( (HKEY)hKey, valueName, 0, valueType, valueSetting, valueSize );
RegCloseKey( (HKEY)hKey );
}
void CreateAssociation( ) {
char szPath[_MAX_PATH];
GetModuleFileName(GetModuleHandle( 0 ), szPath, _MAX_PATH);
// File associations begin with a listing of the default extension under HKEY_CLASSES_ROOT
// Time to create one.
CreateNewKey( ".ost", HKEY_CLASSES_ROOT );
// Now we tell the registry where to look for commands based on that app
SetKeyValue( ".ost", "", (BYTE *)("ost_auto_file"), REG_SZ, strlen( "ost_auto_file" ) + 1 );
// Create the MyApp.Document directory, which is where we will put all commands for the file type
CreateNewKey( "ost_auto_file\\shell\\open\\command", HKEY_CLASSES_ROOT );
CreateNewKey( "ost_auto_file\\shell\\edit\\command", HKEY_CLASSES_ROOT );
// Set its default item to "MyApp Document", which will be displayed in Windows explorer
SetKeyValue( "ost_auto_file", "", (BYTE *)("OpenScript File"), REG_SZ, strlen( "OpenScript File" ) + 1 );
// Append '%1' to ensure that the file is passed as an argument to the program
strcat( szPath, " %1" );
SetKeyValue( "ost_auto_file\\shell\\open\\command", "", (BYTE *)szPath, REG_SZ, strlen( szPath ) + 1 );
SetKeyValue( "ost_auto_file\\shell\\edit\\command", "", (BYTE *)("notepad.exe %1"), REG_SZ, strlen( szPath ) + 1 );
// Done!
}