-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegHelper.cpp
More file actions
242 lines (209 loc) · 8.2 KB
/
Copy pathRegHelper.cpp
File metadata and controls
242 lines (209 loc) · 8.2 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "RegHelper.h"
BOOL RegisterFileRelation(char *strExt, char *strAppName, char * strAppFileName, char * strProgid, char *strAppKey, char *strDefaultIcon, char *strDescribe)
{
char strTemp[_MAX_PATH];
HKEY hKey;
BOOL bSuccess = FALSE;
int sysVersion = GetSystemVersion();
RegCreateKeyA(HKEY_CLASSES_ROOT,strExt,&hKey);
RegSetValueExA(hKey,"",NULL,REG_SZ,(BYTE*)strAppKey,strlen(strAppKey)+1);
RegCloseKey(hKey);
RegCreateKeyA(HKEY_CLASSES_ROOT,strAppKey,&hKey);
RegSetValueExA(hKey,"",NULL,REG_SZ,(BYTE*)strDescribe,strlen(strDescribe)+1);
RegCloseKey(hKey);
sprintf_s(strTemp,"%s\\DefaultIcon",strAppKey);
RegCreateKeyA(HKEY_CLASSES_ROOT,strTemp,&hKey);
RegSetValueExA(hKey,"",NULL,REG_SZ,(BYTE*)strDefaultIcon,strlen(strDefaultIcon)+1);
RegCloseKey(hKey);
sprintf_s(strTemp,"%s\\Shell",strAppKey);
RegCreateKeyA(HKEY_CLASSES_ROOT,strTemp,&hKey);
RegSetValueExA(hKey,"",NULL,REG_SZ,(BYTE*)"Open",strlen("Open")+1);
RegCloseKey(hKey);
sprintf_s(strTemp,"%s\\Shell\\Open\\Command",strAppKey);
RegCreateKeyA(HKEY_CLASSES_ROOT,strTemp,&hKey);
sprintf_s(strTemp,"%s \"%%1\"",strAppName);
RegSetValueExA(hKey,"",NULL,REG_SZ,(BYTE*)strTemp,strlen(strTemp)+1);
RegCloseKey(hKey);
sprintf_s(strTemp,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%s\\OpenWithList",strExt);
RegCreateKeyA(HKEY_CURRENT_USER, strTemp, &hKey);
RegSetValueExA(hKey, "MRUList", NULL, REG_SZ , (BYTE*)"", 0);
RegCloseKey(hKey);
if(sysVersion == SYS_VERSION_WIN_7)
{
sprintf_s(strTemp,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%s\\UserChoice",strExt);
RegCreateKeyA(HKEY_CURRENT_USER, strTemp, &hKey);
RegSetValueExA(hKey, "Progid", NULL, REG_SZ , (BYTE*)(strProgid), strlen(strProgid)+1);
char progidResult[_MAX_PATH];
DWORD dwSize = sizeof(progidResult);
RegQueryValueExA(hKey, "Progid", NULL, NULL, (LPBYTE)progidResult, &dwSize);
if(_stricmp(progidResult,strProgid) != 0)
{
if(IsUserAdmin())
{
if(GaintKeyPrivilege(hKey))
{
RegCloseKey(hKey);
RegCreateKeyA(HKEY_CURRENT_USER, strTemp, &hKey);
RegSetValueExA(hKey, "Progid", NULL, REG_SZ , (BYTE*)(strProgid), strlen(strProgid)+1);
RegCloseKey(hKey);
bSuccess = true;
}
else
{
//Unknow ERROR occured when gaint UserChoice key privilege!
}
}
else
{
//Current user dosen't have admin privilege to edit special reg key value!
}
}
}
else if(sysVersion == SYS_VERSION_WIN_8 )
{
sprintf_s(strTemp,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%s\\UserChoice",strExt);
RegCreateKeyA(HKEY_CURRENT_USER, strTemp, &hKey);
RegSetValueExA(hKey, "Progid", NULL, REG_SZ , (BYTE*)(strAppKey), strlen(strAppKey)+1);
char progidResult[_MAX_PATH];
DWORD dwSize = sizeof(progidResult);
RegQueryValueExA(hKey, "Progid", NULL, NULL, (LPBYTE)progidResult, &dwSize);
if(_stricmp(progidResult,strAppKey) != 0)
{
if(IsUserAdmin())
{
if(GaintKeyPrivilege(hKey))
{
RegCloseKey(hKey);
RegCreateKeyA(HKEY_CURRENT_USER, strTemp, &hKey);
RegSetValueExA(hKey, "Progid", NULL, REG_SZ , (BYTE*)(strAppKey), strlen(strAppKey)+1);
RegCloseKey(hKey);
bSuccess = true;
}
else
{
//Unknow ERROR occured when gaint UserChoice key privilege!
}
}
else
{
//Current user dosen't have admin privilege to edit special reg key value!
}
}
RegCloseKey(hKey);
RegCreateKeyA(HKEY_CURRENT_USER, strTemp, &hKey);
RegSetValueExA(hKey, "Hash", NULL, REG_SZ , (BYTE*)(HASH), strlen(HASH)+1);
RegCloseKey(hKey);
}
if(sysVersion == SYS_VERSION_WIN_XP)
{
sprintf_s(strTemp,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%s",strExt);
RegCreateKeyA(HKEY_CURRENT_USER, strTemp, &hKey);
RegSetValueExA(hKey, "Application", NULL, REG_SZ , (BYTE*)(strAppFileName), strlen(strAppFileName)+1);
RegCloseKey(hKey);
bSuccess = true;
}
if(sysVersion == SYS_VERSION_WIN_XP || sysVersion == SYS_VERSION_WIN_8 )
{
sprintf_s(strTemp,"Applications\\%s\\shell\\open\\command",strAppFileName);
RegCreateKeyA(HKEY_CLASSES_ROOT, strTemp, &hKey);
sprintf_s(strTemp,"%s \"%%1\"",strAppName);
RegSetValueExA(hKey,"",NULL,REG_SZ,(BYTE*)strTemp,strlen(strTemp)+1);
RegCloseKey(hKey);
}
if(sysVersion == SYS_VERSION_OTHER)
{
//un support OS for register file relation!
}
::SHChangeNotify(SHCNE_ASSOCCHANGED,SHCNF_IDLIST|SHCNF_FLUSH,0,0);
return bSuccess;
}
BOOL IsUserAdmin(VOID)
{
BOOL b;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup;
b = AllocateAndInitializeSid(
&NtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&AdministratorsGroup);
if(b)
{
if (!CheckTokenMembership( NULL, AdministratorsGroup, &b))
{
b = FALSE;
}
FreeSid(AdministratorsGroup);
}
return(b);
}
int GetSystemVersion()
{
SYSTEM_INFO info;
GetSystemInfo(&info);
OSVERSIONINFOEX os;
os.dwOSVersionInfoSize=sizeof(OSVERSIONINFOEX);
if(GetVersionEx((OSVERSIONINFO *)&os))
{
switch(os.dwMajorVersion)
{
//MajorVersion
case 5:
switch(os.dwMinorVersion)
{
//MinorVersion
case 1:
return SYS_VERSION_WIN_XP;
case 2:
if(os.wProductType==VER_NT_WORKSTATION &&
info.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
return SYS_VERSION_WIN_XP;
}
break;
case 6:
switch(os.dwMinorVersion)
{
case 1:
if(os.wProductType==VER_NT_WORKSTATION)
return SYS_VERSION_WIN_7;
case 2:
return SYS_VERSION_WIN_8;
}
break;
}
}
return SYS_VERSION_OTHER;
}
BOOL GaintKeyPrivilege( HKEY & hKey )
{
SID_IDENTIFIER_AUTHORITY SecIA = SECURITY_NT_AUTHORITY;
PSID pSid = NULL;
if (FALSE == ::AllocateAndInitializeSid(&SecIA, 1,
SECURITY_INTERACTIVE_RID, 0, 0, 0, 0, 0, 0, 0, &pSid))
{
return FALSE;
}
DWORD dwAclSize = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) +
::GetLengthSid(pSid) - sizeof(DWORD);
PACL pDacl = (PACL) new BYTE[dwAclSize];
if (TRUE == ::InitializeAcl(pDacl, dwAclSize, ACL_REVISION))
{
if (TRUE == ::AddAccessAllowedAce(pDacl, ACL_REVISION, KEY_ALL_ACCESS,
pSid))
{
SECURITY_DESCRIPTOR SecDesc;
if (TRUE == ::InitializeSecurityDescriptor(&SecDesc,
SECURITY_DESCRIPTOR_REVISION))
{
if (TRUE == ::SetSecurityDescriptorDacl(&SecDesc, TRUE, pDacl, FALSE))
{
return (ERROR_SUCCESS == ::RegSetKeySecurity(hKey,
(SECURITY_INFORMATION)DACL_SECURITY_INFORMATION, &SecDesc));
}
}
}
}
return false;
}