3 Part of the rfx installer (Main program).
5 Copyright (c) 2004-2008 Matthias Kramm <kramm@quiss.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
28 #include "installer.h"
34 static int config_forAllUsers = 0;
35 static int config_createLinks = 0;
36 static int config_createStartmenu = 1;
37 static int config_createDesktop = 1;
38 static int config_deleteextra = 1;
40 static char path_startmenu[MAX_PATH] = "\0";
41 static char path_desktop[MAX_PATH] = "\0";
42 static char path_programfiles[MAX_PATH] = "\0";
44 static char pathBuf[MAX_PATH];
45 static int do_abort = 0;
47 static char* pdf2swf_path;
49 static char registry_path[1024];
51 static char elevated = 0;
53 static char*install_path = "c:\\swftools\\";
54 #define SOFTWARE_DOMAIN "quiss.org"
55 #define SOFTWARE_NAME "SWFTools"
56 #define INSTALLER_NAME "SWFTools Installer"
58 static HBITMAP logo = 0;
62 #define USER_SETMESSAGE 0x7f01
66 extern int crndata_len;
67 extern int crn_decompressed_size;
68 extern char*license_text;
70 #include "background.c"
73 typedef struct _filelist
76 struct _filelist*next;
80 static filelist_t* registerFile(filelist_t*next, const char*filename, char type)
82 filelist_t*file = malloc(sizeof(filelist_t));
83 file->filename = strdup(filename);
89 static filelist_t* readFileList(char*filename)
91 FILE*fi = fopen(filename, "rb");
95 fseek(fi, 0, SEEK_END);
97 fseek(fi, 0, SEEK_SET);
98 char*data = malloc(len+1);
99 fread(data, len, 1, fi);
103 filelist_t*list = 0,*lpos=0;;
105 if(data[t]=='\r' || data[t]=='\n') {
107 if(strchr("DFI", line[0]) && line[1]==' ' && line[2]!=' ') {
108 filelist_t*f = malloc(sizeof(filelist_t));
110 f->filename=strdup(line+2);
119 // skip line- this usually only happens if somebody tampered
122 while(t<len && (data[t]=='\0' || data[t]=='\r' || data[t]=='\n'))
132 static void writeFileList(filelist_t*file, const char*filename)
134 FILE*fi = fopen(filename, "wb");
135 fprintf(fi, "[%s installed files]\r\n", SOFTWARE_NAME);
138 sprintf(buf, "Couldn't write file %s", filename);
139 MessageBox(0, buf, INSTALLER_NAME, MB_OK|MB_ICONERROR);
144 fprintf(fi, "%c %s\r\n", file->type, file->filename);
150 static filelist_t*installedFiles = 0;
152 static void addFile(const char*filename)
154 installedFiles = registerFile(installedFiles, filename, 'F');
156 static void addDir(const char*filename)
158 installedFiles = registerFile(installedFiles, filename, 'D');
161 static void handleTemplateFile(const char*filename)
163 FILE*fi = fopen(filename, "rb");
164 fseek(fi, 0, SEEK_END);
166 fseek(fi, 0, SEEK_SET);
167 char*file = malloc(len+1);
168 fread(file, len, 1, fi);
170 int l = strlen(install_path);
171 fi = fopen(filename, "wb");
175 pos = strstr(pos, "%%PATH%%");
181 fwrite(lastpos, pos-lastpos, 1, fi);
182 fwrite(install_path, l, 1, fi);
183 pos+=8; // length of "%%PATH%%"
186 fwrite(lastpos, pos-lastpos, 1, fi);
191 static int setRegistryEntry(char*key,char*value)
195 int ret1 = -1, ret2= -1;
196 ret1 = RegCreateKey(HKEY_CURRENT_USER, key, &hkey1);
197 if(config_forAllUsers) {
198 ret2 = RegCreateKey(HKEY_LOCAL_MACHINE, key, &hkey2);
202 fprintf(stderr, "registry: CreateKey %s failed\n", key);
206 installedFiles = registerFile(installedFiles, key, 'k');
209 installedFiles = registerFile(installedFiles, key, 'K');
213 ret1 = RegSetValue(hkey1, NULL, REG_SZ, value, strlen(value)+1);
215 ret2 = RegSetValue(hkey2, NULL, REG_SZ, value, strlen(value)+1);
217 fprintf(stderr, "registry: SetValue %s failed\n", key);
224 static char* getRegistryEntry(char*path)
232 rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_ALL_ACCESS/* KEY_READ*/, &key);
233 if (rc != ERROR_SUCCESS) {
234 fprintf(stderr, "RegOpenKeyEx failed\n");
237 rc = RegQueryValueEx(key, NULL, 0, 0, 0, (LPDWORD)&size) ;
238 if(rc != ERROR_SUCCESS) {
239 fprintf(stderr, "RegQueryValueEx(1) failed: %d\n", rc);
242 buf = (char*)malloc(size+1);
243 rc = RegQueryValueEx(key, NULL, 0, &type, (BYTE*)buf, (LPDWORD)&size);
244 if(rc != ERROR_SUCCESS) {
245 fprintf(stderr, "RegQueryValueEx(2) failed: %d\n", rc);
248 if(type == REG_SZ || type == REG_EXPAND_SZ) {
249 while(size && buf[size-1] == '\0')
254 } else if(type == REG_BINARY) {
259 static int has_full_access = 0;
260 static char hasFullAccess()
262 /* find out whether we can write keys in HKEY_LOCAL_MACHINE */
264 int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall", 0,
265 KEY_CREATE_SUB_KEY, &hKey);
274 void processMessages()
277 while(PeekMessage(&msg,NULL,0,0,0))
279 GetMessage(&msg, NULL, 0, 0);
280 TranslateMessage(&msg);
281 DispatchMessage(&msg);
285 int addRegistryEntries(char*install_dir)
288 ret = setRegistryEntry(registry_path, install_dir);
293 int CreateShortcut(char*path, char*description, char*filename, char*arguments, int iconindex, char*iconpath, char*workdir)
295 printf("Creating %s -> %s\n", filename, path);
296 WCHAR wszFilename[MAX_PATH];
297 IShellLink *ps1 = NULL;
298 IPersistFile *pPf = NULL;
300 hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (void*)&ps1);
301 if(FAILED(hr)) return 0;
302 hr = ps1->lpVtbl->QueryInterface(ps1, &IID_IPersistFile, (void **)&pPf);
303 if(FAILED(hr)) return 0;
304 hr = ps1->lpVtbl->SetPath(ps1, path);
305 if(FAILED(hr)) return 0;
306 hr = ps1->lpVtbl->SetDescription(ps1, description);
309 hr = ps1->lpVtbl->SetArguments(ps1, arguments);
310 if(FAILED(hr)) return 0;
313 hr = ps1->lpVtbl->SetIconLocation(ps1, iconpath, iconindex);
314 if (FAILED(hr)) return 0;
317 hr = ps1->lpVtbl->SetWorkingDirectory(ps1, workdir);
318 if (FAILED(hr)) return 0;
320 MultiByteToWideChar(CP_ACP, 0, filename, -1, wszFilename, MAX_PATH);
321 hr = pPf->lpVtbl->Save(pPf, wszFilename, TRUE);
325 pPf->lpVtbl->Release(pPf);
326 ps1->lpVtbl->Release(ps1);
331 static int CreateURL(const char*url, const char*path)
333 FILE*fi = fopen(path, "wb");
336 fprintf(fi, "[InternetShortcut]\r\n");
337 fprintf(fi, "URL=http://localhost:8081/\r\n");
344 BOOL CALLBACK PropertySheetFuncCommon(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, int buttons)
348 HWND dialog = GetParent(hwnd);
350 if(message == WM_INITDIALOG) {
352 SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)logo);
355 GetWindowRect(dialog, &rc);
356 int width = rc.right - rc.left;
357 int height = rc.bottom - rc.top;
358 MoveWindow(dialog, (GetSystemMetrics(SM_CXSCREEN) - width)/2, (GetSystemMetrics(SM_CYSCREEN) - height)/2, width, height, FALSE);
362 if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_SETACTIVE)) {
363 PropSheet_SetWizButtons(dialog, buttons);
370 BOOL CALLBACK PropertySheetFunc1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
371 if(message == WM_INITDIALOG) {
372 SetDlgItemText(hwnd, IDC_LICENSE, license_text);
374 return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_NEXT);
376 BOOL CALLBACK PropertySheetFunc2(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
377 if(message == WM_INITDIALOG) {
378 SetDlgItemText(hwnd, IDC_INSTALL_PATH, install_path);
380 SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, config_forAllUsers, 0);
381 SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, config_forAllUsers^1, 0);
383 if(message == WM_COMMAND) {
384 if((wParam&0xffff) == IDC_BROWSE) {
386 memset(&browse, 0, sizeof(browse));
387 browse.ulFlags = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_USENEWUI;// | BIF_RETURNONLYFSDIRS; //BIF_VALIDATE
388 browse.pszDisplayName = (CHAR*)malloc(MAX_PATH);
389 memset(browse.pszDisplayName, 0, MAX_PATH);
390 browse.lpszTitle = "Select installation directory";
391 browse.pidlRoot = SHBrowseForFolder(&browse);
392 if(browse.pszDisplayName) {
393 if(SHGetPathFromIDList(browse.pidlRoot, browse.pszDisplayName)) {
394 install_path = browse.pszDisplayName;
395 int l = strlen(install_path);
396 while(l && install_path[l-1]=='\\') {
401 SendDlgItemMessage(hwnd, IDC_INSTALL_PATH, WM_SETTEXT, 0, (LPARAM)install_path);
405 else if((wParam&0xffff) == IDC_ALLUSERS) {
406 config_forAllUsers = 1;
407 SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, config_forAllUsers, 0);
408 SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, config_forAllUsers^1, 0);
410 else if((wParam&0xffff) == IDC_CURRENTUSER) {
411 config_forAllUsers = 0;
412 SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, config_forAllUsers, 0);
413 SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, config_forAllUsers^1, 0);
415 else if((wParam&0xffff) == IDC_INSTALL_PATH) {
416 SendDlgItemMessage(hwnd, IDC_INSTALL_PATH, WM_GETTEXT, sizeof(pathBuf), (LPARAM)&(pathBuf[0]));
418 install_path = pathBuf;
419 int l = strlen(install_path);
420 while(l && install_path[l-1]=='\\') {
427 if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_SETACTIVE)) {
428 if(!elevated && !has_full_access && config_forAllUsers) {
429 OSVERSIONINFO winverinfo;
430 memset(&winverinfo, 0, sizeof(OSVERSIONINFO));
431 winverinfo.dwOSVersionInfoSize = sizeof(winverinfo);
432 if (GetVersionEx(&winverinfo) && winverinfo.dwMajorVersion >= 5) {
433 /* we're on Vista, were asked to install for all users, but don't have
434 priviledges to do so. Ask to spawn the process elevated. */
435 char exename[MAX_PATH];
436 GetModuleFileName(NULL, exename, sizeof(exename));
437 if((int)ShellExecute(0, "runas", exename, "elevated", NULL, SW_SHOWNORMAL)>32) {
438 /* that worked- the second process will do the work */
444 return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
447 static int progress_pos = 0;
449 void PropertyArchiveError(char*text)
452 int ret = MessageBox(0, text, "Error", MB_RETRYCANCEL|MB_ICONERROR);
453 if(ret==IDRETRY) continue;
458 void PropertyArchive_NewFile(char*filename)
463 void PropertyArchive_NewDirectory(char*filename)
469 static int lastlen = 0;
470 void PropertyArchiveStatus(int pos, int len)
473 SendDlgItemMessage(statuswnd, IDC_PROGRESS, PBM_SETRANGE, 0, (LPARAM)MAKELONG(0,len));
476 SendDlgItemMessage(statuswnd, IDC_PROGRESS, PBM_SETPOS, pos, 0);
480 void PropertyArchiveMessage(char*text)
482 if(text && text[0]=='[') {
485 SetDlgItemText(statuswnd, IDC_INFO, strdup(text));
489 void print_space(char*dest, char*msg, ULONGLONG size)
492 sprintf(dest, "%s%d Bytes", msg, size);
493 else if(size < 1048576l)
494 sprintf(dest, "%s%.2f Kb", msg, size/1024.0);
495 else if(size < 1073741824l)
496 sprintf(dest, "%s%.2f Mb", msg, size/1048576.0);
497 else if(size < 1099511627776ll)
498 sprintf(dest, "%s%.2f Gb", msg, size/1073741824.0);
500 sprintf(dest, "%s%.2f Tb", msg, size/1125899906842624.0);
503 BOOL CALLBACK PropertySheetFunc3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
504 HWND dialog = GetParent(hwnd);
505 if(message == WM_INITDIALOG) {
506 SetDlgItemText(hwnd, IDC_INFO, "Ready to install");
509 print_space(buf, "Space required: ", crn_decompressed_size);
510 SetDlgItemText(hwnd, IDC_SPACE1, buf);
511 ULARGE_INTEGER available,total,totalfree;
512 available.QuadPart=0;
514 totalfree.QuadPart=0;
515 if(GetDiskFreeSpaceEx(install_path, &available, &total, &totalfree)) {
516 print_space(buf, "Space available: ", available.QuadPart);
518 sprintf(buf, "Space available: [Error %d]", GetLastError());
519 if((GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_PATH_NOT_FOUND)
520 && install_path[0] && install_path[1]==':') {
521 /* installation directory does not yet exist */
522 char path[3]={'c',':',0};
523 path[0] = install_path[0];
524 if(GetDiskFreeSpaceEx(path, &available, &total, &totalfree)) {
525 print_space(buf, "Space available: ", available.QuadPart);
529 SetDlgItemText(hwnd, IDC_SPACE2, buf);
531 if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_WIZNEXT)) {
532 SetDlgItemText(hwnd, IDC_SPACE1, "");
533 SetDlgItemText(hwnd, IDC_SPACE2, "");
534 PropSheet_SetWizButtons(dialog, 0);
535 SendMessage(dialog, PSM_CANCELTOCLOSE, 0, 0); //makes wine display a warning
536 SetDlgItemText(hwnd, IDC_TITLE, "Installing files...");
539 status.status = PropertyArchiveStatus;
540 status.message = PropertyArchiveMessage;
541 status.error = PropertyArchiveError;
542 status.new_file = PropertyArchive_NewFile;
543 status.new_directory = PropertyArchive_NewDirectory;
544 int success = unpack_archive(crndata, crndata_len, install_path, &status);
545 memset(&status, 0, sizeof(status_t));
547 MessageBox(0, "Couldn't extract all installation files", INSTALLER_NAME, MB_OK|MB_ICONERROR);
553 return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
556 static HRESULT (WINAPI *f_SHGetSpecialFolderPath)(HWND hwnd, LPTSTR lpszPath, int nFolder, BOOL fCreate);
558 BOOL CALLBACK PropertySheetFunc4(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
559 if(message == WM_INITDIALOG) {
560 pdf2swf_path = concatPaths(install_path, "pdf2swf_gui.exe");
561 FILE*fi = fopen(pdf2swf_path, "rb");
563 printf("a GUI program exists, creating desktop/startmenu links\n");
564 config_createLinks = 1;
567 config_createLinks = 0;
568 config_createStartmenu = 0;
569 config_createDesktop = 0;
571 if(!config_createLinks) {
572 SendDlgItemMessage(hwnd, IDC_STARTMENU, BN_DISABLE, 0, 0);
573 SendDlgItemMessage(hwnd, IDC_DESKTOP, BN_DISABLE, 0, 0);
576 SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_SETCHECK, config_createStartmenu, 0);
577 SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_SETCHECK, config_createDesktop, 0);
579 if(message == WM_COMMAND) {
580 if((wParam&0xffff) == IDC_STARTMENU) {
581 config_createStartmenu = SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_GETCHECK, 0, 0);
582 if(config_createLinks) {
583 config_createStartmenu^=1;
585 SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_SETCHECK, config_createStartmenu, 0);
588 if((wParam&0xffff) == IDC_DESKTOP) {
589 config_createDesktop = SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_GETCHECK, 0, 0);
590 if(config_createLinks) {
591 config_createDesktop^=1;
593 SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_SETCHECK, config_createDesktop, 0);
598 if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_WIZFINISH)) {
599 if(!addRegistryEntries(install_path)) {
600 MessageBox(0, "Couldn't create Registry Entries", INSTALLER_NAME, MB_OK|MB_ICONERROR);
604 char mypath[MAX_PATH];
605 path_startmenu[0] = 0;
607 if(config_forAllUsers) {
608 f_SHGetSpecialFolderPath(NULL, path_desktop, CSIDL_COMMON_DESKTOPDIRECTORY, 0);
609 f_SHGetSpecialFolderPath(NULL, path_startmenu, CSIDL_COMMON_PROGRAMS, 0);
611 /* get local program/desktop directory- this is both for forAllUsers=0 as well
612 as a fallback if the above didn't return any paths */
613 if(!path_startmenu[0]) {
614 f_SHGetSpecialFolderPath(NULL, path_startmenu, CSIDL_PROGRAMS, 0);
616 if(!path_desktop[0]) {
617 f_SHGetSpecialFolderPath(NULL, path_desktop, CSIDL_DESKTOPDIRECTORY, 0);
620 char*uninstall_path = concatPaths(install_path, "uninstall.exe");
622 if(config_createLinks) {
623 if(config_createDesktop && path_desktop[0]) {
624 char* linkName = concatPaths(path_desktop, "pdf2swf.lnk");
625 printf("Creating desktop link %s -> %s\n", linkName, pdf2swf_path);
626 if(!CreateShortcut(pdf2swf_path, "pdf2swf", linkName, 0, 0, 0, install_path)) {
627 MessageBox(0, "Couldn't create desktop shortcut", INSTALLER_NAME, MB_OK);
631 if(config_createStartmenu && path_startmenu[0]) {
632 char* group = concatPaths(path_startmenu, "pdf2swf");
633 CreateDirectory(group, 0);
635 char* linkName = concatPaths(group, "pdf2swf.lnk");
636 if(!CreateShortcut(pdf2swf_path, "pdf2swf", concatPaths(group, "pdf2swf.lnk"), 0, 0, 0, install_path) ||
637 !CreateShortcut(uninstall_path, "uninstall", concatPaths(group, "uninstall.lnk"), 0, 0, 0, install_path)) {
638 MessageBox(0, "Couldn't create start menu entry", INSTALLER_NAME, MB_OK);
643 printf("not creating desktop/startmenu links\n");
646 char*uninstall_ini = concatPaths(install_path, "uninstall.ini");
647 addFile(uninstall_ini);
648 writeFileList(installedFiles, uninstall_ini);
650 return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_FINISH);
656 void findfiles(char*path, int*pos, char*data, int len, char del)
658 WIN32_FIND_DATA findFileData;
659 HANDLE hFind = FindFirstFile(concatPaths(path, "*"), &findFileData);
660 if(hFind == INVALID_HANDLE_VALUE)
663 if(findFileData.cFileName[0] == '.' &&
664 (findFileData.cFileName[0] == '.' || findFileData.cFileName == '\0'))
666 char*f = concatPaths(path, findFileData.cFileName);
667 if(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
668 findfiles(f, pos, data, len, del);
669 /* always try to remove directories- if they are empty, this
670 will work, and they won't prevent superdirectory deletion later */
675 /* don't list the uninstaller as file- it's going to be removed *after*
676 everything else is done */
677 char*uninstaller="uninstall.exe";
678 int ll = strlen(uninstaller);
680 if(!strcasecmp(&f[l-ll],uninstaller)) {
687 memcpy(&data[*pos], f, l);(*pos)+=l;
688 data[(*pos)++] = '\r';
689 data[(*pos)++] = '\n';
699 } while(FindNextFile(hFind, &findFileData));
703 static char*extrafiles = 0;
705 BOOL CALLBACK PropertySheetFunc5(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
706 HWND dialog = GetParent(hwnd);
707 if(message == WM_INITDIALOG) {
708 SetDlgItemText(hwnd, IDC_INFO, "Ready to uninstall");
710 if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_WIZNEXT)) {
712 filelist_t* list = readFileList("uninstall.ini");
714 list = readFileList(concatPaths(install_path, "uninstall.ini"));
716 //Don't abort. If there's still something there, it'll be catched by the "extra files"
717 //functionality later
718 //MessageBox(0, "Couldn't determine installed files list- did you run uninstall twice?", INSTALLER_NAME, MB_OK);
722 filelist_t* l = list;
724 while(l) {num++;l=l->next;}
726 PropSheet_SetWizButtons(dialog, 0);
727 SendMessage(dialog, PSM_CANCELTOCLOSE, 0, 0);
728 SetDlgItemText(hwnd, IDC_TITLE, "Uninstalling files...");
729 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, (LPARAM)MAKELONG(0,num));
733 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, num, 0);
735 DeleteFile(l->filename);
736 else if(l->type=='D')
737 RemoveDirectory(l->filename);
738 else if(l->type=='I')
739 /* skip- we will remove ourselves later */;
744 findfiles(install_path, &len, 0, 0, 0);
746 extrafiles = malloc(len);
748 findfiles(install_path, &pos, extrafiles, len, 0);
750 PropSheet_RemovePage(dialog, 1, 0);
754 return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
757 BOOL CALLBACK PropertySheetFunc6(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
758 if(message == WM_INITDIALOG) {
759 SendDlgItemMessage(hwnd, IDC_DELETEEXTRA, BM_SETCHECK, config_deleteextra, 0);
761 SetDlgItemText(hwnd, IDC_FILELIST, extrafiles);
764 if(message == WM_COMMAND) {
765 if((wParam&0xffff) == IDC_DELETEEXTRA) {
766 config_deleteextra = SendDlgItemMessage(hwnd, IDC_DELETEEXTRA, BM_GETCHECK, 0, 0);
767 config_deleteextra ^=1;
768 SendDlgItemMessage(hwnd, IDC_DELETEEXTRA, BM_SETCHECK, config_deleteextra, 0);
772 if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_WIZNEXT)) {
773 if(config_deleteextra) {
775 findfiles(install_path, &pos, 0, 0, 1);
778 return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_NEXT);
781 BOOL CALLBACK PropertySheetFunc7(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
782 if(message == WM_INITDIALOG) {
785 return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_FINISH);
789 #ifndef PSP_HIDEHEADER
790 #define PSP_HIDEHEADER 2048
793 typedef struct _wizardpage {
798 void runPropertySheet(HWND parent)
800 PROPSHEETHEADER sheet;
802 wizardpage_t wpage[5] = {
804 {PropertySheetFunc1, IDD_LICENSE},
805 {PropertySheetFunc2, IDD_INSTALLDIR},
806 {PropertySheetFunc3, IDD_PROGRESS},
807 {PropertySheetFunc4, IDD_FINISH},
809 {PropertySheetFunc5, IDD_SURE},
810 {PropertySheetFunc6, IDD_EXTRAFILES},
811 {PropertySheetFunc7, IDD_DEINSTALLED},
815 int num = sizeof(wpage)/sizeof(wpage[0]);
820 TODO: remove installdir querying, too (pass installdir
821 to second process) */
824 wpage[t-1] = wpage[t];
830 HPROPSHEETPAGE pages[num];
834 memset(&page, 0, sizeof(PROPSHEETPAGE));
835 page.dwSize = sizeof(PROPSHEETPAGE);
836 page.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER;
838 page.pfnDlgProc = wpage[t].function;
839 page.pszTemplate = MAKEINTRESOURCE(wpage[t].resource);
840 pages[t] = CreatePropertySheetPage(&page);
843 memset(&sheet, 0, sizeof(PROPSHEETHEADER));
844 sheet.dwSize = sizeof(PROPSHEETHEADER);
845 sheet.hInstance = me;
846 sheet.hwndParent = parent;
847 sheet.phpage = pages;
848 sheet.dwFlags = PSH_WIZARD;
850 PropertySheet(&sheet);
855 static void remove_self()
857 char exename[MAX_PATH];
858 char batdir[MAX_PATH];
859 char batfile[MAX_PATH];
863 memset(batdir, 0, sizeof(batdir));
865 GetModuleFileName(NULL, exename, sizeof(exename));
866 GetTempPath(MAX_PATH, batdir);
867 sprintf(batfile, "%08x.bat", rand());
869 batname = concatPaths(batdir, batfile);
871 fp = fopen(batname, "w");
876 fprintf(fp, ":Repeat\n");
877 fprintf(fp, "del \"%s\"\n", exename);
878 fprintf(fp, "if exist \"%s\" goto Repeat\n", exename);
879 fprintf(fp, "rmdir \"%s\"\n", install_path);
880 fprintf(fp, "del \"%s\"\n", batname);
884 PROCESS_INFORMATION pi;
885 memset(&si, 0, sizeof(si));
886 si.dwFlags = STARTF_USESHOWWINDOW;
887 si.wShowWindow = SW_HIDE;
889 if (CreateProcess(NULL, batname, NULL, NULL, FALSE,
890 CREATE_SUSPENDED | IDLE_PRIORITY_CLASS,
891 NULL, "\\", &si, &pi)) {
892 SetThreadPriority(pi.hThread, THREAD_PRIORITY_IDLE);
893 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
894 SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
895 CloseHandle(pi.hProcess);
896 ResumeThread(pi.hThread);
897 CloseHandle(pi.hThread);
902 int WINAPI WinMain(HINSTANCE _me,HINSTANCE hPrevInst,LPSTR lpszArgs, int nWinMode)
905 sprintf(registry_path, "Software\\%s\\%s\\InstallPath", SOFTWARE_DOMAIN, SOFTWARE_NAME);
908 logo = LoadBitmap(me, "LOGO");
910 install_path = getRegistryEntry(registry_path);
911 if(!install_path || !install_path[0]) {
912 MessageBox(0, "Couldn't find software installation directory- did you run the uninstallation twice?", INSTALLER_NAME, MB_OK);
917 InitCommonControls();
919 runPropertySheet(background);
925 int WINAPI WinMain(HINSTANCE _me,HINSTANCE hPrevInst,LPSTR lpszArgs, int nWinMode)
928 WNDCLASSEX wcl_background;
929 wcl_background.hInstance = me;
930 wcl_background.lpfnWndProc = WindowFunc;
931 wcl_background.lpszClassName= INSTALLER_NAME;
932 wcl_background.style = CS_HREDRAW | CS_VREDRAW;
933 wcl_background.hIcon = LoadIcon(NULL, IDI_APPLICATION);
934 wcl_background.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
935 wcl_background.hCursor = LoadCursor(NULL, IDC_ARROW);
936 wcl_background.lpszMenuName = NULL; //no menu
937 wcl_background.cbClsExtra = 0;
938 wcl_background.cbWndExtra = 0;
939 wcl_background.hbrBackground= CreateSolidBrush(RGB(0, 0, 128));
940 wcl_background.cbSize = sizeof(WNDCLASSEX);
942 sprintf(registry_path, "Software\\%s\\%s\\InstallPath", SOFTWARE_DOMAIN, SOFTWARE_NAME);
944 if(lpszArgs && strstr(lpszArgs, "elevated")) {
947 has_full_access = hasFullAccess();
948 config_forAllUsers = has_full_access;
950 HINSTANCE shell32 = LoadLibrary("shell32.dll");
952 MessageBox(0, "Could not load shell32.dll", INSTALLER_NAME, MB_OK);
955 f_SHGetSpecialFolderPath = (HRESULT (WINAPI*)(HWND,LPTSTR,int,BOOL))GetProcAddress(shell32, "SHGetSpecialFolderPathA");
956 if(!f_SHGetSpecialFolderPath) {
957 MessageBox(0, "Could not load shell32.dll", INSTALLER_NAME, MB_OK);
961 HRESULT coret = CoInitialize(NULL);
963 MessageBox(0, "Could not initialize COM interface", INSTALLER_NAME, MB_OK);
967 path_programfiles[0] = 0;
968 f_SHGetSpecialFolderPath(NULL, path_programfiles, CSIDL_PROGRAM_FILES, 0);
970 if(!RegisterClassEx(&wcl_background)) {
971 MessageBox(0, "Could not register window background class", INSTALLER_NAME, MB_OK);
975 HWND background = CreateWindow(wcl_background.lpszClassName, INSTALLER_NAME,
977 GetSystemMetrics(SM_CXFULLSCREEN),
978 GetSystemMetrics(SM_CYFULLSCREEN),
980 (void*)"background");
983 MessageBox(0, "Could not create installation background window", INSTALLER_NAME, MB_OK);
987 ShowWindow(background, SW_SHOWMAXIMIZED);
988 SetForegroundWindow(background);
989 UpdateWindow(background);
991 logo = LoadBitmap(me, "LOGO");
993 install_path = getRegistryEntry(registry_path);
994 if(!install_path || !install_path[0]) {
995 if(path_programfiles[0]) {
996 install_path = concatPaths(path_programfiles, SOFTWARE_NAME);
998 install_path = concatPaths("c:\\", SOFTWARE_NAME);
1002 InitCommonControls();
1004 runPropertySheet(background);