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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#define WIN32_EXTRALEAN
#include <windows.h>
#include <shlobj.h>
#include <winver.h>
#include <crtdbg.h>
#include <string.h>
#include <stdio.h>
extern "C" WINSHELLAPI void WINAPI SHFree( LPVOID);
template<class T> class Smart {
public:
T *pT;
Smart() : pT(NULL) {}
Smart(int cb) : pT(new T[cb]) {}
Smart(T* p) : pT(p) {}
~Smart() { if(pT)delete pT; }
Smart& operator=(T* p) { if(pT)delete pT; pT=p; return *this; }
operator T* () { return pT; }
// T& operator[](int ndx) { return pT[ndx]; }
T* Detach() { T* rv = pT; pT=NULL; return rv; }
};
typedef Smart<char> STRING;
#define APPEND_SLASH(str) if((str)[strlen(str)-1]!='\\')strcat(str,"\\")
HINSTANCE theInstance;
LPSTR strFETCH_REG_KEY(HKEY hRoot,LPCSTR subKey,LPCSTR val)
{
HKEY hkey;
if(RegOpenKeyEx(hRoot,subKey,0,KEY_QUERY_VALUE,&hkey)!=ERROR_SUCCESS)
return NULL;
DWORD kType,cb=0;
STRING rv;
if(RegQueryValueEx(hkey,val,NULL,&kType,NULL,&cb)==ERROR_SUCCESS && kType==REG_SZ){
rv= new char[cb];
_ASSERT(rv!=NULL);
if(RegQueryValueEx(hkey,val,NULL,&kType,(LPBYTE)(LPSTR)rv,&cb)!=ERROR_SUCCESS)
rv=NULL;
}
RegCloseKey(hkey);
return rv.Detach();
}
BOOL strSET_REG_KEY(HKEY hRoot,LPCSTR subKey,LPCSTR valName,LPCSTR val)
{
HKEY hkey;
DWORD dw;
if(RegCreateKeyEx(hRoot,subKey,0,REG_NONE,REG_OPTION_NON_VOLATILE,KEY_READ|KEY_WRITE,NULL,&hkey,&dw)!=ERROR_SUCCESS)
return FALSE;
BOOL rv = (RegSetValueEx(hkey,valName,0,REG_SZ,(LPBYTE)val,strlen(val)+1)==ERROR_SUCCESS);
RegCloseKey(hkey);
return rv;
}
void MAKE_PATH(LPCSTR path)
{
STRING tmp(strlen(path)+1);
LPCSTR t0=path;
LPSTR t1=tmp;
while(*t0){
if((*t0)=='\\'){
*t1=0;
CreateDirectory(tmp,NULL);
}
*(t1++)=*(t0++);
}
*t1=0;
CreateDirectory(tmp,NULL);
}
BOOL ADDMENU(LPCSTR menu,LPCSTR item,LPCSTR path,LPCSTR program)
{
STRING stm = strFETCH_REG_KEY(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders","Programs");
if(!stm)
return FALSE;
int pil = 1+strlen(path)+1+strlen(program)+1+1;
STRING pi(pil);
strcpy(pi,path);
APPEND_SLASH(pi);
strcat(pi,program);
int ipl = strlen(stm)+1+strlen(menu)+1+strlen(item)+4+1;
STRING ip(ipl);
memmove(ip,stm,strlen(stm)+1);
APPEND_SLASH(ip);
strcat(ip,menu);
MAKE_PATH(ip);
APPEND_SLASH(ip);
strcat(ip,item);
strcat(ip,".lnk");
IShellLink* sl = NULL;
IPersistFile* pf = NULL;
BOOL rv = FALSE;
do{
HRESULT hrv = CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(LPVOID*)&sl);
if(!SUCCEEDED(hrv))
break;
sl->SetDescription(item);
sl->SetPath(pi);
hrv = sl->QueryInterface(IID_IPersistFile,(LPVOID*)&pf);
if(!SUCCEEDED(hrv))
break;
WORD wsz[MAX_PATH];
MultiByteToWideChar(CP_ACP,0,ip,-1,wsz,MAX_PATH);
hrv = pf->Save(wsz,TRUE);
if(SUCCEEDED(hrv))
rv=TRUE;
}while(FALSE);
if(pf)
pf->Release();
if(sl)
sl->Release();
return rv;
}
FILE* CREATE_INF_FILE(LPCSTR path,LPCSTR file)
{
STRING fn(strlen(path)+1+strlen(file)+1);
strcpy(fn,path);
APPEND_SLASH(fn);
strcat(fn,file);
return fopen(fn,"wt");
}
BOOL INSTALLFILE(LPCSTR res,LPCSTR path,LPCSTR file)
{
STRING temp(MAX_PATH);
if(!GetTempPath(MAX_PATH,temp)) return FALSE;
STRING tf(MAX_PATH);
if(!GetTempFileName(temp,"KGI",0,tf)) return FALSE;
HRSRC hrsrc = FindResource(NULL,res,MAKEINTRESOURCE(RT_RCDATA));
if(!hrsrc) return FALSE;
DWORD sor = SizeofResource(NULL,hrsrc);
if(!sor) return FALSE;
HGLOBAL hglobal = LoadResource(NULL,hrsrc);
if(!hglobal) return FALSE;
LPVOID lpv = LockResource(hglobal);
if(!lpv) return FALSE;
HANDLE hf = CreateFile(tf,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_TEMPORARY,NULL);
if(!hf) return FALSE;
DWORD written = 0;
if(!WriteFile(hf,lpv,sor,&written,NULL) || written!=sor){
CloseHandle(hf);
return FALSE;
}
CloseHandle(hf);
STRING toKill(strlen(tf)+1);
strcpy(toKill,tf);
for(int tmp=strlen(tf)-1;tmp>0 && ((tf[tmp])!='\\');tmp--);
if(tf[tmp]=='\\')
tf[tmp++]=0;
STRING nothing(_MAX_PATH);
UINT nothingLength=_MAX_PATH;
if(VerInstallFile(0,&tf[tmp],(LPSTR)file,tf,(LPSTR)path,NULL,nothing,¬hingLength)){
DeleteFile(toKill);
return FALSE;
}
DeleteFile(toKill);
return TRUE;
}
LPCSTR pdTitle, pdPrompt;
char pdPath[_MAX_PATH];
BOOL CALLBACK pathDlgProc(HWND hwnd,UINT uMsg,WPARAM wP,LPARAM lP)
{
switch(uMsg){
case WM_INITDIALOG:
SetWindowText(hwnd,pdTitle);
SetDlgItemText(hwnd,IDC_PROMPT,pdPrompt);
SetDlgItemText(hwnd,IDC_PATH,pdPath);
SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
return 1;
case WM_COMMAND:
switch(LOWORD(wP)){ // ID
case IDC_BROWSE:
switch(HIWORD(wP)){
case BN_CLICKED:
{
BROWSEINFO bi;
memset(&bi,0,sizeof(bi));
bi.hwndOwner=hwnd;
bi.pszDisplayName=pdPath;
bi.lpszTitle="Select Folder..";
bi.ulFlags=BIF_RETURNONLYFSDIRS;
LPITEMIDLIST lpidl=SHBrowseForFolder(&bi);
if(lpidl){
SHGetPathFromIDList(lpidl,pdPath);
SHFree(lpidl);
SetDlgItemText(hwnd,IDC_PATH,pdPath);
}
}
return 1;
}
break;
case IDOK:
switch(HIWORD(wP)){
case BN_CLICKED:
if(GetDlgItemText(hwnd,IDC_PATH,pdPath,sizeof(pdPath)))
EndDialog(hwnd,IDOK);
else
// *** Error message
EndDialog(hwnd,IDCANCEL);
return 1;
}
break;
case IDCANCEL:
switch(HIWORD(wP)){
case BN_CLICKED:
EndDialog(hwnd,IDCANCEL);
return 1;
}
break;
};
break;
}
return 0;
}
LPSTR REQUESTPATH(LPCSTR title,LPCSTR prompt,LPCSTR defPath)
{
pdTitle=title;pdPrompt=prompt;
strcpy(pdPath,defPath);
if(DialogBox(NULL,MAKEINTRESOURCE(IDD_PATH),NULL/*Parent*/,(DLGPROC)&pathDlgProc)!=IDOK)
return NULL;
STRING rv(strlen(pdPath)+1);
strcpy(rv,pdPath);
return rv.Detach();
}
HKEY uninstallKey(LPCSTR regKey) {
STRING rk(100+strlen(regKey)+1);
sprintf(rk,"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%s",regKey);
HKEY rv = NULL;
DWORD dw;
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE,rk,0,REG_NONE,REG_OPTION_NON_VOLATILE,KEY_READ|KEY_WRITE,NULL,&rv,&dw)!=ERROR_SUCCESS)
return NULL;
return rv;
}
BOOL REG_UNINSTALL_COMMAND(LPCSTR regKey,LPCSTR dName,LPCSTR iPath,LPCSTR iFile,LPCSTR iSection)
{
HKEY hKey = uninstallKey(regKey);
if(!hKey)
return FALSE;
BOOL rv=FALSE;
do{
if(RegSetValueEx(hKey,"DisplayName",0,REG_SZ,(LPBYTE)dName,strlen(dName)+1)!=ERROR_SUCCESS)
break;
STRING us(50+strlen(iPath)+1+strlen(iFile)+strlen(iSection)+1);
strcpy(us,"RunDll32 setupapi.dll,InstallHinfSection ");
strcat(us,iSection);
strcat(us," 132 ");
strcat(us,iPath);
APPEND_SLASH(us);
strcat(us,iFile);
if(RegSetValueEx(hKey,"UninstallString",0,REG_SZ,(LPBYTE)(LPCSTR)us,strlen(us)+1)!=ERROR_SUCCESS)
break;
rv=TRUE;
}while(FALSE);
RegCloseKey(hKey);
return rv;
}
BOOL REG_UNINSTALL_ICON(LPCSTR regKey,LPCSTR path,LPCSTR file,int n) {
HKEY hKey = uninstallKey(regKey);
if(!hKey)
return FALSE;
STRING uis(strlen(path)+1+strlen(file)+7);
strcpy(uis,path);
APPEND_SLASH(uis);
strcat(uis,file);
char tmp[8];
sprintf(tmp,";%d",n);
strcat(uis,tmp);
BOOL rv = TRUE;
if(RegSetValueEx(hKey,"DisplayIcon",0,REG_SZ,(LPBYTE)(LPCSTR)uis,strlen(uis)+1)!=ERROR_SUCCESS)
rv = FALSE;
RegCloseKey(hKey);
return rv;
}
BOOL REG_UNINSTALL_COMMENT(LPCSTR regKey,LPCSTR comment) {
HKEY hKey = uninstallKey(regKey);
if(!hKey)
return FALSE;
BOOL rv = TRUE;
if(RegSetValueEx(hKey,"lComment",0,REG_SZ,(LPBYTE)comment,strlen(comment)+1)!=ERROR_SUCCESS)
rv = FALSE;
RegCloseKey(hKey);
return rv;
}
BOOL REG_UNINSTALL_VERSION(LPCSTR regKey,LPCSTR version) {
HKEY hKey = uninstallKey(regKey);
if(!hKey)
return FALSE;
BOOL rv = TRUE;
if(RegSetValueEx(hKey,"DisplayVersion",0,REG_SZ,(LPBYTE)version,strlen(version)+1)!=ERROR_SUCCESS)
rv = FALSE;
RegCloseKey(hKey);
return rv;
}
BOOL REG_UNINSTALL_LOCATION(LPCSTR regKey,LPCSTR location) {
HKEY hKey = uninstallKey(regKey);
if(!hKey)
return FALSE;
BOOL rv = TRUE;
if(RegSetValueEx(hKey,"InstallLocation",0,REG_SZ,(LPBYTE)location,strlen(location)+1)!=ERROR_SUCCESS)
rv = FALSE;
RegCloseKey(hKey);
return rv;
}
BOOL REG_UNINSTALL_PUBLISHER(LPCSTR regKey,LPCSTR publisher) {
HKEY hKey = uninstallKey(regKey);
if(!hKey)
return FALSE;
BOOL rv = TRUE;
if(RegSetValueEx(hKey,"Publisher",0,REG_SZ,(LPBYTE)publisher,strlen(publisher)+1)!=ERROR_SUCCESS)
rv = FALSE;
RegCloseKey(hKey);
return rv;
}
BOOL REG_UNINSTALL_URLS(LPCSTR regKey,LPCSTR about,LPCSTR update) {
HKEY hKey = uninstallKey(regKey);
if(!hKey)
return FALSE;
BOOL rv = TRUE;
if(RegSetValueEx(hKey,"URLInfoAbout",0,REG_SZ,(LPBYTE)about,strlen(about)+1)!=ERROR_SUCCESS)
rv = FALSE;
if(RegSetValueEx(hKey,"URLUpdateInfo",0,REG_SZ,(LPBYTE)update,strlen(update)+1)!=ERROR_SUCCESS)
rv = FALSE;
RegCloseKey(hKey);
return rv;
}
#define INF_FILE_HEADER(i) fprintf(i,"[Version]\nSignature=\"$CHICAGO$\"\n\n")
#define INF_FILE_SECTION(i,s) fprintf(i,"\n[%s]\n",s)
#define INF_UNINSTALL_REG(i,p) fprintf(i,"HKLM,Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%s\nHKLM,Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%s,DisplayName\nHKLM,Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%s,UninstallString\n",p,p,p)
#define INF_MENU_GROUP(i,n,m) fprintf(i,"setup.ini, progman.groups,,\"group%d=%s\"\n",n,m)
#define INF_MENU_ITEM(i,n,m) fprintf(i,"setup.ini, group%d,, \"\"\"%s\"\"\"\n",n,m);
#define INF_REMOVE_ROOT(i,g,r) fprintf(i,"HKLM,Software\\Microsoft\\Windows\\CurrentVersion\\DeleteFiles\\%s,,,\"%s\"\n",g,r)
#define INF_REMOVE_FILE(i,g,f) fprintf(i,"HKLM,Software\\Microsoft\\Windows\\CurrentVersion\\DeleteFiles\\%s,%s,,\"%s\"\n",g,f,f)
#define INF_REMOVE_HELP_FILE(i,g,f) {INF_REMOVE_FILE(i,g,f".hlp");INF_REMOVE_FILE(i,g,f".cnt");INF_REMOVE_FILE(i,g,f".GID");INF_REMOVE_FILE(i,g,f".FTS");}
LPSTR GET_SHORT_PATH(LPCSTR path)
{
char tmp;
DWORD len = GetShortPathName(path,&tmp,1);
if(!len)
return NULL;
STRING rv(len+1);
if(!GetShortPathName(path,rv,len+1))
return NULL;
return rv.Detach();
}
BOOL Install(void);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR,int)
{
theInstance=hInstance;
CoInitialize(NULL);
Install();
CoUninitialize();
return 0;
}
|