Привязка типа файла к программе

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
#include <iostream>
#include <Windows.h>
using namespace std;
bool AssociateFileType( const string & extension, // расширение файла
const string & section, // секция в реестре
const string & path, // путь к программе
const string & info ) // описание
{
HKEY hk;
DWORD dwDisp;
// [HKEY_CLASSES_ROOT\.wld]
// @="WLD-File"
if ( RegCreateKeyEx(HKEY_CLASSES_ROOT, extension.c_str(), 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &hk, &dwDisp) != ERROR_SUCCESS )
{
if ( RegOpenKeyEx(HKEY_CLASSES_ROOT, extension.c_str(), 0, KEY_QUERY_VALUE, &hk) != ERROR_SUCCESS )
return false;
}
if ( RegSetValueEx(hk, "", 0, REG_SZ,
(LPBYTE)section.c_str(), (DWORD)(section.length()+1)*sizeof(section[0])) != ERROR_SUCCESS )
{
return false;
}
RegCloseKey(hk);
// [HKEY_CLASSES_ROOT\WLD-File]
// @="TAU Engine World File"
if ( RegCreateKeyEx(HKEY_CLASSES_ROOT, section.c_str(), 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &hk, &dwDisp) != ERROR_SUCCESS )
{
if ( RegOpenKeyEx(HKEY_CLASSES_ROOT, section.c_str(), 0, KEY_QUERY_VALUE, &hk) != ERROR_SUCCESS )
return false;
}
if ( RegSetValueEx(hk, "", 0, REG_SZ,
(LPBYTE)info.c_str(), (DWORD)(info.length()+1)*sizeof(info[0])) != ERROR_SUCCESS )
{
return false;
}
RegCloseKey(hk);
// [HKEY_CLASSES_ROOT\WLD-File\shell]
// [HKEY_CLASSES_ROOT\WLD-File\shell\Open]
// [HKEY_CLASSES_ROOT\WLD-File\shell\Open\command]
// @="E:\\[development]\\Programs\\FreedomWorld\\TAU Crafter\\Crafter.exe \"%1\""
string s = section;
s += "\\shell\\Open\\command";
string mod_path = path;
mod_path += " \"%1\"";
if ( RegCreateKeyEx(HKEY_CLASSES_ROOT, s.c_str(), 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &hk, &dwDisp) != ERROR_SUCCESS )
{
if ( RegOpenKeyEx(HKEY_CLASSES_ROOT, s.c_str(), 0, KEY_QUERY_VALUE, &hk) != ERROR_SUCCESS )
return false;
}
if ( RegSetValueEx(hk, "", 0, REG_SZ,
(LPBYTE)mod_path.c_str(), (DWORD)( mod_path.length()+1)*sizeof(mod_path[0])) != ERROR_SUCCESS )
{
return false;
}
RegCloseKey(hk);
// [HKEY_CLASSES_ROOT\WLD-File\DefaultIcon]
// @="E:\\[development]\\Programs\\FreedomWorld\\TAU Crafter\\Crafter.exe,0"
s = section;
s += "\\DefaultIcon";
mod_path = path;
mod_path += ", 0";
if ( RegCreateKeyEx(HKEY_CLASSES_ROOT, s.c_str(), 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &hk, &dwDisp) != ERROR_SUCCESS )
{
if ( RegOpenKeyEx(HKEY_CLASSES_ROOT, s.c_str(), 0, KEY_QUERY_VALUE, &hk) != ERROR_SUCCESS )
return false;
}
if ( RegSetValueEx(hk, "", 0, REG_SZ,
(LPBYTE)mod_path.c_str(), (DWORD)(mod_path.length()+1)*sizeof(mod_path[0])) != ERROR_SUCCESS )
{
return false;
}
RegCloseKey(hk);
return true;
}
int main()
{
cout << AssociateFileType(".wld",
"WLD-File",
"E:\\[development]\\Programs\\FreedomWorld\\TAU Crafter\\Crafter.exe",
"TAU Engine World File")
<< endl;
system("PAUSE");
return 0;
}