| 1 | n/a | /* |
|---|
| 2 | n/a | IMPORTANT NOTE: IF THIS FILE IS CHANGED, PCBUILD\BDIST_WININST.VCXPROJ MUST |
|---|
| 3 | n/a | BE REBUILT AS WELL. |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | IF CHANGES TO THIS FILE ARE CHECKED IN, THE RECOMPILED BINARIES MUST BE |
|---|
| 6 | n/a | CHECKED IN AS WELL! |
|---|
| 7 | n/a | */ |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | /* |
|---|
| 10 | n/a | * Written by Thomas Heller, May 2000 |
|---|
| 11 | n/a | * |
|---|
| 12 | n/a | * $Id$ |
|---|
| 13 | n/a | */ |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | /* |
|---|
| 16 | n/a | * Windows Installer program for distutils. |
|---|
| 17 | n/a | * |
|---|
| 18 | n/a | * (a kind of self-extracting zip-file) |
|---|
| 19 | n/a | * |
|---|
| 20 | n/a | * At runtime, the exefile has appended: |
|---|
| 21 | n/a | * - compressed setup-data in ini-format, containing the following sections: |
|---|
| 22 | n/a | * [metadata] |
|---|
| 23 | n/a | * author=Greg Ward |
|---|
| 24 | n/a | * author_email=gward@python.net |
|---|
| 25 | n/a | * description=Python Distribution Utilities |
|---|
| 26 | n/a | * licence=Python |
|---|
| 27 | n/a | * name=Distutils |
|---|
| 28 | n/a | * url=http://www.python.org/sigs/distutils-sig/ |
|---|
| 29 | n/a | * version=0.9pre |
|---|
| 30 | n/a | * |
|---|
| 31 | n/a | * [Setup] |
|---|
| 32 | n/a | * info= text to be displayed in the edit-box |
|---|
| 33 | n/a | * title= to be displayed by this program |
|---|
| 34 | n/a | * target_version = if present, python version required |
|---|
| 35 | n/a | * pyc_compile = if 0, do not compile py to pyc |
|---|
| 36 | n/a | * pyo_compile = if 0, do not compile py to pyo |
|---|
| 37 | n/a | * |
|---|
| 38 | n/a | * - a struct meta_data_hdr, describing the above |
|---|
| 39 | n/a | * - a zip-file, containing the modules to be installed. |
|---|
| 40 | n/a | * for the format see http://www.pkware.com/appnote.html |
|---|
| 41 | n/a | * |
|---|
| 42 | n/a | * What does this program do? |
|---|
| 43 | n/a | * - the setup-data is uncompressed and written to a temporary file. |
|---|
| 44 | n/a | * - setup-data is queried with GetPrivateProfile... calls |
|---|
| 45 | n/a | * - [metadata] - info is displayed in the dialog box |
|---|
| 46 | n/a | * - The registry is searched for installations of python |
|---|
| 47 | n/a | * - The user can select the python version to use. |
|---|
| 48 | n/a | * - The python-installation directory (sys.prefix) is displayed |
|---|
| 49 | n/a | * - When the start-button is pressed, files from the zip-archive |
|---|
| 50 | n/a | * are extracted to the file system. All .py filenames are stored |
|---|
| 51 | n/a | * in a list. |
|---|
| 52 | n/a | */ |
|---|
| 53 | n/a | /* |
|---|
| 54 | n/a | * Includes now an uninstaller. |
|---|
| 55 | n/a | */ |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | /* |
|---|
| 58 | n/a | * To Do: |
|---|
| 59 | n/a | * |
|---|
| 60 | n/a | * display some explanation when no python version is found |
|---|
| 61 | n/a | * instead showing the user an empty listbox to select something from. |
|---|
| 62 | n/a | * |
|---|
| 63 | n/a | * Finish the code so that we can use other python installations |
|---|
| 64 | n/a | * additionally to those found in the registry, |
|---|
| 65 | n/a | * and then #define USE_OTHER_PYTHON_VERSIONS |
|---|
| 66 | n/a | * |
|---|
| 67 | n/a | * - install a help-button, which will display something meaningful |
|---|
| 68 | n/a | * to the poor user. |
|---|
| 69 | n/a | * text to the user |
|---|
| 70 | n/a | * - should there be a possibility to display a README file |
|---|
| 71 | n/a | * before starting the installation (if one is present in the archive) |
|---|
| 72 | n/a | * - more comments about what the code does(?) |
|---|
| 73 | n/a | * |
|---|
| 74 | n/a | * - evolve this into a full blown installer (???) |
|---|
| 75 | n/a | */ |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | #include <windows.h> |
|---|
| 78 | n/a | #include <commctrl.h> |
|---|
| 79 | n/a | #include <imagehlp.h> |
|---|
| 80 | n/a | #include <objbase.h> |
|---|
| 81 | n/a | #include <shlobj.h> |
|---|
| 82 | n/a | #include <objidl.h> |
|---|
| 83 | n/a | #include "resource.h" |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | #include <stdio.h> |
|---|
| 86 | n/a | #include <stdlib.h> |
|---|
| 87 | n/a | #include <stdarg.h> |
|---|
| 88 | n/a | #include <string.h> |
|---|
| 89 | n/a | #include <time.h> |
|---|
| 90 | n/a | #include <sys/types.h> |
|---|
| 91 | n/a | #include <sys/stat.h> |
|---|
| 92 | n/a | #include <malloc.h> |
|---|
| 93 | n/a | #include <io.h> |
|---|
| 94 | n/a | #include <fcntl.h> |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | #include "archive.h" |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | /* Only for debugging! |
|---|
| 99 | n/a | static int dprintf(char *fmt, ...) |
|---|
| 100 | n/a | { |
|---|
| 101 | n/a | char Buffer[4096]; |
|---|
| 102 | n/a | va_list marker; |
|---|
| 103 | n/a | int result; |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | va_start(marker, fmt); |
|---|
| 106 | n/a | result = wvsprintf(Buffer, fmt, marker); |
|---|
| 107 | n/a | OutputDebugString(Buffer); |
|---|
| 108 | n/a | return result; |
|---|
| 109 | n/a | } |
|---|
| 110 | n/a | */ |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | /* Bah: global variables */ |
|---|
| 113 | n/a | FILE *logfile; |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | char modulename[MAX_PATH]; |
|---|
| 116 | n/a | wchar_t wmodulename[MAX_PATH]; |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | HWND hwndMain; |
|---|
| 119 | n/a | HWND hDialog; |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | char *ini_file; /* Full pathname of ini-file */ |
|---|
| 122 | n/a | /* From ini-file */ |
|---|
| 123 | n/a | char info[4096]; /* [Setup] info= */ |
|---|
| 124 | n/a | char title[80]; /* [Setup] title=, contains package name |
|---|
| 125 | n/a | including version: "Distutils-1.0.1" */ |
|---|
| 126 | n/a | char target_version[10]; /* [Setup] target_version=, required python |
|---|
| 127 | n/a | version or empty string */ |
|---|
| 128 | n/a | char build_info[80]; /* [Setup] build_info=, distutils version |
|---|
| 129 | n/a | and build date */ |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | char meta_name[80]; /* package name without version like |
|---|
| 132 | n/a | 'Distutils' */ |
|---|
| 133 | n/a | char install_script[MAX_PATH]; |
|---|
| 134 | n/a | char *pre_install_script; /* run before we install a single file */ |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | char user_access_control[10]; // one of 'auto', 'force', otherwise none. |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | int py_major, py_minor; /* Python version selected for installation */ |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | char *arc_data; /* memory mapped archive */ |
|---|
| 141 | n/a | DWORD arc_size; /* number of bytes in archive */ |
|---|
| 142 | n/a | int exe_size; /* number of bytes for exe-file portion */ |
|---|
| 143 | n/a | char python_dir[MAX_PATH]; |
|---|
| 144 | n/a | char pythondll[MAX_PATH]; |
|---|
| 145 | n/a | BOOL pyc_compile, pyo_compile; |
|---|
| 146 | n/a | /* Either HKLM or HKCU, depending on where Python itself is registered, and |
|---|
| 147 | n/a | the permissions of the current user. */ |
|---|
| 148 | n/a | HKEY hkey_root = (HKEY)-1; |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | BOOL success; /* Installation successful? */ |
|---|
| 151 | n/a | char *failure_reason = NULL; |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | HANDLE hBitmap; |
|---|
| 154 | n/a | char *bitmap_bytes; |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | static const char *REGISTRY_SUFFIX_6432 = |
|---|
| 157 | n/a | #ifdef _WIN64 |
|---|
| 158 | n/a | ""; |
|---|
| 159 | n/a | #else |
|---|
| 160 | n/a | "-32"; |
|---|
| 161 | n/a | #endif |
|---|
| 162 | n/a | |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | #define WM_NUMFILES WM_USER+1 |
|---|
| 165 | n/a | /* wParam: 0, lParam: total number of files */ |
|---|
| 166 | n/a | #define WM_NEXTFILE WM_USER+2 |
|---|
| 167 | n/a | /* wParam: number of this file */ |
|---|
| 168 | n/a | /* lParam: points to pathname */ |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | static BOOL notify(int code, char *fmt, ...); |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | /* Note: If scheme.prefix is nonempty, it must end with a '\'! */ |
|---|
| 173 | n/a | /* Note: purelib must be the FIRST entry! */ |
|---|
| 174 | n/a | SCHEME old_scheme[] = { |
|---|
| 175 | n/a | { "PURELIB", "" }, |
|---|
| 176 | n/a | { "PLATLIB", "" }, |
|---|
| 177 | n/a | { "HEADERS", "" }, /* 'Include/dist_name' part already in archive */ |
|---|
| 178 | n/a | { "SCRIPTS", "Scripts\\" }, |
|---|
| 179 | n/a | { "DATA", "" }, |
|---|
| 180 | n/a | { NULL, NULL }, |
|---|
| 181 | n/a | }; |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | SCHEME new_scheme[] = { |
|---|
| 184 | n/a | { "PURELIB", "Lib\\site-packages\\" }, |
|---|
| 185 | n/a | { "PLATLIB", "Lib\\site-packages\\" }, |
|---|
| 186 | n/a | { "HEADERS", "" }, /* 'Include/dist_name' part already in archive */ |
|---|
| 187 | n/a | { "SCRIPTS", "Scripts\\" }, |
|---|
| 188 | n/a | { "DATA", "" }, |
|---|
| 189 | n/a | { NULL, NULL }, |
|---|
| 190 | n/a | }; |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | static void unescape(char *dst, char *src, unsigned size) |
|---|
| 193 | n/a | { |
|---|
| 194 | n/a | char *eon; |
|---|
| 195 | n/a | char ch; |
|---|
| 196 | n/a | |
|---|
| 197 | n/a | while (src && *src && (size > 2)) { |
|---|
| 198 | n/a | if (*src == '\\') { |
|---|
| 199 | n/a | switch (*++src) { |
|---|
| 200 | n/a | case 'n': |
|---|
| 201 | n/a | ++src; |
|---|
| 202 | n/a | *dst++ = '\r'; |
|---|
| 203 | n/a | *dst++ = '\n'; |
|---|
| 204 | n/a | size -= 2; |
|---|
| 205 | n/a | break; |
|---|
| 206 | n/a | case 'r': |
|---|
| 207 | n/a | ++src; |
|---|
| 208 | n/a | *dst++ = '\r'; |
|---|
| 209 | n/a | --size; |
|---|
| 210 | n/a | break; |
|---|
| 211 | n/a | case '0': case '1': case '2': case '3': |
|---|
| 212 | n/a | ch = (char)strtol(src, &eon, 8); |
|---|
| 213 | n/a | if (ch == '\n') { |
|---|
| 214 | n/a | *dst++ = '\r'; |
|---|
| 215 | n/a | --size; |
|---|
| 216 | n/a | } |
|---|
| 217 | n/a | *dst++ = ch; |
|---|
| 218 | n/a | --size; |
|---|
| 219 | n/a | src = eon; |
|---|
| 220 | n/a | } |
|---|
| 221 | n/a | } else { |
|---|
| 222 | n/a | *dst++ = *src++; |
|---|
| 223 | n/a | --size; |
|---|
| 224 | n/a | } |
|---|
| 225 | n/a | } |
|---|
| 226 | n/a | *dst = '\0'; |
|---|
| 227 | n/a | } |
|---|
| 228 | n/a | |
|---|
| 229 | n/a | static struct tagFile { |
|---|
| 230 | n/a | char *path; |
|---|
| 231 | n/a | struct tagFile *next; |
|---|
| 232 | n/a | } *file_list = NULL; |
|---|
| 233 | n/a | |
|---|
| 234 | n/a | static void set_failure_reason(char *reason) |
|---|
| 235 | n/a | { |
|---|
| 236 | n/a | if (failure_reason) |
|---|
| 237 | n/a | free(failure_reason); |
|---|
| 238 | n/a | failure_reason = strdup(reason); |
|---|
| 239 | n/a | success = FALSE; |
|---|
| 240 | n/a | } |
|---|
| 241 | n/a | static char *get_failure_reason() |
|---|
| 242 | n/a | { |
|---|
| 243 | n/a | if (!failure_reason) |
|---|
| 244 | n/a | return "Installation failed."; |
|---|
| 245 | n/a | return failure_reason; |
|---|
| 246 | n/a | } |
|---|
| 247 | n/a | |
|---|
| 248 | n/a | static void add_to_filelist(char *path) |
|---|
| 249 | n/a | { |
|---|
| 250 | n/a | struct tagFile *p; |
|---|
| 251 | n/a | p = (struct tagFile *)malloc(sizeof(struct tagFile)); |
|---|
| 252 | n/a | p->path = strdup(path); |
|---|
| 253 | n/a | p->next = file_list; |
|---|
| 254 | n/a | file_list = p; |
|---|
| 255 | n/a | } |
|---|
| 256 | n/a | |
|---|
| 257 | n/a | static int do_compile_files(int (__cdecl * PyRun_SimpleString)(char *), |
|---|
| 258 | n/a | int optimize) |
|---|
| 259 | n/a | { |
|---|
| 260 | n/a | struct tagFile *p; |
|---|
| 261 | n/a | int total, n; |
|---|
| 262 | n/a | char Buffer[MAX_PATH + 64]; |
|---|
| 263 | n/a | int errors = 0; |
|---|
| 264 | n/a | |
|---|
| 265 | n/a | total = 0; |
|---|
| 266 | n/a | p = file_list; |
|---|
| 267 | n/a | while (p) { |
|---|
| 268 | n/a | ++total; |
|---|
| 269 | n/a | p = p->next; |
|---|
| 270 | n/a | } |
|---|
| 271 | n/a | SendDlgItemMessage(hDialog, IDC_PROGRESS, PBM_SETRANGE, 0, |
|---|
| 272 | n/a | MAKELPARAM(0, total)); |
|---|
| 273 | n/a | SendDlgItemMessage(hDialog, IDC_PROGRESS, PBM_SETPOS, 0, 0); |
|---|
| 274 | n/a | |
|---|
| 275 | n/a | n = 0; |
|---|
| 276 | n/a | p = file_list; |
|---|
| 277 | n/a | while (p) { |
|---|
| 278 | n/a | ++n; |
|---|
| 279 | n/a | wsprintf(Buffer, |
|---|
| 280 | n/a | "import py_compile; py_compile.compile (r'%s')", |
|---|
| 281 | n/a | p->path); |
|---|
| 282 | n/a | if (PyRun_SimpleString(Buffer)) { |
|---|
| 283 | n/a | ++errors; |
|---|
| 284 | n/a | } |
|---|
| 285 | n/a | /* We send the notification even if the files could not |
|---|
| 286 | n/a | * be created so that the uninstaller will remove them |
|---|
| 287 | n/a | * in case they are created later. |
|---|
| 288 | n/a | */ |
|---|
| 289 | n/a | wsprintf(Buffer, "%s%c", p->path, optimize ? 'o' : 'c'); |
|---|
| 290 | n/a | notify(FILE_CREATED, Buffer); |
|---|
| 291 | n/a | |
|---|
| 292 | n/a | SendDlgItemMessage(hDialog, IDC_PROGRESS, PBM_SETPOS, n, 0); |
|---|
| 293 | n/a | SetDlgItemText(hDialog, IDC_INFO, p->path); |
|---|
| 294 | n/a | p = p->next; |
|---|
| 295 | n/a | } |
|---|
| 296 | n/a | return errors; |
|---|
| 297 | n/a | } |
|---|
| 298 | n/a | |
|---|
| 299 | n/a | #define DECLPROC(dll, result, name, args)\ |
|---|
| 300 | n/a | typedef result (*__PROC__##name) args;\ |
|---|
| 301 | n/a | result (*name)args = (__PROC__##name)GetProcAddress(dll, #name) |
|---|
| 302 | n/a | |
|---|
| 303 | n/a | |
|---|
| 304 | n/a | #define DECLVAR(dll, type, name)\ |
|---|
| 305 | n/a | type *name = (type*)GetProcAddress(dll, #name) |
|---|
| 306 | n/a | |
|---|
| 307 | n/a | typedef void PyObject; |
|---|
| 308 | n/a | |
|---|
| 309 | n/a | // Convert a "char *" string to "whcar_t *", or NULL on error. |
|---|
| 310 | n/a | // Result string must be free'd |
|---|
| 311 | n/a | wchar_t *widen_string(char *src) |
|---|
| 312 | n/a | { |
|---|
| 313 | n/a | wchar_t *result; |
|---|
| 314 | n/a | DWORD dest_cch; |
|---|
| 315 | n/a | int src_len = strlen(src) + 1; // include NULL term in all ops |
|---|
| 316 | n/a | /* use MultiByteToWideChar() to see how much we need. */ |
|---|
| 317 | n/a | /* NOTE: this will include the null-term in the length */ |
|---|
| 318 | n/a | dest_cch = MultiByteToWideChar(CP_ACP, 0, src, src_len, NULL, 0); |
|---|
| 319 | n/a | // alloc the buffer |
|---|
| 320 | n/a | result = (wchar_t *)malloc(dest_cch * sizeof(wchar_t)); |
|---|
| 321 | n/a | if (result==NULL) |
|---|
| 322 | n/a | return NULL; |
|---|
| 323 | n/a | /* do the conversion */ |
|---|
| 324 | n/a | if (0==MultiByteToWideChar(CP_ACP, 0, src, src_len, result, dest_cch)) { |
|---|
| 325 | n/a | free(result); |
|---|
| 326 | n/a | return NULL; |
|---|
| 327 | n/a | } |
|---|
| 328 | n/a | return result; |
|---|
| 329 | n/a | } |
|---|
| 330 | n/a | |
|---|
| 331 | n/a | /* |
|---|
| 332 | n/a | * Returns number of files which failed to compile, |
|---|
| 333 | n/a | * -1 if python could not be loaded at all |
|---|
| 334 | n/a | */ |
|---|
| 335 | n/a | static int compile_filelist(HINSTANCE hPython, BOOL optimize_flag) |
|---|
| 336 | n/a | { |
|---|
| 337 | n/a | DECLPROC(hPython, void, Py_Initialize, (void)); |
|---|
| 338 | n/a | DECLPROC(hPython, void, Py_SetProgramName, (wchar_t *)); |
|---|
| 339 | n/a | DECLPROC(hPython, void, Py_Finalize, (void)); |
|---|
| 340 | n/a | DECLPROC(hPython, int, PyRun_SimpleString, (char *)); |
|---|
| 341 | n/a | DECLPROC(hPython, PyObject *, PySys_GetObject, (char *)); |
|---|
| 342 | n/a | DECLVAR(hPython, int, Py_OptimizeFlag); |
|---|
| 343 | n/a | |
|---|
| 344 | n/a | int errors = 0; |
|---|
| 345 | n/a | struct tagFile *p = file_list; |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | if (!p) |
|---|
| 348 | n/a | return 0; |
|---|
| 349 | n/a | |
|---|
| 350 | n/a | if (!Py_Initialize || !Py_SetProgramName || !Py_Finalize) |
|---|
| 351 | n/a | return -1; |
|---|
| 352 | n/a | |
|---|
| 353 | n/a | if (!PyRun_SimpleString || !PySys_GetObject || !Py_OptimizeFlag) |
|---|
| 354 | n/a | return -1; |
|---|
| 355 | n/a | |
|---|
| 356 | n/a | *Py_OptimizeFlag = optimize_flag ? 1 : 0; |
|---|
| 357 | n/a | Py_SetProgramName(wmodulename); |
|---|
| 358 | n/a | Py_Initialize(); |
|---|
| 359 | n/a | |
|---|
| 360 | n/a | errors += do_compile_files(PyRun_SimpleString, optimize_flag); |
|---|
| 361 | n/a | Py_Finalize(); |
|---|
| 362 | n/a | |
|---|
| 363 | n/a | return errors; |
|---|
| 364 | n/a | } |
|---|
| 365 | n/a | |
|---|
| 366 | n/a | typedef PyObject *(*PyCFunction)(PyObject *, PyObject *); |
|---|
| 367 | n/a | |
|---|
| 368 | n/a | struct PyMethodDef { |
|---|
| 369 | n/a | char *ml_name; |
|---|
| 370 | n/a | PyCFunction ml_meth; |
|---|
| 371 | n/a | int ml_flags; |
|---|
| 372 | n/a | char *ml_doc; |
|---|
| 373 | n/a | }; |
|---|
| 374 | n/a | typedef struct PyMethodDef PyMethodDef; |
|---|
| 375 | n/a | |
|---|
| 376 | n/a | // XXX - all of these are potentially fragile! We load and unload |
|---|
| 377 | n/a | // the Python DLL multiple times - so storing functions pointers |
|---|
| 378 | n/a | // is dangerous (although things *look* OK at present) |
|---|
| 379 | n/a | // Better might be to roll prepare_script_environment() into |
|---|
| 380 | n/a | // LoadPythonDll(), and create a new UnloadPythonDLL() which also |
|---|
| 381 | n/a | // clears the global pointers. |
|---|
| 382 | n/a | void *(*g_Py_BuildValue)(char *, ...); |
|---|
| 383 | n/a | int (*g_PyArg_ParseTuple)(PyObject *, char *, ...); |
|---|
| 384 | n/a | PyObject * (*g_PyLong_FromVoidPtr)(void *); |
|---|
| 385 | n/a | |
|---|
| 386 | n/a | PyObject *g_PyExc_ValueError; |
|---|
| 387 | n/a | PyObject *g_PyExc_OSError; |
|---|
| 388 | n/a | |
|---|
| 389 | n/a | PyObject *(*g_PyErr_Format)(PyObject *, char *, ...); |
|---|
| 390 | n/a | |
|---|
| 391 | n/a | #define DEF_CSIDL(name) { name, #name } |
|---|
| 392 | n/a | |
|---|
| 393 | n/a | struct { |
|---|
| 394 | n/a | int nFolder; |
|---|
| 395 | n/a | char *name; |
|---|
| 396 | n/a | } csidl_names[] = { |
|---|
| 397 | n/a | /* Startup menu for all users. |
|---|
| 398 | n/a | NT only */ |
|---|
| 399 | n/a | DEF_CSIDL(CSIDL_COMMON_STARTMENU), |
|---|
| 400 | n/a | /* Startup menu. */ |
|---|
| 401 | n/a | DEF_CSIDL(CSIDL_STARTMENU), |
|---|
| 402 | n/a | |
|---|
| 403 | n/a | /* DEF_CSIDL(CSIDL_COMMON_APPDATA), */ |
|---|
| 404 | n/a | /* DEF_CSIDL(CSIDL_LOCAL_APPDATA), */ |
|---|
| 405 | n/a | /* Repository for application-specific data. |
|---|
| 406 | n/a | Needs Internet Explorer 4.0 */ |
|---|
| 407 | n/a | DEF_CSIDL(CSIDL_APPDATA), |
|---|
| 408 | n/a | |
|---|
| 409 | n/a | /* The desktop for all users. |
|---|
| 410 | n/a | NT only */ |
|---|
| 411 | n/a | DEF_CSIDL(CSIDL_COMMON_DESKTOPDIRECTORY), |
|---|
| 412 | n/a | /* The desktop. */ |
|---|
| 413 | n/a | DEF_CSIDL(CSIDL_DESKTOPDIRECTORY), |
|---|
| 414 | n/a | |
|---|
| 415 | n/a | /* Startup folder for all users. |
|---|
| 416 | n/a | NT only */ |
|---|
| 417 | n/a | DEF_CSIDL(CSIDL_COMMON_STARTUP), |
|---|
| 418 | n/a | /* Startup folder. */ |
|---|
| 419 | n/a | DEF_CSIDL(CSIDL_STARTUP), |
|---|
| 420 | n/a | |
|---|
| 421 | n/a | /* Programs item in the start menu for all users. |
|---|
| 422 | n/a | NT only */ |
|---|
| 423 | n/a | DEF_CSIDL(CSIDL_COMMON_PROGRAMS), |
|---|
| 424 | n/a | /* Program item in the user's start menu. */ |
|---|
| 425 | n/a | DEF_CSIDL(CSIDL_PROGRAMS), |
|---|
| 426 | n/a | |
|---|
| 427 | n/a | /* DEF_CSIDL(CSIDL_PROGRAM_FILES_COMMON), */ |
|---|
| 428 | n/a | /* DEF_CSIDL(CSIDL_PROGRAM_FILES), */ |
|---|
| 429 | n/a | |
|---|
| 430 | n/a | /* Virtual folder containing fonts. */ |
|---|
| 431 | n/a | DEF_CSIDL(CSIDL_FONTS), |
|---|
| 432 | n/a | }; |
|---|
| 433 | n/a | |
|---|
| 434 | n/a | #define DIM(a) (sizeof(a) / sizeof((a)[0])) |
|---|
| 435 | n/a | |
|---|
| 436 | n/a | static PyObject *FileCreated(PyObject *self, PyObject *args) |
|---|
| 437 | n/a | { |
|---|
| 438 | n/a | char *path; |
|---|
| 439 | n/a | if (!g_PyArg_ParseTuple(args, "s", &path)) |
|---|
| 440 | n/a | return NULL; |
|---|
| 441 | n/a | notify(FILE_CREATED, path); |
|---|
| 442 | n/a | return g_Py_BuildValue(""); |
|---|
| 443 | n/a | } |
|---|
| 444 | n/a | |
|---|
| 445 | n/a | static PyObject *DirectoryCreated(PyObject *self, PyObject *args) |
|---|
| 446 | n/a | { |
|---|
| 447 | n/a | char *path; |
|---|
| 448 | n/a | if (!g_PyArg_ParseTuple(args, "s", &path)) |
|---|
| 449 | n/a | return NULL; |
|---|
| 450 | n/a | notify(DIR_CREATED, path); |
|---|
| 451 | n/a | return g_Py_BuildValue(""); |
|---|
| 452 | n/a | } |
|---|
| 453 | n/a | |
|---|
| 454 | n/a | static PyObject *GetSpecialFolderPath(PyObject *self, PyObject *args) |
|---|
| 455 | n/a | { |
|---|
| 456 | n/a | char *name; |
|---|
| 457 | n/a | char lpszPath[MAX_PATH]; |
|---|
| 458 | n/a | int i; |
|---|
| 459 | n/a | static HRESULT (WINAPI *My_SHGetSpecialFolderPath)(HWND hwnd, |
|---|
| 460 | n/a | LPTSTR lpszPath, |
|---|
| 461 | n/a | int nFolder, |
|---|
| 462 | n/a | BOOL fCreate); |
|---|
| 463 | n/a | |
|---|
| 464 | n/a | if (!My_SHGetSpecialFolderPath) { |
|---|
| 465 | n/a | HINSTANCE hLib = LoadLibrary("shell32.dll"); |
|---|
| 466 | n/a | if (!hLib) { |
|---|
| 467 | n/a | g_PyErr_Format(g_PyExc_OSError, |
|---|
| 468 | n/a | "function not available"); |
|---|
| 469 | n/a | return NULL; |
|---|
| 470 | n/a | } |
|---|
| 471 | n/a | My_SHGetSpecialFolderPath = (BOOL (WINAPI *)(HWND, LPTSTR, |
|---|
| 472 | n/a | int, BOOL)) |
|---|
| 473 | n/a | GetProcAddress(hLib, |
|---|
| 474 | n/a | "SHGetSpecialFolderPathA"); |
|---|
| 475 | n/a | } |
|---|
| 476 | n/a | |
|---|
| 477 | n/a | if (!g_PyArg_ParseTuple(args, "s", &name)) |
|---|
| 478 | n/a | return NULL; |
|---|
| 479 | n/a | |
|---|
| 480 | n/a | if (!My_SHGetSpecialFolderPath) { |
|---|
| 481 | n/a | g_PyErr_Format(g_PyExc_OSError, "function not available"); |
|---|
| 482 | n/a | return NULL; |
|---|
| 483 | n/a | } |
|---|
| 484 | n/a | |
|---|
| 485 | n/a | for (i = 0; i < DIM(csidl_names); ++i) { |
|---|
| 486 | n/a | if (0 == strcmpi(csidl_names[i].name, name)) { |
|---|
| 487 | n/a | int nFolder; |
|---|
| 488 | n/a | nFolder = csidl_names[i].nFolder; |
|---|
| 489 | n/a | if (My_SHGetSpecialFolderPath(NULL, lpszPath, |
|---|
| 490 | n/a | nFolder, 0)) |
|---|
| 491 | n/a | return g_Py_BuildValue("s", lpszPath); |
|---|
| 492 | n/a | else { |
|---|
| 493 | n/a | g_PyErr_Format(g_PyExc_OSError, |
|---|
| 494 | n/a | "no such folder (%s)", name); |
|---|
| 495 | n/a | return NULL; |
|---|
| 496 | n/a | } |
|---|
| 497 | n/a | |
|---|
| 498 | n/a | } |
|---|
| 499 | n/a | }; |
|---|
| 500 | n/a | g_PyErr_Format(g_PyExc_ValueError, "unknown CSIDL (%s)", name); |
|---|
| 501 | n/a | return NULL; |
|---|
| 502 | n/a | } |
|---|
| 503 | n/a | |
|---|
| 504 | n/a | static PyObject *CreateShortcut(PyObject *self, PyObject *args) |
|---|
| 505 | n/a | { |
|---|
| 506 | n/a | char *path; /* path and filename */ |
|---|
| 507 | n/a | char *description; |
|---|
| 508 | n/a | char *filename; |
|---|
| 509 | n/a | |
|---|
| 510 | n/a | char *arguments = NULL; |
|---|
| 511 | n/a | char *iconpath = NULL; |
|---|
| 512 | n/a | int iconindex = 0; |
|---|
| 513 | n/a | char *workdir = NULL; |
|---|
| 514 | n/a | |
|---|
| 515 | n/a | WCHAR wszFilename[MAX_PATH]; |
|---|
| 516 | n/a | |
|---|
| 517 | n/a | IShellLink *ps1 = NULL; |
|---|
| 518 | n/a | IPersistFile *pPf = NULL; |
|---|
| 519 | n/a | |
|---|
| 520 | n/a | HRESULT hr; |
|---|
| 521 | n/a | |
|---|
| 522 | n/a | hr = CoInitialize(NULL); |
|---|
| 523 | n/a | if (FAILED(hr)) { |
|---|
| 524 | n/a | g_PyErr_Format(g_PyExc_OSError, |
|---|
| 525 | n/a | "CoInitialize failed, error 0x%x", hr); |
|---|
| 526 | n/a | goto error; |
|---|
| 527 | n/a | } |
|---|
| 528 | n/a | |
|---|
| 529 | n/a | if (!g_PyArg_ParseTuple(args, "sss|sssi", |
|---|
| 530 | n/a | &path, &description, &filename, |
|---|
| 531 | n/a | &arguments, &workdir, &iconpath, &iconindex)) |
|---|
| 532 | n/a | return NULL; |
|---|
| 533 | n/a | |
|---|
| 534 | n/a | hr = CoCreateInstance(&CLSID_ShellLink, |
|---|
| 535 | n/a | NULL, |
|---|
| 536 | n/a | CLSCTX_INPROC_SERVER, |
|---|
| 537 | n/a | &IID_IShellLink, |
|---|
| 538 | n/a | &ps1); |
|---|
| 539 | n/a | if (FAILED(hr)) { |
|---|
| 540 | n/a | g_PyErr_Format(g_PyExc_OSError, |
|---|
| 541 | n/a | "CoCreateInstance failed, error 0x%x", hr); |
|---|
| 542 | n/a | goto error; |
|---|
| 543 | n/a | } |
|---|
| 544 | n/a | |
|---|
| 545 | n/a | hr = ps1->lpVtbl->QueryInterface(ps1, &IID_IPersistFile, |
|---|
| 546 | n/a | (void **)&pPf); |
|---|
| 547 | n/a | if (FAILED(hr)) { |
|---|
| 548 | n/a | g_PyErr_Format(g_PyExc_OSError, |
|---|
| 549 | n/a | "QueryInterface(IPersistFile) error 0x%x", hr); |
|---|
| 550 | n/a | goto error; |
|---|
| 551 | n/a | } |
|---|
| 552 | n/a | |
|---|
| 553 | n/a | |
|---|
| 554 | n/a | hr = ps1->lpVtbl->SetPath(ps1, path); |
|---|
| 555 | n/a | if (FAILED(hr)) { |
|---|
| 556 | n/a | g_PyErr_Format(g_PyExc_OSError, |
|---|
| 557 | n/a | "SetPath() failed, error 0x%x", hr); |
|---|
| 558 | n/a | goto error; |
|---|
| 559 | n/a | } |
|---|
| 560 | n/a | |
|---|
| 561 | n/a | hr = ps1->lpVtbl->SetDescription(ps1, description); |
|---|
| 562 | n/a | if (FAILED(hr)) { |
|---|
| 563 | n/a | g_PyErr_Format(g_PyExc_OSError, |
|---|
| 564 | n/a | "SetDescription() failed, error 0x%x", hr); |
|---|
| 565 | n/a | goto error; |
|---|
| 566 | n/a | } |
|---|
| 567 | n/a | |
|---|
| 568 | n/a | if (arguments) { |
|---|
| 569 | n/a | hr = ps1->lpVtbl->SetArguments(ps1, arguments); |
|---|
| 570 | n/a | if (FAILED(hr)) { |
|---|
| 571 | n/a | g_PyErr_Format(g_PyExc_OSError, |
|---|
| 572 | n/a | "SetArguments() error 0x%x", hr); |
|---|
| 573 | n/a | goto error; |
|---|
| 574 | n/a | } |
|---|
| 575 | n/a | } |
|---|
| 576 | n/a | |
|---|
| 577 | n/a | if (iconpath) { |
|---|
| 578 | n/a | hr = ps1->lpVtbl->SetIconLocation(ps1, iconpath, iconindex); |
|---|
| 579 | n/a | if (FAILED(hr)) { |
|---|
| 580 | n/a | g_PyErr_Format(g_PyExc_OSError, |
|---|
| 581 | n/a | "SetIconLocation() error 0x%x", hr); |
|---|
| 582 | n/a | goto error; |
|---|
| 583 | n/a | } |
|---|
| 584 | n/a | } |
|---|
| 585 | n/a | |
|---|
| 586 | n/a | if (workdir) { |
|---|
| 587 | n/a | hr = ps1->lpVtbl->SetWorkingDirectory(ps1, workdir); |
|---|
| 588 | n/a | if (FAILED(hr)) { |
|---|
| 589 | n/a | g_PyErr_Format(g_PyExc_OSError, |
|---|
| 590 | n/a | "SetWorkingDirectory() error 0x%x", hr); |
|---|
| 591 | n/a | goto error; |
|---|
| 592 | n/a | } |
|---|
| 593 | n/a | } |
|---|
| 594 | n/a | |
|---|
| 595 | n/a | MultiByteToWideChar(CP_ACP, 0, |
|---|
| 596 | n/a | filename, -1, |
|---|
| 597 | n/a | wszFilename, MAX_PATH); |
|---|
| 598 | n/a | |
|---|
| 599 | n/a | hr = pPf->lpVtbl->Save(pPf, wszFilename, TRUE); |
|---|
| 600 | n/a | if (FAILED(hr)) { |
|---|
| 601 | n/a | g_PyErr_Format(g_PyExc_OSError, |
|---|
| 602 | n/a | "Failed to create shortcut '%s' - error 0x%x", filename, hr); |
|---|
| 603 | n/a | goto error; |
|---|
| 604 | n/a | } |
|---|
| 605 | n/a | |
|---|
| 606 | n/a | pPf->lpVtbl->Release(pPf); |
|---|
| 607 | n/a | ps1->lpVtbl->Release(ps1); |
|---|
| 608 | n/a | CoUninitialize(); |
|---|
| 609 | n/a | return g_Py_BuildValue(""); |
|---|
| 610 | n/a | |
|---|
| 611 | n/a | error: |
|---|
| 612 | n/a | if (pPf) |
|---|
| 613 | n/a | pPf->lpVtbl->Release(pPf); |
|---|
| 614 | n/a | |
|---|
| 615 | n/a | if (ps1) |
|---|
| 616 | n/a | ps1->lpVtbl->Release(ps1); |
|---|
| 617 | n/a | |
|---|
| 618 | n/a | CoUninitialize(); |
|---|
| 619 | n/a | |
|---|
| 620 | n/a | return NULL; |
|---|
| 621 | n/a | } |
|---|
| 622 | n/a | |
|---|
| 623 | n/a | static PyObject *PyMessageBox(PyObject *self, PyObject *args) |
|---|
| 624 | n/a | { |
|---|
| 625 | n/a | int rc; |
|---|
| 626 | n/a | char *text, *caption; |
|---|
| 627 | n/a | int flags; |
|---|
| 628 | n/a | if (!g_PyArg_ParseTuple(args, "ssi", &text, &caption, &flags)) |
|---|
| 629 | n/a | return NULL; |
|---|
| 630 | n/a | rc = MessageBox(GetFocus(), text, caption, flags); |
|---|
| 631 | n/a | return g_Py_BuildValue("i", rc); |
|---|
| 632 | n/a | } |
|---|
| 633 | n/a | |
|---|
| 634 | n/a | static PyObject *GetRootHKey(PyObject *self) |
|---|
| 635 | n/a | { |
|---|
| 636 | n/a | return g_PyLong_FromVoidPtr(hkey_root); |
|---|
| 637 | n/a | } |
|---|
| 638 | n/a | |
|---|
| 639 | n/a | #define METH_VARARGS 0x0001 |
|---|
| 640 | n/a | #define METH_NOARGS 0x0004 |
|---|
| 641 | n/a | typedef PyObject *(*PyCFunction)(PyObject *, PyObject *); |
|---|
| 642 | n/a | |
|---|
| 643 | n/a | PyMethodDef meth[] = { |
|---|
| 644 | n/a | {"create_shortcut", CreateShortcut, METH_VARARGS, NULL}, |
|---|
| 645 | n/a | {"get_special_folder_path", GetSpecialFolderPath, METH_VARARGS, NULL}, |
|---|
| 646 | n/a | {"get_root_hkey", (PyCFunction)GetRootHKey, METH_NOARGS, NULL}, |
|---|
| 647 | n/a | {"file_created", FileCreated, METH_VARARGS, NULL}, |
|---|
| 648 | n/a | {"directory_created", DirectoryCreated, METH_VARARGS, NULL}, |
|---|
| 649 | n/a | {"message_box", PyMessageBox, METH_VARARGS, NULL}, |
|---|
| 650 | n/a | }; |
|---|
| 651 | n/a | |
|---|
| 652 | n/a | static HINSTANCE LoadPythonDll(char *fname) |
|---|
| 653 | n/a | { |
|---|
| 654 | n/a | char fullpath[_MAX_PATH]; |
|---|
| 655 | n/a | LONG size = sizeof(fullpath); |
|---|
| 656 | n/a | char subkey_name[80]; |
|---|
| 657 | n/a | char buffer[260 + 12]; |
|---|
| 658 | n/a | HINSTANCE h; |
|---|
| 659 | n/a | |
|---|
| 660 | n/a | /* make sure PYTHONHOME is set, to that sys.path is initialized correctly */ |
|---|
| 661 | n/a | wsprintf(buffer, "PYTHONHOME=%s", python_dir); |
|---|
| 662 | n/a | _putenv(buffer); |
|---|
| 663 | n/a | h = LoadLibrary(fname); |
|---|
| 664 | n/a | if (h) |
|---|
| 665 | n/a | return h; |
|---|
| 666 | n/a | wsprintf(subkey_name, |
|---|
| 667 | n/a | "SOFTWARE\\Python\\PythonCore\\%d.%d%s\\InstallPath", |
|---|
| 668 | n/a | py_major, py_minor, REGISTRY_SUFFIX_6432); |
|---|
| 669 | n/a | if (ERROR_SUCCESS != RegQueryValue(HKEY_CURRENT_USER, subkey_name, |
|---|
| 670 | n/a | fullpath, &size) && |
|---|
| 671 | n/a | ERROR_SUCCESS != RegQueryValue(HKEY_LOCAL_MACHINE, subkey_name, |
|---|
| 672 | n/a | fullpath, &size)) |
|---|
| 673 | n/a | return NULL; |
|---|
| 674 | n/a | strcat(fullpath, "\\"); |
|---|
| 675 | n/a | strcat(fullpath, fname); |
|---|
| 676 | n/a | // We use LOAD_WITH_ALTERED_SEARCH_PATH to ensure any dependent DLLs |
|---|
| 677 | n/a | // next to the Python DLL (eg, the CRT DLL) are also loaded. |
|---|
| 678 | n/a | return LoadLibraryEx(fullpath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); |
|---|
| 679 | n/a | } |
|---|
| 680 | n/a | |
|---|
| 681 | n/a | static int prepare_script_environment(HINSTANCE hPython) |
|---|
| 682 | n/a | { |
|---|
| 683 | n/a | PyObject *mod; |
|---|
| 684 | n/a | DECLPROC(hPython, PyObject *, PyImport_ImportModule, (char *)); |
|---|
| 685 | n/a | DECLPROC(hPython, int, PyObject_SetAttrString, (PyObject *, char *, PyObject *)); |
|---|
| 686 | n/a | DECLPROC(hPython, PyObject *, PyObject_GetAttrString, (PyObject *, char *)); |
|---|
| 687 | n/a | DECLPROC(hPython, PyObject *, PyCFunction_New, (PyMethodDef *, PyObject *)); |
|---|
| 688 | n/a | DECLPROC(hPython, PyObject *, Py_BuildValue, (char *, ...)); |
|---|
| 689 | n/a | DECLPROC(hPython, int, PyArg_ParseTuple, (PyObject *, char *, ...)); |
|---|
| 690 | n/a | DECLPROC(hPython, PyObject *, PyErr_Format, (PyObject *, char *)); |
|---|
| 691 | n/a | DECLPROC(hPython, PyObject *, PyLong_FromVoidPtr, (void *)); |
|---|
| 692 | n/a | if (!PyImport_ImportModule || !PyObject_GetAttrString || |
|---|
| 693 | n/a | !PyObject_SetAttrString || !PyCFunction_New) |
|---|
| 694 | n/a | return 1; |
|---|
| 695 | n/a | if (!Py_BuildValue || !PyArg_ParseTuple || !PyErr_Format) |
|---|
| 696 | n/a | return 1; |
|---|
| 697 | n/a | |
|---|
| 698 | n/a | mod = PyImport_ImportModule("builtins"); |
|---|
| 699 | n/a | if (mod) { |
|---|
| 700 | n/a | int i; |
|---|
| 701 | n/a | g_PyExc_ValueError = PyObject_GetAttrString(mod, "ValueError"); |
|---|
| 702 | n/a | g_PyExc_OSError = PyObject_GetAttrString(mod, "OSError"); |
|---|
| 703 | n/a | for (i = 0; i < DIM(meth); ++i) { |
|---|
| 704 | n/a | PyObject_SetAttrString(mod, meth[i].ml_name, |
|---|
| 705 | n/a | PyCFunction_New(&meth[i], NULL)); |
|---|
| 706 | n/a | } |
|---|
| 707 | n/a | } |
|---|
| 708 | n/a | g_Py_BuildValue = Py_BuildValue; |
|---|
| 709 | n/a | g_PyArg_ParseTuple = PyArg_ParseTuple; |
|---|
| 710 | n/a | g_PyErr_Format = PyErr_Format; |
|---|
| 711 | n/a | g_PyLong_FromVoidPtr = PyLong_FromVoidPtr; |
|---|
| 712 | n/a | |
|---|
| 713 | n/a | return 0; |
|---|
| 714 | n/a | } |
|---|
| 715 | n/a | |
|---|
| 716 | n/a | /* |
|---|
| 717 | n/a | * This function returns one of the following error codes: |
|---|
| 718 | n/a | * 1 if the Python-dll does not export the functions we need |
|---|
| 719 | n/a | * 2 if no install-script is specified in pathname |
|---|
| 720 | n/a | * 3 if the install-script file could not be opened |
|---|
| 721 | n/a | * the return value of PyRun_SimpleString() or Py_FinalizeEx() otherwise, |
|---|
| 722 | n/a | * which is 0 if everything is ok, -1 if an exception had occurred |
|---|
| 723 | n/a | * in the install-script. |
|---|
| 724 | n/a | */ |
|---|
| 725 | n/a | |
|---|
| 726 | n/a | static int |
|---|
| 727 | n/a | do_run_installscript(HINSTANCE hPython, char *pathname, int argc, char **argv) |
|---|
| 728 | n/a | { |
|---|
| 729 | n/a | int fh, result, i; |
|---|
| 730 | n/a | static wchar_t *wargv[256]; |
|---|
| 731 | n/a | DECLPROC(hPython, void, Py_Initialize, (void)); |
|---|
| 732 | n/a | DECLPROC(hPython, int, PySys_SetArgv, (int, wchar_t **)); |
|---|
| 733 | n/a | DECLPROC(hPython, int, PyRun_SimpleString, (char *)); |
|---|
| 734 | n/a | DECLPROC(hPython, int, Py_FinalizeEx, (void)); |
|---|
| 735 | n/a | DECLPROC(hPython, PyObject *, Py_BuildValue, (char *, ...)); |
|---|
| 736 | n/a | DECLPROC(hPython, PyObject *, PyCFunction_New, |
|---|
| 737 | n/a | (PyMethodDef *, PyObject *)); |
|---|
| 738 | n/a | DECLPROC(hPython, int, PyArg_ParseTuple, (PyObject *, char *, ...)); |
|---|
| 739 | n/a | DECLPROC(hPython, PyObject *, PyErr_Format, (PyObject *, char *)); |
|---|
| 740 | n/a | |
|---|
| 741 | n/a | if (!Py_Initialize || !PySys_SetArgv |
|---|
| 742 | n/a | || !PyRun_SimpleString || !Py_FinalizeEx) |
|---|
| 743 | n/a | return 1; |
|---|
| 744 | n/a | |
|---|
| 745 | n/a | if (!Py_BuildValue || !PyArg_ParseTuple || !PyErr_Format) |
|---|
| 746 | n/a | return 1; |
|---|
| 747 | n/a | |
|---|
| 748 | n/a | if (!PyCFunction_New || !PyArg_ParseTuple || !PyErr_Format) |
|---|
| 749 | n/a | return 1; |
|---|
| 750 | n/a | |
|---|
| 751 | n/a | if (pathname == NULL || pathname[0] == '\0') |
|---|
| 752 | n/a | return 2; |
|---|
| 753 | n/a | |
|---|
| 754 | n/a | fh = open(pathname, _O_RDONLY | O_NOINHERIT); |
|---|
| 755 | n/a | if (-1 == fh) { |
|---|
| 756 | n/a | fprintf(stderr, "Could not open postinstall-script %s\n", |
|---|
| 757 | n/a | pathname); |
|---|
| 758 | n/a | return 3; |
|---|
| 759 | n/a | } |
|---|
| 760 | n/a | |
|---|
| 761 | n/a | SetDlgItemText(hDialog, IDC_INFO, "Running Script..."); |
|---|
| 762 | n/a | |
|---|
| 763 | n/a | Py_Initialize(); |
|---|
| 764 | n/a | |
|---|
| 765 | n/a | prepare_script_environment(hPython); |
|---|
| 766 | n/a | // widen the argv array for py3k. |
|---|
| 767 | n/a | memset(wargv, 0, sizeof(wargv)); |
|---|
| 768 | n/a | for (i=0;i<argc;i++) |
|---|
| 769 | n/a | wargv[i] = argv[i] ? widen_string(argv[i]) : NULL; |
|---|
| 770 | n/a | PySys_SetArgv(argc, wargv); |
|---|
| 771 | n/a | // free the strings we just widened. |
|---|
| 772 | n/a | for (i=0;i<argc;i++) |
|---|
| 773 | n/a | if (wargv[i]) |
|---|
| 774 | n/a | free(wargv[i]); |
|---|
| 775 | n/a | |
|---|
| 776 | n/a | result = 3; |
|---|
| 777 | n/a | { |
|---|
| 778 | n/a | struct _stat statbuf; |
|---|
| 779 | n/a | if(0 == _fstat(fh, &statbuf)) { |
|---|
| 780 | n/a | char *script = alloca(statbuf.st_size + 5); |
|---|
| 781 | n/a | int n = read(fh, script, statbuf.st_size); |
|---|
| 782 | n/a | if (n > 0) { |
|---|
| 783 | n/a | script[n] = '\n'; |
|---|
| 784 | n/a | script[n+1] = 0; |
|---|
| 785 | n/a | result = PyRun_SimpleString(script); |
|---|
| 786 | n/a | } |
|---|
| 787 | n/a | } |
|---|
| 788 | n/a | } |
|---|
| 789 | n/a | if (Py_FinalizeEx() < 0) { |
|---|
| 790 | n/a | result = -1; |
|---|
| 791 | n/a | } |
|---|
| 792 | n/a | |
|---|
| 793 | n/a | close(fh); |
|---|
| 794 | n/a | return result; |
|---|
| 795 | n/a | } |
|---|
| 796 | n/a | |
|---|
| 797 | n/a | static int |
|---|
| 798 | n/a | run_installscript(char *pathname, int argc, char **argv, char **pOutput) |
|---|
| 799 | n/a | { |
|---|
| 800 | n/a | HINSTANCE hPython; |
|---|
| 801 | n/a | int result = 1; |
|---|
| 802 | n/a | int out_buf_size; |
|---|
| 803 | n/a | HANDLE redirected, old_stderr, old_stdout; |
|---|
| 804 | n/a | char *tempname; |
|---|
| 805 | n/a | |
|---|
| 806 | n/a | *pOutput = NULL; |
|---|
| 807 | n/a | |
|---|
| 808 | n/a | tempname = tempnam(NULL, NULL); |
|---|
| 809 | n/a | // We use a static CRT while the Python version we load uses |
|---|
| 810 | n/a | // the CRT from one of various possible DLLs. As a result we |
|---|
| 811 | n/a | // need to redirect the standard handles using the API rather |
|---|
| 812 | n/a | // than the CRT. |
|---|
| 813 | n/a | redirected = CreateFile( |
|---|
| 814 | n/a | tempname, |
|---|
| 815 | n/a | GENERIC_WRITE | GENERIC_READ, |
|---|
| 816 | n/a | FILE_SHARE_READ, |
|---|
| 817 | n/a | NULL, |
|---|
| 818 | n/a | CREATE_ALWAYS, |
|---|
| 819 | n/a | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, |
|---|
| 820 | n/a | NULL); |
|---|
| 821 | n/a | old_stdout = GetStdHandle(STD_OUTPUT_HANDLE); |
|---|
| 822 | n/a | old_stderr = GetStdHandle(STD_ERROR_HANDLE); |
|---|
| 823 | n/a | SetStdHandle(STD_OUTPUT_HANDLE, redirected); |
|---|
| 824 | n/a | SetStdHandle(STD_ERROR_HANDLE, redirected); |
|---|
| 825 | n/a | |
|---|
| 826 | n/a | hPython = LoadPythonDll(pythondll); |
|---|
| 827 | n/a | if (hPython) { |
|---|
| 828 | n/a | result = do_run_installscript(hPython, pathname, argc, argv); |
|---|
| 829 | n/a | FreeLibrary(hPython); |
|---|
| 830 | n/a | } else { |
|---|
| 831 | n/a | fprintf(stderr, "*** Could not load Python ***"); |
|---|
| 832 | n/a | } |
|---|
| 833 | n/a | SetStdHandle(STD_OUTPUT_HANDLE, old_stdout); |
|---|
| 834 | n/a | SetStdHandle(STD_ERROR_HANDLE, old_stderr); |
|---|
| 835 | n/a | out_buf_size = min(GetFileSize(redirected, NULL), 4096); |
|---|
| 836 | n/a | *pOutput = malloc(out_buf_size+1); |
|---|
| 837 | n/a | if (*pOutput) { |
|---|
| 838 | n/a | DWORD nread = 0; |
|---|
| 839 | n/a | SetFilePointer(redirected, 0, 0, FILE_BEGIN); |
|---|
| 840 | n/a | ReadFile(redirected, *pOutput, out_buf_size, &nread, NULL); |
|---|
| 841 | n/a | (*pOutput)[nread] = '\0'; |
|---|
| 842 | n/a | } |
|---|
| 843 | n/a | CloseHandle(redirected); |
|---|
| 844 | n/a | DeleteFile(tempname); |
|---|
| 845 | n/a | return result; |
|---|
| 846 | n/a | } |
|---|
| 847 | n/a | |
|---|
| 848 | n/a | static int do_run_simple_script(HINSTANCE hPython, char *script) |
|---|
| 849 | n/a | { |
|---|
| 850 | n/a | int rc; |
|---|
| 851 | n/a | DECLPROC(hPython, void, Py_Initialize, (void)); |
|---|
| 852 | n/a | DECLPROC(hPython, void, Py_SetProgramName, (wchar_t *)); |
|---|
| 853 | n/a | DECLPROC(hPython, int, Py_FinalizeEx, (void)); |
|---|
| 854 | n/a | DECLPROC(hPython, int, PyRun_SimpleString, (char *)); |
|---|
| 855 | n/a | DECLPROC(hPython, void, PyErr_Print, (void)); |
|---|
| 856 | n/a | |
|---|
| 857 | n/a | if (!Py_Initialize || !Py_SetProgramName || !Py_FinalizeEx || |
|---|
| 858 | n/a | !PyRun_SimpleString || !PyErr_Print) |
|---|
| 859 | n/a | return -1; |
|---|
| 860 | n/a | |
|---|
| 861 | n/a | Py_SetProgramName(wmodulename); |
|---|
| 862 | n/a | Py_Initialize(); |
|---|
| 863 | n/a | prepare_script_environment(hPython); |
|---|
| 864 | n/a | rc = PyRun_SimpleString(script); |
|---|
| 865 | n/a | if (rc) |
|---|
| 866 | n/a | PyErr_Print(); |
|---|
| 867 | n/a | if (Py_FinalizeEx() < 0) { |
|---|
| 868 | n/a | rc = -1; |
|---|
| 869 | n/a | } |
|---|
| 870 | n/a | return rc; |
|---|
| 871 | n/a | } |
|---|
| 872 | n/a | |
|---|
| 873 | n/a | static int run_simple_script(char *script) |
|---|
| 874 | n/a | { |
|---|
| 875 | n/a | int rc; |
|---|
| 876 | n/a | HINSTANCE hPython; |
|---|
| 877 | n/a | char *tempname = tempnam(NULL, NULL); |
|---|
| 878 | n/a | // Redirect output using win32 API - see comments above... |
|---|
| 879 | n/a | HANDLE redirected = CreateFile( |
|---|
| 880 | n/a | tempname, |
|---|
| 881 | n/a | GENERIC_WRITE | GENERIC_READ, |
|---|
| 882 | n/a | FILE_SHARE_READ, |
|---|
| 883 | n/a | NULL, |
|---|
| 884 | n/a | CREATE_ALWAYS, |
|---|
| 885 | n/a | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, |
|---|
| 886 | n/a | NULL); |
|---|
| 887 | n/a | HANDLE old_stdout = GetStdHandle(STD_OUTPUT_HANDLE); |
|---|
| 888 | n/a | HANDLE old_stderr = GetStdHandle(STD_ERROR_HANDLE); |
|---|
| 889 | n/a | SetStdHandle(STD_OUTPUT_HANDLE, redirected); |
|---|
| 890 | n/a | SetStdHandle(STD_ERROR_HANDLE, redirected); |
|---|
| 891 | n/a | |
|---|
| 892 | n/a | hPython = LoadPythonDll(pythondll); |
|---|
| 893 | n/a | if (!hPython) { |
|---|
| 894 | n/a | char reason[128]; |
|---|
| 895 | n/a | wsprintf(reason, "Can't load Python for pre-install script (%d)", GetLastError()); |
|---|
| 896 | n/a | set_failure_reason(reason); |
|---|
| 897 | n/a | return -1; |
|---|
| 898 | n/a | } |
|---|
| 899 | n/a | rc = do_run_simple_script(hPython, script); |
|---|
| 900 | n/a | FreeLibrary(hPython); |
|---|
| 901 | n/a | SetStdHandle(STD_OUTPUT_HANDLE, old_stdout); |
|---|
| 902 | n/a | SetStdHandle(STD_ERROR_HANDLE, old_stderr); |
|---|
| 903 | n/a | /* We only care about the output when we fail. If the script works |
|---|
| 904 | n/a | OK, then we discard it |
|---|
| 905 | n/a | */ |
|---|
| 906 | n/a | if (rc) { |
|---|
| 907 | n/a | int err_buf_size; |
|---|
| 908 | n/a | char *err_buf; |
|---|
| 909 | n/a | const char *prefix = "Running the pre-installation script failed\r\n"; |
|---|
| 910 | n/a | int prefix_len = strlen(prefix); |
|---|
| 911 | n/a | err_buf_size = GetFileSize(redirected, NULL); |
|---|
| 912 | n/a | if (err_buf_size==INVALID_FILE_SIZE) // an error - let's try anyway... |
|---|
| 913 | n/a | err_buf_size = 4096; |
|---|
| 914 | n/a | err_buf = malloc(prefix_len + err_buf_size + 1); |
|---|
| 915 | n/a | if (err_buf) { |
|---|
| 916 | n/a | DWORD n = 0; |
|---|
| 917 | n/a | strcpy(err_buf, prefix); |
|---|
| 918 | n/a | SetFilePointer(redirected, 0, 0, FILE_BEGIN); |
|---|
| 919 | n/a | ReadFile(redirected, err_buf+prefix_len, err_buf_size, &n, NULL); |
|---|
| 920 | n/a | err_buf[prefix_len+n] = '\0'; |
|---|
| 921 | n/a | set_failure_reason(err_buf); |
|---|
| 922 | n/a | free(err_buf); |
|---|
| 923 | n/a | } else { |
|---|
| 924 | n/a | set_failure_reason("Out of memory!"); |
|---|
| 925 | n/a | } |
|---|
| 926 | n/a | } |
|---|
| 927 | n/a | CloseHandle(redirected); |
|---|
| 928 | n/a | DeleteFile(tempname); |
|---|
| 929 | n/a | return rc; |
|---|
| 930 | n/a | } |
|---|
| 931 | n/a | |
|---|
| 932 | n/a | |
|---|
| 933 | n/a | static BOOL SystemError(int error, char *msg) |
|---|
| 934 | n/a | { |
|---|
| 935 | n/a | char Buffer[1024]; |
|---|
| 936 | n/a | int n; |
|---|
| 937 | n/a | |
|---|
| 938 | n/a | if (error) { |
|---|
| 939 | n/a | LPVOID lpMsgBuf; |
|---|
| 940 | n/a | FormatMessage( |
|---|
| 941 | n/a | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
|---|
| 942 | n/a | FORMAT_MESSAGE_FROM_SYSTEM, |
|---|
| 943 | n/a | NULL, |
|---|
| 944 | n/a | error, |
|---|
| 945 | n/a | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
|---|
| 946 | n/a | (LPSTR)&lpMsgBuf, |
|---|
| 947 | n/a | 0, |
|---|
| 948 | n/a | NULL |
|---|
| 949 | n/a | ); |
|---|
| 950 | n/a | strncpy(Buffer, lpMsgBuf, sizeof(Buffer)); |
|---|
| 951 | n/a | LocalFree(lpMsgBuf); |
|---|
| 952 | n/a | } else |
|---|
| 953 | n/a | Buffer[0] = '\0'; |
|---|
| 954 | n/a | n = lstrlen(Buffer); |
|---|
| 955 | n/a | _snprintf(Buffer+n, sizeof(Buffer)-n, msg); |
|---|
| 956 | n/a | MessageBox(hwndMain, Buffer, "Runtime Error", MB_OK | MB_ICONSTOP); |
|---|
| 957 | n/a | return FALSE; |
|---|
| 958 | n/a | } |
|---|
| 959 | n/a | |
|---|
| 960 | n/a | static BOOL notify (int code, char *fmt, ...) |
|---|
| 961 | n/a | { |
|---|
| 962 | n/a | char Buffer[1024]; |
|---|
| 963 | n/a | va_list marker; |
|---|
| 964 | n/a | BOOL result = TRUE; |
|---|
| 965 | n/a | int a, b; |
|---|
| 966 | n/a | char *cp; |
|---|
| 967 | n/a | |
|---|
| 968 | n/a | va_start(marker, fmt); |
|---|
| 969 | n/a | _vsnprintf(Buffer, sizeof(Buffer), fmt, marker); |
|---|
| 970 | n/a | |
|---|
| 971 | n/a | switch (code) { |
|---|
| 972 | n/a | /* Questions */ |
|---|
| 973 | n/a | case CAN_OVERWRITE: |
|---|
| 974 | n/a | break; |
|---|
| 975 | n/a | |
|---|
| 976 | n/a | /* Information notification */ |
|---|
| 977 | n/a | case DIR_CREATED: |
|---|
| 978 | n/a | if (logfile) |
|---|
| 979 | n/a | fprintf(logfile, "100 Made Dir: %s\n", fmt); |
|---|
| 980 | n/a | break; |
|---|
| 981 | n/a | |
|---|
| 982 | n/a | case FILE_CREATED: |
|---|
| 983 | n/a | if (logfile) |
|---|
| 984 | n/a | fprintf(logfile, "200 File Copy: %s\n", fmt); |
|---|
| 985 | n/a | goto add_to_filelist_label; |
|---|
| 986 | n/a | break; |
|---|
| 987 | n/a | |
|---|
| 988 | n/a | case FILE_OVERWRITTEN: |
|---|
| 989 | n/a | if (logfile) |
|---|
| 990 | n/a | fprintf(logfile, "200 File Overwrite: %s\n", fmt); |
|---|
| 991 | n/a | add_to_filelist_label: |
|---|
| 992 | n/a | if ((cp = strrchr(fmt, '.')) && (0 == strcmp (cp, ".py"))) |
|---|
| 993 | n/a | add_to_filelist(fmt); |
|---|
| 994 | n/a | break; |
|---|
| 995 | n/a | |
|---|
| 996 | n/a | /* Error Messages */ |
|---|
| 997 | n/a | case ZLIB_ERROR: |
|---|
| 998 | n/a | MessageBox(GetFocus(), Buffer, "Error", |
|---|
| 999 | n/a | MB_OK | MB_ICONWARNING); |
|---|
| 1000 | n/a | break; |
|---|
| 1001 | n/a | |
|---|
| 1002 | n/a | case SYSTEM_ERROR: |
|---|
| 1003 | n/a | SystemError(GetLastError(), Buffer); |
|---|
| 1004 | n/a | break; |
|---|
| 1005 | n/a | |
|---|
| 1006 | n/a | case NUM_FILES: |
|---|
| 1007 | n/a | a = va_arg(marker, int); |
|---|
| 1008 | n/a | b = va_arg(marker, int); |
|---|
| 1009 | n/a | SendMessage(hDialog, WM_NUMFILES, 0, MAKELPARAM(0, a)); |
|---|
| 1010 | n/a | SendMessage(hDialog, WM_NEXTFILE, b,(LPARAM)fmt); |
|---|
| 1011 | n/a | } |
|---|
| 1012 | n/a | va_end(marker); |
|---|
| 1013 | n/a | |
|---|
| 1014 | n/a | return result; |
|---|
| 1015 | n/a | } |
|---|
| 1016 | n/a | |
|---|
| 1017 | n/a | static char *MapExistingFile(char *pathname, DWORD *psize) |
|---|
| 1018 | n/a | { |
|---|
| 1019 | n/a | HANDLE hFile, hFileMapping; |
|---|
| 1020 | n/a | DWORD nSizeLow, nSizeHigh; |
|---|
| 1021 | n/a | char *data; |
|---|
| 1022 | n/a | |
|---|
| 1023 | n/a | hFile = CreateFile(pathname, |
|---|
| 1024 | n/a | GENERIC_READ, FILE_SHARE_READ, NULL, |
|---|
| 1025 | n/a | OPEN_EXISTING, |
|---|
| 1026 | n/a | FILE_ATTRIBUTE_NORMAL, NULL); |
|---|
| 1027 | n/a | if (hFile == INVALID_HANDLE_VALUE) |
|---|
| 1028 | n/a | return NULL; |
|---|
| 1029 | n/a | nSizeLow = GetFileSize(hFile, &nSizeHigh); |
|---|
| 1030 | n/a | hFileMapping = CreateFileMapping(hFile, |
|---|
| 1031 | n/a | NULL, PAGE_READONLY, 0, 0, NULL); |
|---|
| 1032 | n/a | CloseHandle(hFile); |
|---|
| 1033 | n/a | |
|---|
| 1034 | n/a | if (hFileMapping == NULL) |
|---|
| 1035 | n/a | return NULL; |
|---|
| 1036 | n/a | |
|---|
| 1037 | n/a | data = MapViewOfFile(hFileMapping, |
|---|
| 1038 | n/a | FILE_MAP_READ, 0, 0, 0); |
|---|
| 1039 | n/a | |
|---|
| 1040 | n/a | CloseHandle(hFileMapping); |
|---|
| 1041 | n/a | *psize = nSizeLow; |
|---|
| 1042 | n/a | return data; |
|---|
| 1043 | n/a | } |
|---|
| 1044 | n/a | |
|---|
| 1045 | n/a | |
|---|
| 1046 | n/a | static void create_bitmap(HWND hwnd) |
|---|
| 1047 | n/a | { |
|---|
| 1048 | n/a | BITMAPFILEHEADER *bfh; |
|---|
| 1049 | n/a | BITMAPINFO *bi; |
|---|
| 1050 | n/a | HDC hdc; |
|---|
| 1051 | n/a | |
|---|
| 1052 | n/a | if (!bitmap_bytes) |
|---|
| 1053 | n/a | return; |
|---|
| 1054 | n/a | |
|---|
| 1055 | n/a | if (hBitmap) |
|---|
| 1056 | n/a | return; |
|---|
| 1057 | n/a | |
|---|
| 1058 | n/a | hdc = GetDC(hwnd); |
|---|
| 1059 | n/a | |
|---|
| 1060 | n/a | bfh = (BITMAPFILEHEADER *)bitmap_bytes; |
|---|
| 1061 | n/a | bi = (BITMAPINFO *)(bitmap_bytes + sizeof(BITMAPFILEHEADER)); |
|---|
| 1062 | n/a | |
|---|
| 1063 | n/a | hBitmap = CreateDIBitmap(hdc, |
|---|
| 1064 | n/a | &bi->bmiHeader, |
|---|
| 1065 | n/a | CBM_INIT, |
|---|
| 1066 | n/a | bitmap_bytes + bfh->bfOffBits, |
|---|
| 1067 | n/a | bi, |
|---|
| 1068 | n/a | DIB_RGB_COLORS); |
|---|
| 1069 | n/a | ReleaseDC(hwnd, hdc); |
|---|
| 1070 | n/a | } |
|---|
| 1071 | n/a | |
|---|
| 1072 | n/a | /* Extract everything we need to begin the installation. Currently this |
|---|
| 1073 | n/a | is the INI filename with install data, and the raw pre-install script |
|---|
| 1074 | n/a | */ |
|---|
| 1075 | n/a | static BOOL ExtractInstallData(char *data, DWORD size, int *pexe_size, |
|---|
| 1076 | n/a | char **out_ini_file, char **out_preinstall_script) |
|---|
| 1077 | n/a | { |
|---|
| 1078 | n/a | /* read the end of central directory record */ |
|---|
| 1079 | n/a | struct eof_cdir *pe = (struct eof_cdir *)&data[size - sizeof |
|---|
| 1080 | n/a | (struct eof_cdir)]; |
|---|
| 1081 | n/a | |
|---|
| 1082 | n/a | int arc_start = size - sizeof (struct eof_cdir) - pe->nBytesCDir - |
|---|
| 1083 | n/a | pe->ofsCDir; |
|---|
| 1084 | n/a | |
|---|
| 1085 | n/a | int ofs = arc_start - sizeof (struct meta_data_hdr); |
|---|
| 1086 | n/a | |
|---|
| 1087 | n/a | /* read meta_data info */ |
|---|
| 1088 | n/a | struct meta_data_hdr *pmd = (struct meta_data_hdr *)&data[ofs]; |
|---|
| 1089 | n/a | char *src, *dst; |
|---|
| 1090 | n/a | char *ini_file; |
|---|
| 1091 | n/a | char tempdir[MAX_PATH]; |
|---|
| 1092 | n/a | |
|---|
| 1093 | n/a | /* ensure that if we fail, we don't have garbage out pointers */ |
|---|
| 1094 | n/a | *out_ini_file = *out_preinstall_script = NULL; |
|---|
| 1095 | n/a | |
|---|
| 1096 | n/a | if (pe->tag != 0x06054b50) { |
|---|
| 1097 | n/a | return FALSE; |
|---|
| 1098 | n/a | } |
|---|
| 1099 | n/a | |
|---|
| 1100 | n/a | if (pmd->tag != 0x1234567B) { |
|---|
| 1101 | n/a | return SystemError(0, |
|---|
| 1102 | n/a | "Invalid cfgdata magic number (see bdist_wininst.py)"); |
|---|
| 1103 | n/a | } |
|---|
| 1104 | n/a | if (ofs < 0) { |
|---|
| 1105 | n/a | return FALSE; |
|---|
| 1106 | n/a | } |
|---|
| 1107 | n/a | |
|---|
| 1108 | n/a | if (pmd->bitmap_size) { |
|---|
| 1109 | n/a | /* Store pointer to bitmap bytes */ |
|---|
| 1110 | n/a | bitmap_bytes = (char *)pmd - pmd->uncomp_size - pmd->bitmap_size; |
|---|
| 1111 | n/a | } |
|---|
| 1112 | n/a | |
|---|
| 1113 | n/a | *pexe_size = ofs - pmd->uncomp_size - pmd->bitmap_size; |
|---|
| 1114 | n/a | |
|---|
| 1115 | n/a | src = ((char *)pmd) - pmd->uncomp_size; |
|---|
| 1116 | n/a | ini_file = malloc(MAX_PATH); /* will be returned, so do not free it */ |
|---|
| 1117 | n/a | if (!ini_file) |
|---|
| 1118 | n/a | return FALSE; |
|---|
| 1119 | n/a | if (!GetTempPath(sizeof(tempdir), tempdir) |
|---|
| 1120 | n/a | || !GetTempFileName(tempdir, "~du", 0, ini_file)) { |
|---|
| 1121 | n/a | SystemError(GetLastError(), |
|---|
| 1122 | n/a | "Could not create temporary file"); |
|---|
| 1123 | n/a | return FALSE; |
|---|
| 1124 | n/a | } |
|---|
| 1125 | n/a | |
|---|
| 1126 | n/a | dst = map_new_file(CREATE_ALWAYS, ini_file, NULL, pmd->uncomp_size, |
|---|
| 1127 | n/a | 0, 0, NULL/*notify*/); |
|---|
| 1128 | n/a | if (!dst) |
|---|
| 1129 | n/a | return FALSE; |
|---|
| 1130 | n/a | /* Up to the first \0 is the INI file data. */ |
|---|
| 1131 | n/a | strncpy(dst, src, pmd->uncomp_size); |
|---|
| 1132 | n/a | src += strlen(dst) + 1; |
|---|
| 1133 | n/a | /* Up to next \0 is the pre-install script */ |
|---|
| 1134 | n/a | *out_preinstall_script = strdup(src); |
|---|
| 1135 | n/a | *out_ini_file = ini_file; |
|---|
| 1136 | n/a | UnmapViewOfFile(dst); |
|---|
| 1137 | n/a | return TRUE; |
|---|
| 1138 | n/a | } |
|---|
| 1139 | n/a | |
|---|
| 1140 | n/a | static void PumpMessages(void) |
|---|
| 1141 | n/a | { |
|---|
| 1142 | n/a | MSG msg; |
|---|
| 1143 | n/a | while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { |
|---|
| 1144 | n/a | TranslateMessage(&msg); |
|---|
| 1145 | n/a | DispatchMessage(&msg); |
|---|
| 1146 | n/a | } |
|---|
| 1147 | n/a | } |
|---|
| 1148 | n/a | |
|---|
| 1149 | n/a | LRESULT CALLBACK |
|---|
| 1150 | n/a | WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) |
|---|
| 1151 | n/a | { |
|---|
| 1152 | n/a | HDC hdc; |
|---|
| 1153 | n/a | HFONT hFont; |
|---|
| 1154 | n/a | int h; |
|---|
| 1155 | n/a | PAINTSTRUCT ps; |
|---|
| 1156 | n/a | switch (msg) { |
|---|
| 1157 | n/a | case WM_PAINT: |
|---|
| 1158 | n/a | hdc = BeginPaint(hwnd, &ps); |
|---|
| 1159 | n/a | h = GetSystemMetrics(SM_CYSCREEN) / 10; |
|---|
| 1160 | n/a | hFont = CreateFont(h, 0, 0, 0, 700, TRUE, |
|---|
| 1161 | n/a | 0, 0, 0, 0, 0, 0, 0, "Times Roman"); |
|---|
| 1162 | n/a | hFont = SelectObject(hdc, hFont); |
|---|
| 1163 | n/a | SetBkMode(hdc, TRANSPARENT); |
|---|
| 1164 | n/a | TextOut(hdc, 15, 15, title, strlen(title)); |
|---|
| 1165 | n/a | SetTextColor(hdc, RGB(255, 255, 255)); |
|---|
| 1166 | n/a | TextOut(hdc, 10, 10, title, strlen(title)); |
|---|
| 1167 | n/a | DeleteObject(SelectObject(hdc, hFont)); |
|---|
| 1168 | n/a | EndPaint(hwnd, &ps); |
|---|
| 1169 | n/a | return 0; |
|---|
| 1170 | n/a | } |
|---|
| 1171 | n/a | return DefWindowProc(hwnd, msg, wParam, lParam); |
|---|
| 1172 | n/a | } |
|---|
| 1173 | n/a | |
|---|
| 1174 | n/a | static HWND CreateBackground(char *title) |
|---|
| 1175 | n/a | { |
|---|
| 1176 | n/a | WNDCLASS wc; |
|---|
| 1177 | n/a | HWND hwnd; |
|---|
| 1178 | n/a | char buffer[4096]; |
|---|
| 1179 | n/a | |
|---|
| 1180 | n/a | wc.style = CS_VREDRAW | CS_HREDRAW; |
|---|
| 1181 | n/a | wc.lpfnWndProc = WindowProc; |
|---|
| 1182 | n/a | wc.cbWndExtra = 0; |
|---|
| 1183 | n/a | wc.cbClsExtra = 0; |
|---|
| 1184 | n/a | wc.hInstance = GetModuleHandle(NULL); |
|---|
| 1185 | n/a | wc.hIcon = NULL; |
|---|
| 1186 | n/a | wc.hCursor = LoadCursor(NULL, IDC_ARROW); |
|---|
| 1187 | n/a | wc.hbrBackground = CreateSolidBrush(RGB(0, 0, 128)); |
|---|
| 1188 | n/a | wc.lpszMenuName = NULL; |
|---|
| 1189 | n/a | wc.lpszClassName = "SetupWindowClass"; |
|---|
| 1190 | n/a | |
|---|
| 1191 | n/a | if (!RegisterClass(&wc)) |
|---|
| 1192 | n/a | MessageBox(hwndMain, |
|---|
| 1193 | n/a | "Could not register window class", |
|---|
| 1194 | n/a | "Setup.exe", MB_OK); |
|---|
| 1195 | n/a | |
|---|
| 1196 | n/a | wsprintf(buffer, "Setup %s", title); |
|---|
| 1197 | n/a | hwnd = CreateWindow("SetupWindowClass", |
|---|
| 1198 | n/a | buffer, |
|---|
| 1199 | n/a | 0, |
|---|
| 1200 | n/a | 0, 0, |
|---|
| 1201 | n/a | GetSystemMetrics(SM_CXFULLSCREEN), |
|---|
| 1202 | n/a | GetSystemMetrics(SM_CYFULLSCREEN), |
|---|
| 1203 | n/a | NULL, |
|---|
| 1204 | n/a | NULL, |
|---|
| 1205 | n/a | GetModuleHandle(NULL), |
|---|
| 1206 | n/a | NULL); |
|---|
| 1207 | n/a | ShowWindow(hwnd, SW_SHOWMAXIMIZED); |
|---|
| 1208 | n/a | UpdateWindow(hwnd); |
|---|
| 1209 | n/a | return hwnd; |
|---|
| 1210 | n/a | } |
|---|
| 1211 | n/a | |
|---|
| 1212 | n/a | /* |
|---|
| 1213 | n/a | * Center a window on the screen |
|---|
| 1214 | n/a | */ |
|---|
| 1215 | n/a | static void CenterWindow(HWND hwnd) |
|---|
| 1216 | n/a | { |
|---|
| 1217 | n/a | RECT rc; |
|---|
| 1218 | n/a | int w, h; |
|---|
| 1219 | n/a | |
|---|
| 1220 | n/a | GetWindowRect(hwnd, &rc); |
|---|
| 1221 | n/a | w = GetSystemMetrics(SM_CXSCREEN); |
|---|
| 1222 | n/a | h = GetSystemMetrics(SM_CYSCREEN); |
|---|
| 1223 | n/a | MoveWindow(hwnd, |
|---|
| 1224 | n/a | (w - (rc.right-rc.left))/2, |
|---|
| 1225 | n/a | (h - (rc.bottom-rc.top))/2, |
|---|
| 1226 | n/a | rc.right-rc.left, rc.bottom-rc.top, FALSE); |
|---|
| 1227 | n/a | } |
|---|
| 1228 | n/a | |
|---|
| 1229 | n/a | #include <prsht.h> |
|---|
| 1230 | n/a | |
|---|
| 1231 | n/a | INT_PTR CALLBACK |
|---|
| 1232 | n/a | IntroDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) |
|---|
| 1233 | n/a | { |
|---|
| 1234 | n/a | LPNMHDR lpnm; |
|---|
| 1235 | n/a | char Buffer[4096]; |
|---|
| 1236 | n/a | |
|---|
| 1237 | n/a | switch (msg) { |
|---|
| 1238 | n/a | case WM_INITDIALOG: |
|---|
| 1239 | n/a | create_bitmap(hwnd); |
|---|
| 1240 | n/a | if(hBitmap) |
|---|
| 1241 | n/a | SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE, |
|---|
| 1242 | n/a | IMAGE_BITMAP, (LPARAM)hBitmap); |
|---|
| 1243 | n/a | CenterWindow(GetParent(hwnd)); |
|---|
| 1244 | n/a | wsprintf(Buffer, |
|---|
| 1245 | n/a | "This Wizard will install %s on your computer. " |
|---|
| 1246 | n/a | "Click Next to continue " |
|---|
| 1247 | n/a | "or Cancel to exit the Setup Wizard.", |
|---|
| 1248 | n/a | meta_name); |
|---|
| 1249 | n/a | SetDlgItemText(hwnd, IDC_TITLE, Buffer); |
|---|
| 1250 | n/a | SetDlgItemText(hwnd, IDC_INTRO_TEXT, info); |
|---|
| 1251 | n/a | SetDlgItemText(hwnd, IDC_BUILD_INFO, build_info); |
|---|
| 1252 | n/a | return FALSE; |
|---|
| 1253 | n/a | |
|---|
| 1254 | n/a | case WM_NOTIFY: |
|---|
| 1255 | n/a | lpnm = (LPNMHDR) lParam; |
|---|
| 1256 | n/a | |
|---|
| 1257 | n/a | switch (lpnm->code) { |
|---|
| 1258 | n/a | case PSN_SETACTIVE: |
|---|
| 1259 | n/a | PropSheet_SetWizButtons(GetParent(hwnd), PSWIZB_NEXT); |
|---|
| 1260 | n/a | break; |
|---|
| 1261 | n/a | |
|---|
| 1262 | n/a | case PSN_WIZNEXT: |
|---|
| 1263 | n/a | break; |
|---|
| 1264 | n/a | |
|---|
| 1265 | n/a | case PSN_RESET: |
|---|
| 1266 | n/a | break; |
|---|
| 1267 | n/a | |
|---|
| 1268 | n/a | default: |
|---|
| 1269 | n/a | break; |
|---|
| 1270 | n/a | } |
|---|
| 1271 | n/a | } |
|---|
| 1272 | n/a | return FALSE; |
|---|
| 1273 | n/a | } |
|---|
| 1274 | n/a | |
|---|
| 1275 | n/a | #ifdef USE_OTHER_PYTHON_VERSIONS |
|---|
| 1276 | n/a | /* These are really private variables used to communicate |
|---|
| 1277 | n/a | * between StatusRoutine and CheckPythonExe |
|---|
| 1278 | n/a | */ |
|---|
| 1279 | n/a | char bound_image_dll[_MAX_PATH]; |
|---|
| 1280 | n/a | int bound_image_major; |
|---|
| 1281 | n/a | int bound_image_minor; |
|---|
| 1282 | n/a | |
|---|
| 1283 | n/a | static BOOL __stdcall StatusRoutine(IMAGEHLP_STATUS_REASON reason, |
|---|
| 1284 | n/a | PSTR ImageName, |
|---|
| 1285 | n/a | PSTR DllName, |
|---|
| 1286 | n/a | ULONG Va, |
|---|
| 1287 | n/a | ULONG Parameter) |
|---|
| 1288 | n/a | { |
|---|
| 1289 | n/a | char fname[_MAX_PATH]; |
|---|
| 1290 | n/a | int int_version; |
|---|
| 1291 | n/a | |
|---|
| 1292 | n/a | switch(reason) { |
|---|
| 1293 | n/a | case BindOutOfMemory: |
|---|
| 1294 | n/a | case BindRvaToVaFailed: |
|---|
| 1295 | n/a | case BindNoRoomInImage: |
|---|
| 1296 | n/a | case BindImportProcedureFailed: |
|---|
| 1297 | n/a | break; |
|---|
| 1298 | n/a | |
|---|
| 1299 | n/a | case BindImportProcedure: |
|---|
| 1300 | n/a | case BindForwarder: |
|---|
| 1301 | n/a | case BindForwarderNOT: |
|---|
| 1302 | n/a | case BindImageModified: |
|---|
| 1303 | n/a | case BindExpandFileHeaders: |
|---|
| 1304 | n/a | case BindImageComplete: |
|---|
| 1305 | n/a | case BindSymbolsNotUpdated: |
|---|
| 1306 | n/a | case BindMismatchedSymbols: |
|---|
| 1307 | n/a | case BindImportModuleFailed: |
|---|
| 1308 | n/a | break; |
|---|
| 1309 | n/a | |
|---|
| 1310 | n/a | case BindImportModule: |
|---|
| 1311 | n/a | if (1 == sscanf(DllName, "python%d", &int_version)) { |
|---|
| 1312 | n/a | SearchPath(NULL, DllName, NULL, sizeof(fname), |
|---|
| 1313 | n/a | fname, NULL); |
|---|
| 1314 | n/a | strcpy(bound_image_dll, fname); |
|---|
| 1315 | n/a | bound_image_major = int_version / 10; |
|---|
| 1316 | n/a | bound_image_minor = int_version % 10; |
|---|
| 1317 | n/a | OutputDebugString("BOUND "); |
|---|
| 1318 | n/a | OutputDebugString(fname); |
|---|
| 1319 | n/a | OutputDebugString("\n"); |
|---|
| 1320 | n/a | } |
|---|
| 1321 | n/a | break; |
|---|
| 1322 | n/a | } |
|---|
| 1323 | n/a | return TRUE; |
|---|
| 1324 | n/a | } |
|---|
| 1325 | n/a | |
|---|
| 1326 | n/a | /* |
|---|
| 1327 | n/a | */ |
|---|
| 1328 | n/a | static LPSTR get_sys_prefix(LPSTR exe, LPSTR dll) |
|---|
| 1329 | n/a | { |
|---|
| 1330 | n/a | void (__cdecl * Py_Initialize)(void); |
|---|
| 1331 | n/a | void (__cdecl * Py_SetProgramName)(char *); |
|---|
| 1332 | n/a | void (__cdecl * Py_Finalize)(void); |
|---|
| 1333 | n/a | void* (__cdecl * PySys_GetObject)(char *); |
|---|
| 1334 | n/a | void (__cdecl * PySys_SetArgv)(int, char **); |
|---|
| 1335 | n/a | char* (__cdecl * Py_GetPrefix)(void); |
|---|
| 1336 | n/a | char* (__cdecl * Py_GetPath)(void); |
|---|
| 1337 | n/a | HINSTANCE hPython; |
|---|
| 1338 | n/a | LPSTR prefix = NULL; |
|---|
| 1339 | n/a | int (__cdecl * PyRun_SimpleString)(char *); |
|---|
| 1340 | n/a | |
|---|
| 1341 | n/a | { |
|---|
| 1342 | n/a | char Buffer[256]; |
|---|
| 1343 | n/a | wsprintf(Buffer, "PYTHONHOME=%s", exe); |
|---|
| 1344 | n/a | *strrchr(Buffer, '\\') = '\0'; |
|---|
| 1345 | n/a | // MessageBox(GetFocus(), Buffer, "PYTHONHOME", MB_OK); |
|---|
| 1346 | n/a | _putenv(Buffer); |
|---|
| 1347 | n/a | _putenv("PYTHONPATH="); |
|---|
| 1348 | n/a | } |
|---|
| 1349 | n/a | |
|---|
| 1350 | n/a | hPython = LoadLibrary(dll); |
|---|
| 1351 | n/a | if (!hPython) |
|---|
| 1352 | n/a | return NULL; |
|---|
| 1353 | n/a | Py_Initialize = (void (*)(void))GetProcAddress |
|---|
| 1354 | n/a | (hPython,"Py_Initialize"); |
|---|
| 1355 | n/a | |
|---|
| 1356 | n/a | PySys_SetArgv = (void (*)(int, char **))GetProcAddress |
|---|
| 1357 | n/a | (hPython,"PySys_SetArgv"); |
|---|
| 1358 | n/a | |
|---|
| 1359 | n/a | PyRun_SimpleString = (int (*)(char *))GetProcAddress |
|---|
| 1360 | n/a | (hPython,"PyRun_SimpleString"); |
|---|
| 1361 | n/a | |
|---|
| 1362 | n/a | Py_SetProgramName = (void (*)(char *))GetProcAddress |
|---|
| 1363 | n/a | (hPython,"Py_SetProgramName"); |
|---|
| 1364 | n/a | |
|---|
| 1365 | n/a | PySys_GetObject = (void* (*)(char *))GetProcAddress |
|---|
| 1366 | n/a | (hPython,"PySys_GetObject"); |
|---|
| 1367 | n/a | |
|---|
| 1368 | n/a | Py_GetPrefix = (char * (*)(void))GetProcAddress |
|---|
| 1369 | n/a | (hPython,"Py_GetPrefix"); |
|---|
| 1370 | n/a | |
|---|
| 1371 | n/a | Py_GetPath = (char * (*)(void))GetProcAddress |
|---|
| 1372 | n/a | (hPython,"Py_GetPath"); |
|---|
| 1373 | n/a | |
|---|
| 1374 | n/a | Py_Finalize = (void (*)(void))GetProcAddress(hPython, |
|---|
| 1375 | n/a | "Py_Finalize"); |
|---|
| 1376 | n/a | Py_SetProgramName(exe); |
|---|
| 1377 | n/a | Py_Initialize(); |
|---|
| 1378 | n/a | PySys_SetArgv(1, &exe); |
|---|
| 1379 | n/a | |
|---|
| 1380 | n/a | MessageBox(GetFocus(), Py_GetPrefix(), "PREFIX", MB_OK); |
|---|
| 1381 | n/a | MessageBox(GetFocus(), Py_GetPath(), "PATH", MB_OK); |
|---|
| 1382 | n/a | |
|---|
| 1383 | n/a | Py_Finalize(); |
|---|
| 1384 | n/a | FreeLibrary(hPython); |
|---|
| 1385 | n/a | |
|---|
| 1386 | n/a | return prefix; |
|---|
| 1387 | n/a | } |
|---|
| 1388 | n/a | |
|---|
| 1389 | n/a | static BOOL |
|---|
| 1390 | n/a | CheckPythonExe(LPSTR pathname, LPSTR version, int *pmajor, int *pminor) |
|---|
| 1391 | n/a | { |
|---|
| 1392 | n/a | bound_image_dll[0] = '\0'; |
|---|
| 1393 | n/a | if (!BindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES, |
|---|
| 1394 | n/a | pathname, |
|---|
| 1395 | n/a | NULL, |
|---|
| 1396 | n/a | NULL, |
|---|
| 1397 | n/a | StatusRoutine)) |
|---|
| 1398 | n/a | return SystemError(0, "Could not bind image"); |
|---|
| 1399 | n/a | if (bound_image_dll[0] == '\0') |
|---|
| 1400 | n/a | return SystemError(0, "Does not seem to be a python executable"); |
|---|
| 1401 | n/a | *pmajor = bound_image_major; |
|---|
| 1402 | n/a | *pminor = bound_image_minor; |
|---|
| 1403 | n/a | if (version && *version) { |
|---|
| 1404 | n/a | char core_version[12]; |
|---|
| 1405 | n/a | wsprintf(core_version, "%d.%d", bound_image_major, bound_image_minor); |
|---|
| 1406 | n/a | if (strcmp(version, core_version)) |
|---|
| 1407 | n/a | return SystemError(0, "Wrong Python version"); |
|---|
| 1408 | n/a | } |
|---|
| 1409 | n/a | get_sys_prefix(pathname, bound_image_dll); |
|---|
| 1410 | n/a | return TRUE; |
|---|
| 1411 | n/a | } |
|---|
| 1412 | n/a | |
|---|
| 1413 | n/a | /* |
|---|
| 1414 | n/a | * Browse for other python versions. Insert it into the listbox specified |
|---|
| 1415 | n/a | * by hwnd. version, if not NULL or empty, is the version required. |
|---|
| 1416 | n/a | */ |
|---|
| 1417 | n/a | static BOOL GetOtherPythonVersion(HWND hwnd, LPSTR version) |
|---|
| 1418 | n/a | { |
|---|
| 1419 | n/a | char vers_name[_MAX_PATH + 80]; |
|---|
| 1420 | n/a | DWORD itemindex; |
|---|
| 1421 | n/a | OPENFILENAME of; |
|---|
| 1422 | n/a | char pathname[_MAX_PATH]; |
|---|
| 1423 | n/a | DWORD result; |
|---|
| 1424 | n/a | |
|---|
| 1425 | n/a | strcpy(pathname, "python.exe"); |
|---|
| 1426 | n/a | |
|---|
| 1427 | n/a | memset(&of, 0, sizeof(of)); |
|---|
| 1428 | n/a | of.lStructSize = sizeof(OPENFILENAME); |
|---|
| 1429 | n/a | of.hwndOwner = GetParent(hwnd); |
|---|
| 1430 | n/a | of.hInstance = NULL; |
|---|
| 1431 | n/a | of.lpstrFilter = "python.exe\0python.exe\0"; |
|---|
| 1432 | n/a | of.lpstrCustomFilter = NULL; |
|---|
| 1433 | n/a | of.nMaxCustFilter = 0; |
|---|
| 1434 | n/a | of.nFilterIndex = 1; |
|---|
| 1435 | n/a | of.lpstrFile = pathname; |
|---|
| 1436 | n/a | of.nMaxFile = sizeof(pathname); |
|---|
| 1437 | n/a | of.lpstrFileTitle = NULL; |
|---|
| 1438 | n/a | of.nMaxFileTitle = 0; |
|---|
| 1439 | n/a | of.lpstrInitialDir = NULL; |
|---|
| 1440 | n/a | of.lpstrTitle = "Python executable"; |
|---|
| 1441 | n/a | of.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST; |
|---|
| 1442 | n/a | of.lpstrDefExt = "exe"; |
|---|
| 1443 | n/a | |
|---|
| 1444 | n/a | result = GetOpenFileName(&of); |
|---|
| 1445 | n/a | if (result) { |
|---|
| 1446 | n/a | int major, minor; |
|---|
| 1447 | n/a | if (!CheckPythonExe(pathname, version, &major, &minor)) { |
|---|
| 1448 | n/a | return FALSE; |
|---|
| 1449 | n/a | } |
|---|
| 1450 | n/a | *strrchr(pathname, '\\') = '\0'; |
|---|
| 1451 | n/a | wsprintf(vers_name, "Python Version %d.%d in %s", |
|---|
| 1452 | n/a | major, minor, pathname); |
|---|
| 1453 | n/a | itemindex = SendMessage(hwnd, LB_INSERTSTRING, -1, |
|---|
| 1454 | n/a | (LPARAM)(LPSTR)vers_name); |
|---|
| 1455 | n/a | SendMessage(hwnd, LB_SETCURSEL, itemindex, 0); |
|---|
| 1456 | n/a | SendMessage(hwnd, LB_SETITEMDATA, itemindex, |
|---|
| 1457 | n/a | (LPARAM)(LPSTR)strdup(pathname)); |
|---|
| 1458 | n/a | return TRUE; |
|---|
| 1459 | n/a | } |
|---|
| 1460 | n/a | return FALSE; |
|---|
| 1461 | n/a | } |
|---|
| 1462 | n/a | #endif /* USE_OTHER_PYTHON_VERSIONS */ |
|---|
| 1463 | n/a | |
|---|
| 1464 | n/a | typedef struct _InstalledVersionInfo { |
|---|
| 1465 | n/a | char prefix[MAX_PATH+1]; // sys.prefix directory. |
|---|
| 1466 | n/a | HKEY hkey; // Is this Python in HKCU or HKLM? |
|---|
| 1467 | n/a | } InstalledVersionInfo; |
|---|
| 1468 | n/a | |
|---|
| 1469 | n/a | |
|---|
| 1470 | n/a | /* |
|---|
| 1471 | n/a | * Fill the listbox specified by hwnd with all python versions found |
|---|
| 1472 | n/a | * in the registry. version, if not NULL or empty, is the version |
|---|
| 1473 | n/a | * required. |
|---|
| 1474 | n/a | */ |
|---|
| 1475 | n/a | static BOOL GetPythonVersions(HWND hwnd, HKEY hkRoot, LPSTR version) |
|---|
| 1476 | n/a | { |
|---|
| 1477 | n/a | DWORD index = 0; |
|---|
| 1478 | n/a | char core_version[80]; |
|---|
| 1479 | n/a | HKEY hKey; |
|---|
| 1480 | n/a | BOOL result = TRUE; |
|---|
| 1481 | n/a | DWORD bufsize; |
|---|
| 1482 | n/a | |
|---|
| 1483 | n/a | if (ERROR_SUCCESS != RegOpenKeyEx(hkRoot, |
|---|
| 1484 | n/a | "Software\\Python\\PythonCore", |
|---|
| 1485 | n/a | 0, KEY_READ, &hKey)) |
|---|
| 1486 | n/a | return FALSE; |
|---|
| 1487 | n/a | bufsize = sizeof(core_version); |
|---|
| 1488 | n/a | while (ERROR_SUCCESS == RegEnumKeyEx(hKey, index, |
|---|
| 1489 | n/a | core_version, &bufsize, NULL, |
|---|
| 1490 | n/a | NULL, NULL, NULL)) { |
|---|
| 1491 | n/a | char subkey_name[80], vers_name[80]; |
|---|
| 1492 | n/a | int itemindex; |
|---|
| 1493 | n/a | DWORD value_size; |
|---|
| 1494 | n/a | HKEY hk; |
|---|
| 1495 | n/a | |
|---|
| 1496 | n/a | bufsize = sizeof(core_version); |
|---|
| 1497 | n/a | ++index; |
|---|
| 1498 | n/a | if (version && *version && strcmp(version, core_version)) |
|---|
| 1499 | n/a | continue; |
|---|
| 1500 | n/a | |
|---|
| 1501 | n/a | wsprintf(vers_name, "Python Version %s (found in registry)", |
|---|
| 1502 | n/a | core_version); |
|---|
| 1503 | n/a | wsprintf(subkey_name, |
|---|
| 1504 | n/a | "Software\\Python\\PythonCore\\%s\\InstallPath", |
|---|
| 1505 | n/a | core_version); |
|---|
| 1506 | n/a | if (ERROR_SUCCESS == RegOpenKeyEx(hkRoot, subkey_name, 0, KEY_READ, &hk)) { |
|---|
| 1507 | n/a | InstalledVersionInfo *ivi = |
|---|
| 1508 | n/a | (InstalledVersionInfo *)malloc(sizeof(InstalledVersionInfo)); |
|---|
| 1509 | n/a | value_size = sizeof(ivi->prefix); |
|---|
| 1510 | n/a | if (ivi && |
|---|
| 1511 | n/a | ERROR_SUCCESS == RegQueryValueEx(hk, NULL, NULL, NULL, |
|---|
| 1512 | n/a | ivi->prefix, &value_size)) { |
|---|
| 1513 | n/a | itemindex = SendMessage(hwnd, LB_ADDSTRING, 0, |
|---|
| 1514 | n/a | (LPARAM)(LPSTR)vers_name); |
|---|
| 1515 | n/a | ivi->hkey = hkRoot; |
|---|
| 1516 | n/a | SendMessage(hwnd, LB_SETITEMDATA, itemindex, |
|---|
| 1517 | n/a | (LPARAM)(LPSTR)ivi); |
|---|
| 1518 | n/a | } |
|---|
| 1519 | n/a | RegCloseKey(hk); |
|---|
| 1520 | n/a | } |
|---|
| 1521 | n/a | } |
|---|
| 1522 | n/a | RegCloseKey(hKey); |
|---|
| 1523 | n/a | return result; |
|---|
| 1524 | n/a | } |
|---|
| 1525 | n/a | |
|---|
| 1526 | n/a | /* Determine if the current user can write to HKEY_LOCAL_MACHINE */ |
|---|
| 1527 | n/a | BOOL HasLocalMachinePrivs() |
|---|
| 1528 | n/a | { |
|---|
| 1529 | n/a | HKEY hKey; |
|---|
| 1530 | n/a | DWORD result; |
|---|
| 1531 | n/a | static char KeyName[] = |
|---|
| 1532 | n/a | "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall"; |
|---|
| 1533 | n/a | |
|---|
| 1534 | n/a | result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, |
|---|
| 1535 | n/a | KeyName, |
|---|
| 1536 | n/a | 0, |
|---|
| 1537 | n/a | KEY_CREATE_SUB_KEY, |
|---|
| 1538 | n/a | &hKey); |
|---|
| 1539 | n/a | if (result==0) |
|---|
| 1540 | n/a | RegCloseKey(hKey); |
|---|
| 1541 | n/a | return result==0; |
|---|
| 1542 | n/a | } |
|---|
| 1543 | n/a | |
|---|
| 1544 | n/a | // Check the root registry key to use - either HKLM or HKCU. |
|---|
| 1545 | n/a | // If Python is installed in HKCU, then our extension also must be installed |
|---|
| 1546 | n/a | // in HKCU - as Python won't be available for other users, we shouldn't either |
|---|
| 1547 | n/a | // (and will fail if we are!) |
|---|
| 1548 | n/a | // If Python is installed in HKLM, then we will also prefer to use HKLM, but |
|---|
| 1549 | n/a | // this may not be possible - so we silently fall back to HKCU. |
|---|
| 1550 | n/a | // |
|---|
| 1551 | n/a | // We assume hkey_root is already set to where Python itself is installed. |
|---|
| 1552 | n/a | void CheckRootKey(HWND hwnd) |
|---|
| 1553 | n/a | { |
|---|
| 1554 | n/a | if (hkey_root==HKEY_CURRENT_USER) { |
|---|
| 1555 | n/a | ; // as above, always install ourself in HKCU too. |
|---|
| 1556 | n/a | } else if (hkey_root==HKEY_LOCAL_MACHINE) { |
|---|
| 1557 | n/a | // Python in HKLM, but we may or may not have permissions there. |
|---|
| 1558 | n/a | // Open the uninstall key with 'create' permissions - if this fails, |
|---|
| 1559 | n/a | // we don't have permission. |
|---|
| 1560 | n/a | if (!HasLocalMachinePrivs()) |
|---|
| 1561 | n/a | hkey_root = HKEY_CURRENT_USER; |
|---|
| 1562 | n/a | } else { |
|---|
| 1563 | n/a | MessageBox(hwnd, "Don't know Python's installation type", |
|---|
| 1564 | n/a | "Strange", MB_OK | MB_ICONSTOP); |
|---|
| 1565 | n/a | /* Default to wherever they can, but preferring HKLM */ |
|---|
| 1566 | n/a | hkey_root = HasLocalMachinePrivs() ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; |
|---|
| 1567 | n/a | } |
|---|
| 1568 | n/a | } |
|---|
| 1569 | n/a | |
|---|
| 1570 | n/a | /* Return the installation scheme depending on Python version number */ |
|---|
| 1571 | n/a | SCHEME *GetScheme(int major, int minor) |
|---|
| 1572 | n/a | { |
|---|
| 1573 | n/a | if (major > 2) |
|---|
| 1574 | n/a | return new_scheme; |
|---|
| 1575 | n/a | else if((major == 2) && (minor >= 2)) |
|---|
| 1576 | n/a | return new_scheme; |
|---|
| 1577 | n/a | return old_scheme; |
|---|
| 1578 | n/a | } |
|---|
| 1579 | n/a | |
|---|
| 1580 | n/a | INT_PTR CALLBACK |
|---|
| 1581 | n/a | SelectPythonDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) |
|---|
| 1582 | n/a | { |
|---|
| 1583 | n/a | LPNMHDR lpnm; |
|---|
| 1584 | n/a | |
|---|
| 1585 | n/a | switch (msg) { |
|---|
| 1586 | n/a | case WM_INITDIALOG: |
|---|
| 1587 | n/a | if (hBitmap) |
|---|
| 1588 | n/a | SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE, |
|---|
| 1589 | n/a | IMAGE_BITMAP, (LPARAM)hBitmap); |
|---|
| 1590 | n/a | GetPythonVersions(GetDlgItem(hwnd, IDC_VERSIONS_LIST), |
|---|
| 1591 | n/a | HKEY_LOCAL_MACHINE, target_version); |
|---|
| 1592 | n/a | GetPythonVersions(GetDlgItem(hwnd, IDC_VERSIONS_LIST), |
|---|
| 1593 | n/a | HKEY_CURRENT_USER, target_version); |
|---|
| 1594 | n/a | { /* select the last entry which is the highest python |
|---|
| 1595 | n/a | version found */ |
|---|
| 1596 | n/a | int count; |
|---|
| 1597 | n/a | count = SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST, |
|---|
| 1598 | n/a | LB_GETCOUNT, 0, 0); |
|---|
| 1599 | n/a | if (count && count != LB_ERR) |
|---|
| 1600 | n/a | SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST, LB_SETCURSEL, |
|---|
| 1601 | n/a | count-1, 0); |
|---|
| 1602 | n/a | |
|---|
| 1603 | n/a | /* If a specific Python version is required, |
|---|
| 1604 | n/a | * display a prominent notice showing this fact. |
|---|
| 1605 | n/a | */ |
|---|
| 1606 | n/a | if (target_version && target_version[0]) { |
|---|
| 1607 | n/a | char buffer[4096]; |
|---|
| 1608 | n/a | wsprintf(buffer, |
|---|
| 1609 | n/a | "Python %s is required for this package. " |
|---|
| 1610 | n/a | "Select installation to use:", |
|---|
| 1611 | n/a | target_version); |
|---|
| 1612 | n/a | SetDlgItemText(hwnd, IDC_TITLE, buffer); |
|---|
| 1613 | n/a | } |
|---|
| 1614 | n/a | |
|---|
| 1615 | n/a | if (count == 0) { |
|---|
| 1616 | n/a | char Buffer[4096]; |
|---|
| 1617 | n/a | char *msg; |
|---|
| 1618 | n/a | if (target_version && target_version[0]) { |
|---|
| 1619 | n/a | wsprintf(Buffer, |
|---|
| 1620 | n/a | "Python version %s required, which was not found" |
|---|
| 1621 | n/a | " in the registry.", target_version); |
|---|
| 1622 | n/a | msg = Buffer; |
|---|
| 1623 | n/a | } else |
|---|
| 1624 | n/a | msg = "No Python installation found in the registry."; |
|---|
| 1625 | n/a | MessageBox(hwnd, msg, "Cannot install", |
|---|
| 1626 | n/a | MB_OK | MB_ICONSTOP); |
|---|
| 1627 | n/a | } |
|---|
| 1628 | n/a | } |
|---|
| 1629 | n/a | goto UpdateInstallDir; |
|---|
| 1630 | n/a | break; |
|---|
| 1631 | n/a | |
|---|
| 1632 | n/a | case WM_COMMAND: |
|---|
| 1633 | n/a | switch (LOWORD(wParam)) { |
|---|
| 1634 | n/a | /* |
|---|
| 1635 | n/a | case IDC_OTHERPYTHON: |
|---|
| 1636 | n/a | if (GetOtherPythonVersion(GetDlgItem(hwnd, IDC_VERSIONS_LIST), |
|---|
| 1637 | n/a | target_version)) |
|---|
| 1638 | n/a | goto UpdateInstallDir; |
|---|
| 1639 | n/a | break; |
|---|
| 1640 | n/a | */ |
|---|
| 1641 | n/a | case IDC_VERSIONS_LIST: |
|---|
| 1642 | n/a | switch (HIWORD(wParam)) { |
|---|
| 1643 | n/a | int id; |
|---|
| 1644 | n/a | case LBN_SELCHANGE: |
|---|
| 1645 | n/a | UpdateInstallDir: |
|---|
| 1646 | n/a | PropSheet_SetWizButtons(GetParent(hwnd), |
|---|
| 1647 | n/a | PSWIZB_BACK | PSWIZB_NEXT); |
|---|
| 1648 | n/a | id = SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST, |
|---|
| 1649 | n/a | LB_GETCURSEL, 0, 0); |
|---|
| 1650 | n/a | if (id == LB_ERR) { |
|---|
| 1651 | n/a | PropSheet_SetWizButtons(GetParent(hwnd), |
|---|
| 1652 | n/a | PSWIZB_BACK); |
|---|
| 1653 | n/a | SetDlgItemText(hwnd, IDC_PATH, ""); |
|---|
| 1654 | n/a | SetDlgItemText(hwnd, IDC_INSTALL_PATH, ""); |
|---|
| 1655 | n/a | strcpy(python_dir, ""); |
|---|
| 1656 | n/a | strcpy(pythondll, ""); |
|---|
| 1657 | n/a | } else { |
|---|
| 1658 | n/a | char *pbuf; |
|---|
| 1659 | n/a | int result; |
|---|
| 1660 | n/a | InstalledVersionInfo *ivi; |
|---|
| 1661 | n/a | PropSheet_SetWizButtons(GetParent(hwnd), |
|---|
| 1662 | n/a | PSWIZB_BACK | PSWIZB_NEXT); |
|---|
| 1663 | n/a | /* Get the python directory */ |
|---|
| 1664 | n/a | ivi = (InstalledVersionInfo *) |
|---|
| 1665 | n/a | SendDlgItemMessage(hwnd, |
|---|
| 1666 | n/a | IDC_VERSIONS_LIST, |
|---|
| 1667 | n/a | LB_GETITEMDATA, |
|---|
| 1668 | n/a | id, |
|---|
| 1669 | n/a | 0); |
|---|
| 1670 | n/a | hkey_root = ivi->hkey; |
|---|
| 1671 | n/a | strcpy(python_dir, ivi->prefix); |
|---|
| 1672 | n/a | SetDlgItemText(hwnd, IDC_PATH, python_dir); |
|---|
| 1673 | n/a | /* retrieve the python version and pythondll to use */ |
|---|
| 1674 | n/a | result = SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST, |
|---|
| 1675 | n/a | LB_GETTEXTLEN, (WPARAM)id, 0); |
|---|
| 1676 | n/a | pbuf = (char *)malloc(result + 1); |
|---|
| 1677 | n/a | if (pbuf) { |
|---|
| 1678 | n/a | /* guess the name of the python-dll */ |
|---|
| 1679 | n/a | SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST, |
|---|
| 1680 | n/a | LB_GETTEXT, (WPARAM)id, |
|---|
| 1681 | n/a | (LPARAM)pbuf); |
|---|
| 1682 | n/a | result = sscanf(pbuf, "Python Version %d.%d", |
|---|
| 1683 | n/a | &py_major, &py_minor); |
|---|
| 1684 | n/a | if (result == 2) { |
|---|
| 1685 | n/a | #ifdef _DEBUG |
|---|
| 1686 | n/a | wsprintf(pythondll, "python%d%d_d.dll", |
|---|
| 1687 | n/a | py_major, py_minor); |
|---|
| 1688 | n/a | #else |
|---|
| 1689 | n/a | wsprintf(pythondll, "python%d%d.dll", |
|---|
| 1690 | n/a | py_major, py_minor); |
|---|
| 1691 | n/a | #endif |
|---|
| 1692 | n/a | } |
|---|
| 1693 | n/a | free(pbuf); |
|---|
| 1694 | n/a | } else |
|---|
| 1695 | n/a | strcpy(pythondll, ""); |
|---|
| 1696 | n/a | /* retrieve the scheme for this version */ |
|---|
| 1697 | n/a | { |
|---|
| 1698 | n/a | char install_path[_MAX_PATH]; |
|---|
| 1699 | n/a | SCHEME *scheme = GetScheme(py_major, py_minor); |
|---|
| 1700 | n/a | strcpy(install_path, python_dir); |
|---|
| 1701 | n/a | if (install_path[strlen(install_path)-1] != '\\') |
|---|
| 1702 | n/a | strcat(install_path, "\\"); |
|---|
| 1703 | n/a | strcat(install_path, scheme[0].prefix); |
|---|
| 1704 | n/a | SetDlgItemText(hwnd, IDC_INSTALL_PATH, install_path); |
|---|
| 1705 | n/a | } |
|---|
| 1706 | n/a | } |
|---|
| 1707 | n/a | } |
|---|
| 1708 | n/a | break; |
|---|
| 1709 | n/a | } |
|---|
| 1710 | n/a | return 0; |
|---|
| 1711 | n/a | |
|---|
| 1712 | n/a | case WM_NOTIFY: |
|---|
| 1713 | n/a | lpnm = (LPNMHDR) lParam; |
|---|
| 1714 | n/a | |
|---|
| 1715 | n/a | switch (lpnm->code) { |
|---|
| 1716 | n/a | int id; |
|---|
| 1717 | n/a | case PSN_SETACTIVE: |
|---|
| 1718 | n/a | id = SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST, |
|---|
| 1719 | n/a | LB_GETCURSEL, 0, 0); |
|---|
| 1720 | n/a | if (id == LB_ERR) |
|---|
| 1721 | n/a | PropSheet_SetWizButtons(GetParent(hwnd), |
|---|
| 1722 | n/a | PSWIZB_BACK); |
|---|
| 1723 | n/a | else |
|---|
| 1724 | n/a | PropSheet_SetWizButtons(GetParent(hwnd), |
|---|
| 1725 | n/a | PSWIZB_BACK | PSWIZB_NEXT); |
|---|
| 1726 | n/a | break; |
|---|
| 1727 | n/a | |
|---|
| 1728 | n/a | case PSN_WIZNEXT: |
|---|
| 1729 | n/a | break; |
|---|
| 1730 | n/a | |
|---|
| 1731 | n/a | case PSN_WIZFINISH: |
|---|
| 1732 | n/a | break; |
|---|
| 1733 | n/a | |
|---|
| 1734 | n/a | case PSN_RESET: |
|---|
| 1735 | n/a | break; |
|---|
| 1736 | n/a | |
|---|
| 1737 | n/a | default: |
|---|
| 1738 | n/a | break; |
|---|
| 1739 | n/a | } |
|---|
| 1740 | n/a | } |
|---|
| 1741 | n/a | return 0; |
|---|
| 1742 | n/a | } |
|---|
| 1743 | n/a | |
|---|
| 1744 | n/a | static BOOL OpenLogfile(char *dir) |
|---|
| 1745 | n/a | { |
|---|
| 1746 | n/a | char buffer[_MAX_PATH+1]; |
|---|
| 1747 | n/a | time_t ltime; |
|---|
| 1748 | n/a | struct tm *now; |
|---|
| 1749 | n/a | long result; |
|---|
| 1750 | n/a | HKEY hKey, hSubkey; |
|---|
| 1751 | n/a | char subkey_name[256]; |
|---|
| 1752 | n/a | static char KeyName[] = |
|---|
| 1753 | n/a | "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall"; |
|---|
| 1754 | n/a | const char *root_name = (hkey_root==HKEY_LOCAL_MACHINE ? |
|---|
| 1755 | n/a | "HKEY_LOCAL_MACHINE" : "HKEY_CURRENT_USER"); |
|---|
| 1756 | n/a | DWORD disposition; |
|---|
| 1757 | n/a | |
|---|
| 1758 | n/a | /* Use Create, as the Uninstall subkey may not exist under HKCU. |
|---|
| 1759 | n/a | Use CreateKeyEx, so we can specify a SAM specifying write access |
|---|
| 1760 | n/a | */ |
|---|
| 1761 | n/a | result = RegCreateKeyEx(hkey_root, |
|---|
| 1762 | n/a | KeyName, |
|---|
| 1763 | n/a | 0, /* reserved */ |
|---|
| 1764 | n/a | NULL, /* class */ |
|---|
| 1765 | n/a | 0, /* options */ |
|---|
| 1766 | n/a | KEY_CREATE_SUB_KEY, /* sam */ |
|---|
| 1767 | n/a | NULL, /* security */ |
|---|
| 1768 | n/a | &hKey, /* result key */ |
|---|
| 1769 | n/a | NULL); /* disposition */ |
|---|
| 1770 | n/a | if (result != ERROR_SUCCESS) { |
|---|
| 1771 | n/a | if (result == ERROR_ACCESS_DENIED) { |
|---|
| 1772 | n/a | /* This should no longer be able to happen - we have already |
|---|
| 1773 | n/a | checked if they have permissions in HKLM, and all users |
|---|
| 1774 | n/a | should have write access to HKCU. |
|---|
| 1775 | n/a | */ |
|---|
| 1776 | n/a | MessageBox(GetFocus(), |
|---|
| 1777 | n/a | "You do not seem to have sufficient access rights\n" |
|---|
| 1778 | n/a | "on this machine to install this software", |
|---|
| 1779 | n/a | NULL, |
|---|
| 1780 | n/a | MB_OK | MB_ICONSTOP); |
|---|
| 1781 | n/a | return FALSE; |
|---|
| 1782 | n/a | } else { |
|---|
| 1783 | n/a | MessageBox(GetFocus(), KeyName, "Could not open key", MB_OK); |
|---|
| 1784 | n/a | } |
|---|
| 1785 | n/a | } |
|---|
| 1786 | n/a | |
|---|
| 1787 | n/a | sprintf(buffer, "%s\\%s-wininst.log", dir, meta_name); |
|---|
| 1788 | n/a | logfile = fopen(buffer, "a"); |
|---|
| 1789 | n/a | if (!logfile) { |
|---|
| 1790 | n/a | char error[1024]; |
|---|
| 1791 | n/a | |
|---|
| 1792 | n/a | sprintf(error, "Can't create \"%s\" (%s).\n\n" |
|---|
| 1793 | n/a | "Try to execute the installer as administrator.", |
|---|
| 1794 | n/a | buffer, strerror(errno)); |
|---|
| 1795 | n/a | MessageBox(GetFocus(), error, NULL, MB_OK | MB_ICONSTOP); |
|---|
| 1796 | n/a | return FALSE; |
|---|
| 1797 | n/a | } |
|---|
| 1798 | n/a | |
|---|
| 1799 | n/a | time(<ime); |
|---|
| 1800 | n/a | now = localtime(<ime); |
|---|
| 1801 | n/a | strftime(buffer, sizeof(buffer), |
|---|
| 1802 | n/a | "*** Installation started %Y/%m/%d %H:%M ***\n", |
|---|
| 1803 | n/a | localtime(<ime)); |
|---|
| 1804 | n/a | fprintf(logfile, buffer); |
|---|
| 1805 | n/a | fprintf(logfile, "Source: %s\n", modulename); |
|---|
| 1806 | n/a | |
|---|
| 1807 | n/a | /* Root key must be first entry processed by uninstaller. */ |
|---|
| 1808 | n/a | fprintf(logfile, "999 Root Key: %s\n", root_name); |
|---|
| 1809 | n/a | |
|---|
| 1810 | n/a | sprintf(subkey_name, "%s-py%d.%d", meta_name, py_major, py_minor); |
|---|
| 1811 | n/a | |
|---|
| 1812 | n/a | result = RegCreateKeyEx(hKey, subkey_name, |
|---|
| 1813 | n/a | 0, NULL, 0, |
|---|
| 1814 | n/a | KEY_WRITE, |
|---|
| 1815 | n/a | NULL, |
|---|
| 1816 | n/a | &hSubkey, |
|---|
| 1817 | n/a | &disposition); |
|---|
| 1818 | n/a | |
|---|
| 1819 | n/a | if (result != ERROR_SUCCESS) |
|---|
| 1820 | n/a | MessageBox(GetFocus(), subkey_name, "Could not create key", MB_OK); |
|---|
| 1821 | n/a | |
|---|
| 1822 | n/a | RegCloseKey(hKey); |
|---|
| 1823 | n/a | |
|---|
| 1824 | n/a | if (disposition == REG_CREATED_NEW_KEY) |
|---|
| 1825 | n/a | fprintf(logfile, "020 Reg DB Key: [%s]%s\n", KeyName, subkey_name); |
|---|
| 1826 | n/a | |
|---|
| 1827 | n/a | sprintf(buffer, "Python %d.%d %s", py_major, py_minor, title); |
|---|
| 1828 | n/a | |
|---|
| 1829 | n/a | result = RegSetValueEx(hSubkey, "DisplayName", |
|---|
| 1830 | n/a | 0, |
|---|
| 1831 | n/a | REG_SZ, |
|---|
| 1832 | n/a | buffer, |
|---|
| 1833 | n/a | strlen(buffer)+1); |
|---|
| 1834 | n/a | |
|---|
| 1835 | n/a | if (result != ERROR_SUCCESS) |
|---|
| 1836 | n/a | MessageBox(GetFocus(), buffer, "Could not set key value", MB_OK); |
|---|
| 1837 | n/a | |
|---|
| 1838 | n/a | fprintf(logfile, "040 Reg DB Value: [%s\\%s]%s=%s\n", |
|---|
| 1839 | n/a | KeyName, subkey_name, "DisplayName", buffer); |
|---|
| 1840 | n/a | |
|---|
| 1841 | n/a | { |
|---|
| 1842 | n/a | FILE *fp; |
|---|
| 1843 | n/a | sprintf(buffer, "%s\\Remove%s.exe", dir, meta_name); |
|---|
| 1844 | n/a | fp = fopen(buffer, "wb"); |
|---|
| 1845 | n/a | fwrite(arc_data, exe_size, 1, fp); |
|---|
| 1846 | n/a | fclose(fp); |
|---|
| 1847 | n/a | |
|---|
| 1848 | n/a | sprintf(buffer, "\"%s\\Remove%s.exe\" -u \"%s\\%s-wininst.log\"", |
|---|
| 1849 | n/a | dir, meta_name, dir, meta_name); |
|---|
| 1850 | n/a | |
|---|
| 1851 | n/a | result = RegSetValueEx(hSubkey, "UninstallString", |
|---|
| 1852 | n/a | 0, |
|---|
| 1853 | n/a | REG_SZ, |
|---|
| 1854 | n/a | buffer, |
|---|
| 1855 | n/a | strlen(buffer)+1); |
|---|
| 1856 | n/a | |
|---|
| 1857 | n/a | if (result != ERROR_SUCCESS) |
|---|
| 1858 | n/a | MessageBox(GetFocus(), buffer, "Could not set key value", MB_OK); |
|---|
| 1859 | n/a | |
|---|
| 1860 | n/a | fprintf(logfile, "040 Reg DB Value: [%s\\%s]%s=%s\n", |
|---|
| 1861 | n/a | KeyName, subkey_name, "UninstallString", buffer); |
|---|
| 1862 | n/a | } |
|---|
| 1863 | n/a | return TRUE; |
|---|
| 1864 | n/a | } |
|---|
| 1865 | n/a | |
|---|
| 1866 | n/a | static void CloseLogfile(void) |
|---|
| 1867 | n/a | { |
|---|
| 1868 | n/a | char buffer[_MAX_PATH+1]; |
|---|
| 1869 | n/a | time_t ltime; |
|---|
| 1870 | n/a | struct tm *now; |
|---|
| 1871 | n/a | |
|---|
| 1872 | n/a | time(<ime); |
|---|
| 1873 | n/a | now = localtime(<ime); |
|---|
| 1874 | n/a | strftime(buffer, sizeof(buffer), |
|---|
| 1875 | n/a | "*** Installation finished %Y/%m/%d %H:%M ***\n", |
|---|
| 1876 | n/a | localtime(<ime)); |
|---|
| 1877 | n/a | fprintf(logfile, buffer); |
|---|
| 1878 | n/a | if (logfile) |
|---|
| 1879 | n/a | fclose(logfile); |
|---|
| 1880 | n/a | } |
|---|
| 1881 | n/a | |
|---|
| 1882 | n/a | INT_PTR CALLBACK |
|---|
| 1883 | n/a | InstallFilesDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) |
|---|
| 1884 | n/a | { |
|---|
| 1885 | n/a | LPNMHDR lpnm; |
|---|
| 1886 | n/a | char Buffer[4096]; |
|---|
| 1887 | n/a | SCHEME *scheme; |
|---|
| 1888 | n/a | |
|---|
| 1889 | n/a | switch (msg) { |
|---|
| 1890 | n/a | case WM_INITDIALOG: |
|---|
| 1891 | n/a | if (hBitmap) |
|---|
| 1892 | n/a | SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE, |
|---|
| 1893 | n/a | IMAGE_BITMAP, (LPARAM)hBitmap); |
|---|
| 1894 | n/a | wsprintf(Buffer, |
|---|
| 1895 | n/a | "Click Next to begin the installation of %s. " |
|---|
| 1896 | n/a | "If you want to review or change any of your " |
|---|
| 1897 | n/a | " installation settings, click Back. " |
|---|
| 1898 | n/a | "Click Cancel to exit the wizard.", |
|---|
| 1899 | n/a | meta_name); |
|---|
| 1900 | n/a | SetDlgItemText(hwnd, IDC_TITLE, Buffer); |
|---|
| 1901 | n/a | SetDlgItemText(hwnd, IDC_INFO, "Ready to install"); |
|---|
| 1902 | n/a | break; |
|---|
| 1903 | n/a | |
|---|
| 1904 | n/a | case WM_NUMFILES: |
|---|
| 1905 | n/a | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, lParam); |
|---|
| 1906 | n/a | PumpMessages(); |
|---|
| 1907 | n/a | return TRUE; |
|---|
| 1908 | n/a | |
|---|
| 1909 | n/a | case WM_NEXTFILE: |
|---|
| 1910 | n/a | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, wParam, |
|---|
| 1911 | n/a | 0); |
|---|
| 1912 | n/a | SetDlgItemText(hwnd, IDC_INFO, (LPSTR)lParam); |
|---|
| 1913 | n/a | PumpMessages(); |
|---|
| 1914 | n/a | return TRUE; |
|---|
| 1915 | n/a | |
|---|
| 1916 | n/a | case WM_NOTIFY: |
|---|
| 1917 | n/a | lpnm = (LPNMHDR) lParam; |
|---|
| 1918 | n/a | |
|---|
| 1919 | n/a | switch (lpnm->code) { |
|---|
| 1920 | n/a | case PSN_SETACTIVE: |
|---|
| 1921 | n/a | PropSheet_SetWizButtons(GetParent(hwnd), |
|---|
| 1922 | n/a | PSWIZB_BACK | PSWIZB_NEXT); |
|---|
| 1923 | n/a | break; |
|---|
| 1924 | n/a | |
|---|
| 1925 | n/a | case PSN_WIZFINISH: |
|---|
| 1926 | n/a | break; |
|---|
| 1927 | n/a | |
|---|
| 1928 | n/a | case PSN_WIZNEXT: |
|---|
| 1929 | n/a | /* Handle a Next button click here */ |
|---|
| 1930 | n/a | hDialog = hwnd; |
|---|
| 1931 | n/a | success = TRUE; |
|---|
| 1932 | n/a | |
|---|
| 1933 | n/a | /* Disable the buttons while we work. Sending CANCELTOCLOSE has |
|---|
| 1934 | n/a | the effect of disabling the cancel button, which is a) as we |
|---|
| 1935 | n/a | do everything synchronously we can't cancel, and b) the next |
|---|
| 1936 | n/a | step is 'finished', when it is too late to cancel anyway. |
|---|
| 1937 | n/a | The next step being 'Finished' means we also don't need to |
|---|
| 1938 | n/a | restore the button state back */ |
|---|
| 1939 | n/a | PropSheet_SetWizButtons(GetParent(hwnd), 0); |
|---|
| 1940 | n/a | SendMessage(GetParent(hwnd), PSM_CANCELTOCLOSE, 0, 0); |
|---|
| 1941 | n/a | /* Make sure the installation directory name ends in a */ |
|---|
| 1942 | n/a | /* backslash */ |
|---|
| 1943 | n/a | if (python_dir[strlen(python_dir)-1] != '\\') |
|---|
| 1944 | n/a | strcat(python_dir, "\\"); |
|---|
| 1945 | n/a | /* Strip the trailing backslash again */ |
|---|
| 1946 | n/a | python_dir[strlen(python_dir)-1] = '\0'; |
|---|
| 1947 | n/a | |
|---|
| 1948 | n/a | CheckRootKey(hwnd); |
|---|
| 1949 | n/a | |
|---|
| 1950 | n/a | if (!OpenLogfile(python_dir)) |
|---|
| 1951 | n/a | break; |
|---|
| 1952 | n/a | |
|---|
| 1953 | n/a | /* |
|---|
| 1954 | n/a | * The scheme we have to use depends on the Python version... |
|---|
| 1955 | n/a | if sys.version < "2.2": |
|---|
| 1956 | n/a | WINDOWS_SCHEME = { |
|---|
| 1957 | n/a | 'purelib': '$base', |
|---|
| 1958 | n/a | 'platlib': '$base', |
|---|
| 1959 | n/a | 'headers': '$base/Include/$dist_name', |
|---|
| 1960 | n/a | 'scripts': '$base/Scripts', |
|---|
| 1961 | n/a | 'data' : '$base', |
|---|
| 1962 | n/a | } |
|---|
| 1963 | n/a | else: |
|---|
| 1964 | n/a | WINDOWS_SCHEME = { |
|---|
| 1965 | n/a | 'purelib': '$base/Lib/site-packages', |
|---|
| 1966 | n/a | 'platlib': '$base/Lib/site-packages', |
|---|
| 1967 | n/a | 'headers': '$base/Include/$dist_name', |
|---|
| 1968 | n/a | 'scripts': '$base/Scripts', |
|---|
| 1969 | n/a | 'data' : '$base', |
|---|
| 1970 | n/a | } |
|---|
| 1971 | n/a | */ |
|---|
| 1972 | n/a | scheme = GetScheme(py_major, py_minor); |
|---|
| 1973 | n/a | /* Run the pre-install script. */ |
|---|
| 1974 | n/a | if (pre_install_script && *pre_install_script) { |
|---|
| 1975 | n/a | SetDlgItemText (hwnd, IDC_TITLE, |
|---|
| 1976 | n/a | "Running pre-installation script"); |
|---|
| 1977 | n/a | run_simple_script(pre_install_script); |
|---|
| 1978 | n/a | } |
|---|
| 1979 | n/a | if (!success) { |
|---|
| 1980 | n/a | break; |
|---|
| 1981 | n/a | } |
|---|
| 1982 | n/a | /* Extract all files from the archive */ |
|---|
| 1983 | n/a | SetDlgItemText(hwnd, IDC_TITLE, "Installing files..."); |
|---|
| 1984 | n/a | if (!unzip_archive (scheme, |
|---|
| 1985 | n/a | python_dir, arc_data, |
|---|
| 1986 | n/a | arc_size, notify)) |
|---|
| 1987 | n/a | set_failure_reason("Failed to unzip installation files"); |
|---|
| 1988 | n/a | /* Compile the py-files */ |
|---|
| 1989 | n/a | if (success && pyc_compile) { |
|---|
| 1990 | n/a | int errors; |
|---|
| 1991 | n/a | HINSTANCE hPython; |
|---|
| 1992 | n/a | SetDlgItemText(hwnd, IDC_TITLE, |
|---|
| 1993 | n/a | "Compiling files to .pyc..."); |
|---|
| 1994 | n/a | |
|---|
| 1995 | n/a | SetDlgItemText(hDialog, IDC_INFO, "Loading python..."); |
|---|
| 1996 | n/a | hPython = LoadPythonDll(pythondll); |
|---|
| 1997 | n/a | if (hPython) { |
|---|
| 1998 | n/a | errors = compile_filelist(hPython, FALSE); |
|---|
| 1999 | n/a | FreeLibrary(hPython); |
|---|
| 2000 | n/a | } |
|---|
| 2001 | n/a | /* Compilation errors are intentionally ignored: |
|---|
| 2002 | n/a | * Python2.0 contains a bug which will result |
|---|
| 2003 | n/a | * in sys.path containing garbage under certain |
|---|
| 2004 | n/a | * circumstances, and an error message will only |
|---|
| 2005 | n/a | * confuse the user. |
|---|
| 2006 | n/a | */ |
|---|
| 2007 | n/a | } |
|---|
| 2008 | n/a | if (success && pyo_compile) { |
|---|
| 2009 | n/a | int errors; |
|---|
| 2010 | n/a | HINSTANCE hPython; |
|---|
| 2011 | n/a | SetDlgItemText(hwnd, IDC_TITLE, |
|---|
| 2012 | n/a | "Compiling files to .pyo..."); |
|---|
| 2013 | n/a | |
|---|
| 2014 | n/a | SetDlgItemText(hDialog, IDC_INFO, "Loading python..."); |
|---|
| 2015 | n/a | hPython = LoadPythonDll(pythondll); |
|---|
| 2016 | n/a | if (hPython) { |
|---|
| 2017 | n/a | errors = compile_filelist(hPython, TRUE); |
|---|
| 2018 | n/a | FreeLibrary(hPython); |
|---|
| 2019 | n/a | } |
|---|
| 2020 | n/a | /* Errors ignored: see above */ |
|---|
| 2021 | n/a | } |
|---|
| 2022 | n/a | |
|---|
| 2023 | n/a | |
|---|
| 2024 | n/a | break; |
|---|
| 2025 | n/a | |
|---|
| 2026 | n/a | case PSN_RESET: |
|---|
| 2027 | n/a | break; |
|---|
| 2028 | n/a | |
|---|
| 2029 | n/a | default: |
|---|
| 2030 | n/a | break; |
|---|
| 2031 | n/a | } |
|---|
| 2032 | n/a | } |
|---|
| 2033 | n/a | return 0; |
|---|
| 2034 | n/a | } |
|---|
| 2035 | n/a | |
|---|
| 2036 | n/a | |
|---|
| 2037 | n/a | INT_PTR CALLBACK |
|---|
| 2038 | n/a | FinishedDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) |
|---|
| 2039 | n/a | { |
|---|
| 2040 | n/a | LPNMHDR lpnm; |
|---|
| 2041 | n/a | |
|---|
| 2042 | n/a | switch (msg) { |
|---|
| 2043 | n/a | case WM_INITDIALOG: |
|---|
| 2044 | n/a | if (hBitmap) |
|---|
| 2045 | n/a | SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE, |
|---|
| 2046 | n/a | IMAGE_BITMAP, (LPARAM)hBitmap); |
|---|
| 2047 | n/a | if (!success) |
|---|
| 2048 | n/a | SetDlgItemText(hwnd, IDC_INFO, get_failure_reason()); |
|---|
| 2049 | n/a | |
|---|
| 2050 | n/a | /* async delay: will show the dialog box completely before |
|---|
| 2051 | n/a | the install_script is started */ |
|---|
| 2052 | n/a | PostMessage(hwnd, WM_USER, 0, 0L); |
|---|
| 2053 | n/a | return TRUE; |
|---|
| 2054 | n/a | |
|---|
| 2055 | n/a | case WM_USER: |
|---|
| 2056 | n/a | |
|---|
| 2057 | n/a | if (success && install_script && install_script[0]) { |
|---|
| 2058 | n/a | char fname[MAX_PATH]; |
|---|
| 2059 | n/a | char *buffer; |
|---|
| 2060 | n/a | HCURSOR hCursor; |
|---|
| 2061 | n/a | int result; |
|---|
| 2062 | n/a | |
|---|
| 2063 | n/a | char *argv[3] = {NULL, "-install", NULL}; |
|---|
| 2064 | n/a | |
|---|
| 2065 | n/a | SetDlgItemText(hwnd, IDC_TITLE, |
|---|
| 2066 | n/a | "Please wait while running postinstall script..."); |
|---|
| 2067 | n/a | strcpy(fname, python_dir); |
|---|
| 2068 | n/a | strcat(fname, "\\Scripts\\"); |
|---|
| 2069 | n/a | strcat(fname, install_script); |
|---|
| 2070 | n/a | |
|---|
| 2071 | n/a | if (logfile) |
|---|
| 2072 | n/a | fprintf(logfile, "300 Run Script: [%s]%s\n", pythondll, fname); |
|---|
| 2073 | n/a | |
|---|
| 2074 | n/a | hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT)); |
|---|
| 2075 | n/a | |
|---|
| 2076 | n/a | argv[0] = fname; |
|---|
| 2077 | n/a | |
|---|
| 2078 | n/a | result = run_installscript(fname, 2, argv, &buffer); |
|---|
| 2079 | n/a | if (0 != result) { |
|---|
| 2080 | n/a | fprintf(stderr, "*** run_installscript: internal error 0x%X ***\n", result); |
|---|
| 2081 | n/a | } |
|---|
| 2082 | n/a | if (buffer) |
|---|
| 2083 | n/a | SetDlgItemText(hwnd, IDC_INFO, buffer); |
|---|
| 2084 | n/a | SetDlgItemText(hwnd, IDC_TITLE, |
|---|
| 2085 | n/a | "Postinstall script finished.\n" |
|---|
| 2086 | n/a | "Click the Finish button to exit the Setup wizard."); |
|---|
| 2087 | n/a | |
|---|
| 2088 | n/a | free(buffer); |
|---|
| 2089 | n/a | SetCursor(hCursor); |
|---|
| 2090 | n/a | CloseLogfile(); |
|---|
| 2091 | n/a | } |
|---|
| 2092 | n/a | |
|---|
| 2093 | n/a | return TRUE; |
|---|
| 2094 | n/a | |
|---|
| 2095 | n/a | case WM_NOTIFY: |
|---|
| 2096 | n/a | lpnm = (LPNMHDR) lParam; |
|---|
| 2097 | n/a | |
|---|
| 2098 | n/a | switch (lpnm->code) { |
|---|
| 2099 | n/a | case PSN_SETACTIVE: /* Enable the Finish button */ |
|---|
| 2100 | n/a | PropSheet_SetWizButtons(GetParent(hwnd), PSWIZB_FINISH); |
|---|
| 2101 | n/a | break; |
|---|
| 2102 | n/a | |
|---|
| 2103 | n/a | case PSN_WIZNEXT: |
|---|
| 2104 | n/a | break; |
|---|
| 2105 | n/a | |
|---|
| 2106 | n/a | case PSN_WIZFINISH: |
|---|
| 2107 | n/a | break; |
|---|
| 2108 | n/a | |
|---|
| 2109 | n/a | case PSN_RESET: |
|---|
| 2110 | n/a | break; |
|---|
| 2111 | n/a | |
|---|
| 2112 | n/a | default: |
|---|
| 2113 | n/a | break; |
|---|
| 2114 | n/a | } |
|---|
| 2115 | n/a | } |
|---|
| 2116 | n/a | return 0; |
|---|
| 2117 | n/a | } |
|---|
| 2118 | n/a | |
|---|
| 2119 | n/a | void RunWizard(HWND hwnd) |
|---|
| 2120 | n/a | { |
|---|
| 2121 | n/a | PROPSHEETPAGE psp = {0}; |
|---|
| 2122 | n/a | HPROPSHEETPAGE ahpsp[4] = {0}; |
|---|
| 2123 | n/a | PROPSHEETHEADER psh = {0}; |
|---|
| 2124 | n/a | |
|---|
| 2125 | n/a | /* Display module information */ |
|---|
| 2126 | n/a | psp.dwSize = sizeof(psp); |
|---|
| 2127 | n/a | psp.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER; |
|---|
| 2128 | n/a | psp.hInstance = GetModuleHandle (NULL); |
|---|
| 2129 | n/a | psp.lParam = 0; |
|---|
| 2130 | n/a | psp.pfnDlgProc = IntroDlgProc; |
|---|
| 2131 | n/a | psp.pszTemplate = MAKEINTRESOURCE(IDD_INTRO); |
|---|
| 2132 | n/a | |
|---|
| 2133 | n/a | ahpsp[0] = CreatePropertySheetPage(&psp); |
|---|
| 2134 | n/a | |
|---|
| 2135 | n/a | /* Select python version to use */ |
|---|
| 2136 | n/a | psp.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER; |
|---|
| 2137 | n/a | psp.pszTemplate = MAKEINTRESOURCE(IDD_SELECTPYTHON); |
|---|
| 2138 | n/a | psp.pfnDlgProc = SelectPythonDlgProc; |
|---|
| 2139 | n/a | |
|---|
| 2140 | n/a | ahpsp[1] = CreatePropertySheetPage(&psp); |
|---|
| 2141 | n/a | |
|---|
| 2142 | n/a | /* Install the files */ |
|---|
| 2143 | n/a | psp.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER; |
|---|
| 2144 | n/a | psp.pszTemplate = MAKEINTRESOURCE(IDD_INSTALLFILES); |
|---|
| 2145 | n/a | psp.pfnDlgProc = InstallFilesDlgProc; |
|---|
| 2146 | n/a | |
|---|
| 2147 | n/a | ahpsp[2] = CreatePropertySheetPage(&psp); |
|---|
| 2148 | n/a | |
|---|
| 2149 | n/a | /* Show success or failure */ |
|---|
| 2150 | n/a | psp.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER; |
|---|
| 2151 | n/a | psp.pszTemplate = MAKEINTRESOURCE(IDD_FINISHED); |
|---|
| 2152 | n/a | psp.pfnDlgProc = FinishedDlgProc; |
|---|
| 2153 | n/a | |
|---|
| 2154 | n/a | ahpsp[3] = CreatePropertySheetPage(&psp); |
|---|
| 2155 | n/a | |
|---|
| 2156 | n/a | /* Create the property sheet */ |
|---|
| 2157 | n/a | psh.dwSize = sizeof(psh); |
|---|
| 2158 | n/a | psh.hInstance = GetModuleHandle(NULL); |
|---|
| 2159 | n/a | psh.hwndParent = hwnd; |
|---|
| 2160 | n/a | psh.phpage = ahpsp; |
|---|
| 2161 | n/a | psh.dwFlags = PSH_WIZARD/*97*//*|PSH_WATERMARK|PSH_HEADER*/; |
|---|
| 2162 | n/a | psh.pszbmWatermark = NULL; |
|---|
| 2163 | n/a | psh.pszbmHeader = NULL; |
|---|
| 2164 | n/a | psh.nStartPage = 0; |
|---|
| 2165 | n/a | psh.nPages = 4; |
|---|
| 2166 | n/a | |
|---|
| 2167 | n/a | PropertySheet(&psh); |
|---|
| 2168 | n/a | } |
|---|
| 2169 | n/a | |
|---|
| 2170 | n/a | // subtly different from HasLocalMachinePrivs(), in that after executing |
|---|
| 2171 | n/a | // an 'elevated' process, we expect this to return TRUE - but there is no |
|---|
| 2172 | n/a | // such implication for HasLocalMachinePrivs |
|---|
| 2173 | n/a | BOOL MyIsUserAnAdmin() |
|---|
| 2174 | n/a | { |
|---|
| 2175 | n/a | typedef BOOL (WINAPI *PFNIsUserAnAdmin)(); |
|---|
| 2176 | n/a | static PFNIsUserAnAdmin pfnIsUserAnAdmin = NULL; |
|---|
| 2177 | n/a | HMODULE shell32; |
|---|
| 2178 | n/a | // This function isn't guaranteed to be available (and it can't hurt |
|---|
| 2179 | n/a | // to leave the library loaded) |
|---|
| 2180 | n/a | if (0 == (shell32=LoadLibrary("shell32.dll"))) |
|---|
| 2181 | n/a | return FALSE; |
|---|
| 2182 | n/a | if (0 == (pfnIsUserAnAdmin=(PFNIsUserAnAdmin)GetProcAddress(shell32, "IsUserAnAdmin"))) |
|---|
| 2183 | n/a | return FALSE; |
|---|
| 2184 | n/a | return (*pfnIsUserAnAdmin)(); |
|---|
| 2185 | n/a | } |
|---|
| 2186 | n/a | |
|---|
| 2187 | n/a | // Some magic for Vista's UAC. If there is a target_version, and |
|---|
| 2188 | n/a | // if that target version is installed in the registry under |
|---|
| 2189 | n/a | // HKLM, and we are not current administrator, then |
|---|
| 2190 | n/a | // re-execute ourselves requesting elevation. |
|---|
| 2191 | n/a | // Split into 2 functions - "should we elevate" and "spawn elevated" |
|---|
| 2192 | n/a | |
|---|
| 2193 | n/a | // Returns TRUE if we should spawn an elevated child |
|---|
| 2194 | n/a | BOOL NeedAutoUAC() |
|---|
| 2195 | n/a | { |
|---|
| 2196 | n/a | HKEY hk; |
|---|
| 2197 | n/a | char key_name[80]; |
|---|
| 2198 | n/a | // no Python version info == we can't know yet. |
|---|
| 2199 | n/a | if (target_version[0] == '\0') |
|---|
| 2200 | n/a | return FALSE; |
|---|
| 2201 | n/a | // see how python is current installed |
|---|
| 2202 | n/a | wsprintf(key_name, |
|---|
| 2203 | n/a | "Software\\Python\\PythonCore\\%s\\InstallPath", |
|---|
| 2204 | n/a | target_version); |
|---|
| 2205 | n/a | if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, |
|---|
| 2206 | n/a | key_name, 0, KEY_READ, &hk)) |
|---|
| 2207 | n/a | return FALSE; |
|---|
| 2208 | n/a | RegCloseKey(hk); |
|---|
| 2209 | n/a | // Python is installed in HKLM - we must elevate. |
|---|
| 2210 | n/a | return TRUE; |
|---|
| 2211 | n/a | } |
|---|
| 2212 | n/a | |
|---|
| 2213 | n/a | // Spawn ourself as an elevated application. On failure, a message is |
|---|
| 2214 | n/a | // displayed to the user - but this app will always terminate, even |
|---|
| 2215 | n/a | // on error. |
|---|
| 2216 | n/a | void SpawnUAC() |
|---|
| 2217 | n/a | { |
|---|
| 2218 | n/a | // interesting failure scenario that has been seen: initial executable |
|---|
| 2219 | n/a | // runs from a network drive - but once elevated, that network share |
|---|
| 2220 | n/a | // isn't seen, and ShellExecute fails with SE_ERR_ACCESSDENIED. |
|---|
| 2221 | n/a | int ret = (int)ShellExecute(0, "runas", modulename, "", NULL, |
|---|
| 2222 | n/a | SW_SHOWNORMAL); |
|---|
| 2223 | n/a | if (ret <= 32) { |
|---|
| 2224 | n/a | char msg[128]; |
|---|
| 2225 | n/a | wsprintf(msg, "Failed to start elevated process (ShellExecute returned %d)", ret); |
|---|
| 2226 | n/a | MessageBox(0, msg, "Setup", MB_OK | MB_ICONERROR); |
|---|
| 2227 | n/a | } |
|---|
| 2228 | n/a | } |
|---|
| 2229 | n/a | |
|---|
| 2230 | n/a | int DoInstall(void) |
|---|
| 2231 | n/a | { |
|---|
| 2232 | n/a | char ini_buffer[4096]; |
|---|
| 2233 | n/a | |
|---|
| 2234 | n/a | /* Read installation information */ |
|---|
| 2235 | n/a | GetPrivateProfileString("Setup", "title", "", ini_buffer, |
|---|
| 2236 | n/a | sizeof(ini_buffer), ini_file); |
|---|
| 2237 | n/a | unescape(title, ini_buffer, sizeof(title)); |
|---|
| 2238 | n/a | |
|---|
| 2239 | n/a | GetPrivateProfileString("Setup", "info", "", ini_buffer, |
|---|
| 2240 | n/a | sizeof(ini_buffer), ini_file); |
|---|
| 2241 | n/a | unescape(info, ini_buffer, sizeof(info)); |
|---|
| 2242 | n/a | |
|---|
| 2243 | n/a | GetPrivateProfileString("Setup", "build_info", "", build_info, |
|---|
| 2244 | n/a | sizeof(build_info), ini_file); |
|---|
| 2245 | n/a | |
|---|
| 2246 | n/a | pyc_compile = GetPrivateProfileInt("Setup", "target_compile", 1, |
|---|
| 2247 | n/a | ini_file); |
|---|
| 2248 | n/a | pyo_compile = GetPrivateProfileInt("Setup", "target_optimize", 1, |
|---|
| 2249 | n/a | ini_file); |
|---|
| 2250 | n/a | |
|---|
| 2251 | n/a | GetPrivateProfileString("Setup", "target_version", "", |
|---|
| 2252 | n/a | target_version, sizeof(target_version), |
|---|
| 2253 | n/a | ini_file); |
|---|
| 2254 | n/a | |
|---|
| 2255 | n/a | GetPrivateProfileString("metadata", "name", "", |
|---|
| 2256 | n/a | meta_name, sizeof(meta_name), |
|---|
| 2257 | n/a | ini_file); |
|---|
| 2258 | n/a | |
|---|
| 2259 | n/a | GetPrivateProfileString("Setup", "install_script", "", |
|---|
| 2260 | n/a | install_script, sizeof(install_script), |
|---|
| 2261 | n/a | ini_file); |
|---|
| 2262 | n/a | |
|---|
| 2263 | n/a | GetPrivateProfileString("Setup", "user_access_control", "", |
|---|
| 2264 | n/a | user_access_control, sizeof(user_access_control), ini_file); |
|---|
| 2265 | n/a | |
|---|
| 2266 | n/a | strcat(target_version, REGISTRY_SUFFIX_6432); |
|---|
| 2267 | n/a | |
|---|
| 2268 | n/a | // See if we need to do the Vista UAC magic. |
|---|
| 2269 | n/a | if (strcmp(user_access_control, "force")==0) { |
|---|
| 2270 | n/a | if (!MyIsUserAnAdmin()) { |
|---|
| 2271 | n/a | SpawnUAC(); |
|---|
| 2272 | n/a | return 0; |
|---|
| 2273 | n/a | } |
|---|
| 2274 | n/a | // already admin - keep going |
|---|
| 2275 | n/a | } else if (strcmp(user_access_control, "auto")==0) { |
|---|
| 2276 | n/a | // Check if it looks like we need UAC control, based |
|---|
| 2277 | n/a | // on how Python itself was installed. |
|---|
| 2278 | n/a | if (!MyIsUserAnAdmin() && NeedAutoUAC()) { |
|---|
| 2279 | n/a | SpawnUAC(); |
|---|
| 2280 | n/a | return 0; |
|---|
| 2281 | n/a | } |
|---|
| 2282 | n/a | } else { |
|---|
| 2283 | n/a | // display a warning about unknown values - only the developer |
|---|
| 2284 | n/a | // of the extension will see it (until they fix it!) |
|---|
| 2285 | n/a | if (user_access_control[0] && strcmp(user_access_control, "none") != 0) { |
|---|
| 2286 | n/a | MessageBox(GetFocus(), "Bad user_access_control value", "oops", MB_OK); |
|---|
| 2287 | n/a | // nothing to do. |
|---|
| 2288 | n/a | } |
|---|
| 2289 | n/a | } |
|---|
| 2290 | n/a | |
|---|
| 2291 | n/a | hwndMain = CreateBackground(title); |
|---|
| 2292 | n/a | |
|---|
| 2293 | n/a | RunWizard(hwndMain); |
|---|
| 2294 | n/a | |
|---|
| 2295 | n/a | /* Clean up */ |
|---|
| 2296 | n/a | UnmapViewOfFile(arc_data); |
|---|
| 2297 | n/a | if (ini_file) |
|---|
| 2298 | n/a | DeleteFile(ini_file); |
|---|
| 2299 | n/a | |
|---|
| 2300 | n/a | if (hBitmap) |
|---|
| 2301 | n/a | DeleteObject(hBitmap); |
|---|
| 2302 | n/a | |
|---|
| 2303 | n/a | return 0; |
|---|
| 2304 | n/a | } |
|---|
| 2305 | n/a | |
|---|
| 2306 | n/a | /*********************** uninstall section ******************************/ |
|---|
| 2307 | n/a | |
|---|
| 2308 | n/a | static int compare(const void *p1, const void *p2) |
|---|
| 2309 | n/a | { |
|---|
| 2310 | n/a | return strcmp(*(char **)p2, *(char **)p1); |
|---|
| 2311 | n/a | } |
|---|
| 2312 | n/a | |
|---|
| 2313 | n/a | /* |
|---|
| 2314 | n/a | * Commit suicide (remove the uninstaller itself). |
|---|
| 2315 | n/a | * |
|---|
| 2316 | n/a | * Create a batch file to first remove the uninstaller |
|---|
| 2317 | n/a | * (will succeed after it has finished), then the batch file itself. |
|---|
| 2318 | n/a | * |
|---|
| 2319 | n/a | * This technique has been demonstrated by Jeff Richter, |
|---|
| 2320 | n/a | * MSJ 1/1996 |
|---|
| 2321 | n/a | */ |
|---|
| 2322 | n/a | void remove_exe(void) |
|---|
| 2323 | n/a | { |
|---|
| 2324 | n/a | char exename[_MAX_PATH]; |
|---|
| 2325 | n/a | char batname[_MAX_PATH]; |
|---|
| 2326 | n/a | FILE *fp; |
|---|
| 2327 | n/a | STARTUPINFO si; |
|---|
| 2328 | n/a | PROCESS_INFORMATION pi; |
|---|
| 2329 | n/a | |
|---|
| 2330 | n/a | GetModuleFileName(NULL, exename, sizeof(exename)); |
|---|
| 2331 | n/a | sprintf(batname, "%s.bat", exename); |
|---|
| 2332 | n/a | fp = fopen(batname, "w"); |
|---|
| 2333 | n/a | fprintf(fp, ":Repeat\n"); |
|---|
| 2334 | n/a | fprintf(fp, "del \"%s\"\n", exename); |
|---|
| 2335 | n/a | fprintf(fp, "if exist \"%s\" goto Repeat\n", exename); |
|---|
| 2336 | n/a | fprintf(fp, "del \"%s\"\n", batname); |
|---|
| 2337 | n/a | fclose(fp); |
|---|
| 2338 | n/a | |
|---|
| 2339 | n/a | ZeroMemory(&si, sizeof(si)); |
|---|
| 2340 | n/a | si.cb = sizeof(si); |
|---|
| 2341 | n/a | si.dwFlags = STARTF_USESHOWWINDOW; |
|---|
| 2342 | n/a | si.wShowWindow = SW_HIDE; |
|---|
| 2343 | n/a | if (CreateProcess(NULL, |
|---|
| 2344 | n/a | batname, |
|---|
| 2345 | n/a | NULL, |
|---|
| 2346 | n/a | NULL, |
|---|
| 2347 | n/a | FALSE, |
|---|
| 2348 | n/a | CREATE_SUSPENDED | IDLE_PRIORITY_CLASS, |
|---|
| 2349 | n/a | NULL, |
|---|
| 2350 | n/a | "\\", |
|---|
| 2351 | n/a | &si, |
|---|
| 2352 | n/a | &pi)) { |
|---|
| 2353 | n/a | SetThreadPriority(pi.hThread, THREAD_PRIORITY_IDLE); |
|---|
| 2354 | n/a | SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); |
|---|
| 2355 | n/a | SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); |
|---|
| 2356 | n/a | CloseHandle(pi.hProcess); |
|---|
| 2357 | n/a | ResumeThread(pi.hThread); |
|---|
| 2358 | n/a | CloseHandle(pi.hThread); |
|---|
| 2359 | n/a | } |
|---|
| 2360 | n/a | } |
|---|
| 2361 | n/a | |
|---|
| 2362 | n/a | void DeleteRegistryKey(char *string) |
|---|
| 2363 | n/a | { |
|---|
| 2364 | n/a | char *keyname; |
|---|
| 2365 | n/a | char *subkeyname; |
|---|
| 2366 | n/a | char *delim; |
|---|
| 2367 | n/a | HKEY hKey; |
|---|
| 2368 | n/a | long result; |
|---|
| 2369 | n/a | char *line; |
|---|
| 2370 | n/a | |
|---|
| 2371 | n/a | line = strdup(string); /* so we can change it */ |
|---|
| 2372 | n/a | |
|---|
| 2373 | n/a | keyname = strchr(line, '['); |
|---|
| 2374 | n/a | if (!keyname) |
|---|
| 2375 | n/a | return; |
|---|
| 2376 | n/a | ++keyname; |
|---|
| 2377 | n/a | |
|---|
| 2378 | n/a | subkeyname = strchr(keyname, ']'); |
|---|
| 2379 | n/a | if (!subkeyname) |
|---|
| 2380 | n/a | return; |
|---|
| 2381 | n/a | *subkeyname++='\0'; |
|---|
| 2382 | n/a | delim = strchr(subkeyname, '\n'); |
|---|
| 2383 | n/a | if (delim) |
|---|
| 2384 | n/a | *delim = '\0'; |
|---|
| 2385 | n/a | |
|---|
| 2386 | n/a | result = RegOpenKeyEx(hkey_root, |
|---|
| 2387 | n/a | keyname, |
|---|
| 2388 | n/a | 0, |
|---|
| 2389 | n/a | KEY_WRITE, |
|---|
| 2390 | n/a | &hKey); |
|---|
| 2391 | n/a | |
|---|
| 2392 | n/a | if (result != ERROR_SUCCESS) |
|---|
| 2393 | n/a | MessageBox(GetFocus(), string, "Could not open key", MB_OK); |
|---|
| 2394 | n/a | else { |
|---|
| 2395 | n/a | result = RegDeleteKey(hKey, subkeyname); |
|---|
| 2396 | n/a | if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND) |
|---|
| 2397 | n/a | MessageBox(GetFocus(), string, "Could not delete key", MB_OK); |
|---|
| 2398 | n/a | RegCloseKey(hKey); |
|---|
| 2399 | n/a | } |
|---|
| 2400 | n/a | free(line); |
|---|
| 2401 | n/a | } |
|---|
| 2402 | n/a | |
|---|
| 2403 | n/a | void DeleteRegistryValue(char *string) |
|---|
| 2404 | n/a | { |
|---|
| 2405 | n/a | char *keyname; |
|---|
| 2406 | n/a | char *valuename; |
|---|
| 2407 | n/a | char *value; |
|---|
| 2408 | n/a | HKEY hKey; |
|---|
| 2409 | n/a | long result; |
|---|
| 2410 | n/a | char *line; |
|---|
| 2411 | n/a | |
|---|
| 2412 | n/a | line = strdup(string); /* so we can change it */ |
|---|
| 2413 | n/a | |
|---|
| 2414 | n/a | /* Format is 'Reg DB Value: [key]name=value' */ |
|---|
| 2415 | n/a | keyname = strchr(line, '['); |
|---|
| 2416 | n/a | if (!keyname) |
|---|
| 2417 | n/a | return; |
|---|
| 2418 | n/a | ++keyname; |
|---|
| 2419 | n/a | valuename = strchr(keyname, ']'); |
|---|
| 2420 | n/a | if (!valuename) |
|---|
| 2421 | n/a | return; |
|---|
| 2422 | n/a | *valuename++ = '\0'; |
|---|
| 2423 | n/a | value = strchr(valuename, '='); |
|---|
| 2424 | n/a | if (!value) |
|---|
| 2425 | n/a | return; |
|---|
| 2426 | n/a | |
|---|
| 2427 | n/a | *value++ = '\0'; |
|---|
| 2428 | n/a | |
|---|
| 2429 | n/a | result = RegOpenKeyEx(hkey_root, |
|---|
| 2430 | n/a | keyname, |
|---|
| 2431 | n/a | 0, |
|---|
| 2432 | n/a | KEY_WRITE, |
|---|
| 2433 | n/a | &hKey); |
|---|
| 2434 | n/a | if (result != ERROR_SUCCESS) |
|---|
| 2435 | n/a | MessageBox(GetFocus(), string, "Could not open key", MB_OK); |
|---|
| 2436 | n/a | else { |
|---|
| 2437 | n/a | result = RegDeleteValue(hKey, valuename); |
|---|
| 2438 | n/a | if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND) |
|---|
| 2439 | n/a | MessageBox(GetFocus(), string, "Could not delete value", MB_OK); |
|---|
| 2440 | n/a | RegCloseKey(hKey); |
|---|
| 2441 | n/a | } |
|---|
| 2442 | n/a | free(line); |
|---|
| 2443 | n/a | } |
|---|
| 2444 | n/a | |
|---|
| 2445 | n/a | BOOL MyDeleteFile(char *line) |
|---|
| 2446 | n/a | { |
|---|
| 2447 | n/a | char *pathname = strchr(line, ':'); |
|---|
| 2448 | n/a | if (!pathname) |
|---|
| 2449 | n/a | return FALSE; |
|---|
| 2450 | n/a | ++pathname; |
|---|
| 2451 | n/a | while (isspace(*pathname)) |
|---|
| 2452 | n/a | ++pathname; |
|---|
| 2453 | n/a | return DeleteFile(pathname); |
|---|
| 2454 | n/a | } |
|---|
| 2455 | n/a | |
|---|
| 2456 | n/a | BOOL MyRemoveDirectory(char *line) |
|---|
| 2457 | n/a | { |
|---|
| 2458 | n/a | char *pathname = strchr(line, ':'); |
|---|
| 2459 | n/a | if (!pathname) |
|---|
| 2460 | n/a | return FALSE; |
|---|
| 2461 | n/a | ++pathname; |
|---|
| 2462 | n/a | while (isspace(*pathname)) |
|---|
| 2463 | n/a | ++pathname; |
|---|
| 2464 | n/a | return RemoveDirectory(pathname); |
|---|
| 2465 | n/a | } |
|---|
| 2466 | n/a | |
|---|
| 2467 | n/a | BOOL Run_RemoveScript(char *line) |
|---|
| 2468 | n/a | { |
|---|
| 2469 | n/a | char *dllname; |
|---|
| 2470 | n/a | char *scriptname; |
|---|
| 2471 | n/a | static char lastscript[MAX_PATH]; |
|---|
| 2472 | n/a | |
|---|
| 2473 | n/a | /* Format is 'Run Scripts: [pythondll]scriptname' */ |
|---|
| 2474 | n/a | /* XXX Currently, pythondll carries no path!!! */ |
|---|
| 2475 | n/a | dllname = strchr(line, '['); |
|---|
| 2476 | n/a | if (!dllname) |
|---|
| 2477 | n/a | return FALSE; |
|---|
| 2478 | n/a | ++dllname; |
|---|
| 2479 | n/a | scriptname = strchr(dllname, ']'); |
|---|
| 2480 | n/a | if (!scriptname) |
|---|
| 2481 | n/a | return FALSE; |
|---|
| 2482 | n/a | *scriptname++ = '\0'; |
|---|
| 2483 | n/a | /* this function may be called more than one time with the same |
|---|
| 2484 | n/a | script, only run it one time */ |
|---|
| 2485 | n/a | if (strcmp(lastscript, scriptname)) { |
|---|
| 2486 | n/a | char *argv[3] = {NULL, "-remove", NULL}; |
|---|
| 2487 | n/a | char *buffer = NULL; |
|---|
| 2488 | n/a | |
|---|
| 2489 | n/a | argv[0] = scriptname; |
|---|
| 2490 | n/a | |
|---|
| 2491 | n/a | if (0 != run_installscript(scriptname, 2, argv, &buffer)) |
|---|
| 2492 | n/a | fprintf(stderr, "*** Could not run installation script ***"); |
|---|
| 2493 | n/a | |
|---|
| 2494 | n/a | if (buffer && buffer[0]) |
|---|
| 2495 | n/a | MessageBox(GetFocus(), buffer, "uninstall-script", MB_OK); |
|---|
| 2496 | n/a | free(buffer); |
|---|
| 2497 | n/a | |
|---|
| 2498 | n/a | strcpy(lastscript, scriptname); |
|---|
| 2499 | n/a | } |
|---|
| 2500 | n/a | return TRUE; |
|---|
| 2501 | n/a | } |
|---|
| 2502 | n/a | |
|---|
| 2503 | n/a | int DoUninstall(int argc, char **argv) |
|---|
| 2504 | n/a | { |
|---|
| 2505 | n/a | FILE *logfile; |
|---|
| 2506 | n/a | char buffer[4096]; |
|---|
| 2507 | n/a | int nLines = 0; |
|---|
| 2508 | n/a | int i; |
|---|
| 2509 | n/a | char *cp; |
|---|
| 2510 | n/a | int nFiles = 0; |
|---|
| 2511 | n/a | int nDirs = 0; |
|---|
| 2512 | n/a | int nErrors = 0; |
|---|
| 2513 | n/a | char **lines; |
|---|
| 2514 | n/a | int lines_buffer_size = 10; |
|---|
| 2515 | n/a | |
|---|
| 2516 | n/a | if (argc != 3) { |
|---|
| 2517 | n/a | MessageBox(NULL, |
|---|
| 2518 | n/a | "Wrong number of args", |
|---|
| 2519 | n/a | NULL, |
|---|
| 2520 | n/a | MB_OK); |
|---|
| 2521 | n/a | return 1; /* Error */ |
|---|
| 2522 | n/a | } |
|---|
| 2523 | n/a | if (strcmp(argv[1], "-u")) { |
|---|
| 2524 | n/a | MessageBox(NULL, |
|---|
| 2525 | n/a | "2. arg is not -u", |
|---|
| 2526 | n/a | NULL, |
|---|
| 2527 | n/a | MB_OK); |
|---|
| 2528 | n/a | return 1; /* Error */ |
|---|
| 2529 | n/a | } |
|---|
| 2530 | n/a | |
|---|
| 2531 | n/a | logfile = fopen(argv[2], "r"); |
|---|
| 2532 | n/a | if (!logfile) { |
|---|
| 2533 | n/a | MessageBox(NULL, |
|---|
| 2534 | n/a | "could not open logfile", |
|---|
| 2535 | n/a | NULL, |
|---|
| 2536 | n/a | MB_OK); |
|---|
| 2537 | n/a | return 1; /* Error */ |
|---|
| 2538 | n/a | } |
|---|
| 2539 | n/a | |
|---|
| 2540 | n/a | lines = (char **)malloc(sizeof(char *) * lines_buffer_size); |
|---|
| 2541 | n/a | if (!lines) |
|---|
| 2542 | n/a | return SystemError(0, "Out of memory"); |
|---|
| 2543 | n/a | |
|---|
| 2544 | n/a | /* Read the whole logfile, realloacting the buffer */ |
|---|
| 2545 | n/a | while (fgets(buffer, sizeof(buffer), logfile)) { |
|---|
| 2546 | n/a | int len = strlen(buffer); |
|---|
| 2547 | n/a | /* remove trailing white space */ |
|---|
| 2548 | n/a | while (isspace(buffer[len-1])) |
|---|
| 2549 | n/a | len -= 1; |
|---|
| 2550 | n/a | buffer[len] = '\0'; |
|---|
| 2551 | n/a | lines[nLines++] = strdup(buffer); |
|---|
| 2552 | n/a | if (nLines >= lines_buffer_size) { |
|---|
| 2553 | n/a | lines_buffer_size += 10; |
|---|
| 2554 | n/a | lines = (char **)realloc(lines, |
|---|
| 2555 | n/a | sizeof(char *) * lines_buffer_size); |
|---|
| 2556 | n/a | if (!lines) |
|---|
| 2557 | n/a | return SystemError(0, "Out of memory"); |
|---|
| 2558 | n/a | } |
|---|
| 2559 | n/a | } |
|---|
| 2560 | n/a | fclose(logfile); |
|---|
| 2561 | n/a | |
|---|
| 2562 | n/a | /* Sort all the lines, so that highest 3-digit codes are first */ |
|---|
| 2563 | n/a | qsort(&lines[0], nLines, sizeof(char *), |
|---|
| 2564 | n/a | compare); |
|---|
| 2565 | n/a | |
|---|
| 2566 | n/a | if (IDYES != MessageBox(NULL, |
|---|
| 2567 | n/a | "Are you sure you want to remove\n" |
|---|
| 2568 | n/a | "this package from your computer?", |
|---|
| 2569 | n/a | "Please confirm", |
|---|
| 2570 | n/a | MB_YESNO | MB_ICONQUESTION)) |
|---|
| 2571 | n/a | return 0; |
|---|
| 2572 | n/a | |
|---|
| 2573 | n/a | hkey_root = HKEY_LOCAL_MACHINE; |
|---|
| 2574 | n/a | cp = ""; |
|---|
| 2575 | n/a | for (i = 0; i < nLines; ++i) { |
|---|
| 2576 | n/a | /* Ignore duplicate lines */ |
|---|
| 2577 | n/a | if (strcmp(cp, lines[i])) { |
|---|
| 2578 | n/a | int ign; |
|---|
| 2579 | n/a | cp = lines[i]; |
|---|
| 2580 | n/a | /* Parse the lines */ |
|---|
| 2581 | n/a | if (2 == sscanf(cp, "%d Root Key: %s", &ign, &buffer)) { |
|---|
| 2582 | n/a | if (strcmp(buffer, "HKEY_CURRENT_USER")==0) |
|---|
| 2583 | n/a | hkey_root = HKEY_CURRENT_USER; |
|---|
| 2584 | n/a | else { |
|---|
| 2585 | n/a | // HKLM - check they have permissions. |
|---|
| 2586 | n/a | if (!HasLocalMachinePrivs()) { |
|---|
| 2587 | n/a | MessageBox(GetFocus(), |
|---|
| 2588 | n/a | "You do not seem to have sufficient access rights\n" |
|---|
| 2589 | n/a | "on this machine to uninstall this software", |
|---|
| 2590 | n/a | NULL, |
|---|
| 2591 | n/a | MB_OK | MB_ICONSTOP); |
|---|
| 2592 | n/a | return 1; /* Error */ |
|---|
| 2593 | n/a | } |
|---|
| 2594 | n/a | } |
|---|
| 2595 | n/a | } else if (2 == sscanf(cp, "%d Made Dir: %s", &ign, &buffer)) { |
|---|
| 2596 | n/a | if (MyRemoveDirectory(cp)) |
|---|
| 2597 | n/a | ++nDirs; |
|---|
| 2598 | n/a | else { |
|---|
| 2599 | n/a | int code = GetLastError(); |
|---|
| 2600 | n/a | if (code != 2 && code != 3) { /* file or path not found */ |
|---|
| 2601 | n/a | ++nErrors; |
|---|
| 2602 | n/a | } |
|---|
| 2603 | n/a | } |
|---|
| 2604 | n/a | } else if (2 == sscanf(cp, "%d File Copy: %s", &ign, &buffer)) { |
|---|
| 2605 | n/a | if (MyDeleteFile(cp)) |
|---|
| 2606 | n/a | ++nFiles; |
|---|
| 2607 | n/a | else { |
|---|
| 2608 | n/a | int code = GetLastError(); |
|---|
| 2609 | n/a | if (code != 2 && code != 3) { /* file or path not found */ |
|---|
| 2610 | n/a | ++nErrors; |
|---|
| 2611 | n/a | } |
|---|
| 2612 | n/a | } |
|---|
| 2613 | n/a | } else if (2 == sscanf(cp, "%d File Overwrite: %s", &ign, &buffer)) { |
|---|
| 2614 | n/a | if (MyDeleteFile(cp)) |
|---|
| 2615 | n/a | ++nFiles; |
|---|
| 2616 | n/a | else { |
|---|
| 2617 | n/a | int code = GetLastError(); |
|---|
| 2618 | n/a | if (code != 2 && code != 3) { /* file or path not found */ |
|---|
| 2619 | n/a | ++nErrors; |
|---|
| 2620 | n/a | } |
|---|
| 2621 | n/a | } |
|---|
| 2622 | n/a | } else if (2 == sscanf(cp, "%d Reg DB Key: %s", &ign, &buffer)) { |
|---|
| 2623 | n/a | DeleteRegistryKey(cp); |
|---|
| 2624 | n/a | } else if (2 == sscanf(cp, "%d Reg DB Value: %s", &ign, &buffer)) { |
|---|
| 2625 | n/a | DeleteRegistryValue(cp); |
|---|
| 2626 | n/a | } else if (2 == sscanf(cp, "%d Run Script: %s", &ign, &buffer)) { |
|---|
| 2627 | n/a | Run_RemoveScript(cp); |
|---|
| 2628 | n/a | } |
|---|
| 2629 | n/a | } |
|---|
| 2630 | n/a | } |
|---|
| 2631 | n/a | |
|---|
| 2632 | n/a | if (DeleteFile(argv[2])) { |
|---|
| 2633 | n/a | ++nFiles; |
|---|
| 2634 | n/a | } else { |
|---|
| 2635 | n/a | ++nErrors; |
|---|
| 2636 | n/a | SystemError(GetLastError(), argv[2]); |
|---|
| 2637 | n/a | } |
|---|
| 2638 | n/a | if (nErrors) |
|---|
| 2639 | n/a | wsprintf(buffer, |
|---|
| 2640 | n/a | "%d files and %d directories removed\n" |
|---|
| 2641 | n/a | "%d files or directories could not be removed", |
|---|
| 2642 | n/a | nFiles, nDirs, nErrors); |
|---|
| 2643 | n/a | else |
|---|
| 2644 | n/a | wsprintf(buffer, "%d files and %d directories removed", |
|---|
| 2645 | n/a | nFiles, nDirs); |
|---|
| 2646 | n/a | MessageBox(NULL, buffer, "Uninstall Finished!", |
|---|
| 2647 | n/a | MB_OK | MB_ICONINFORMATION); |
|---|
| 2648 | n/a | remove_exe(); |
|---|
| 2649 | n/a | return 0; |
|---|
| 2650 | n/a | } |
|---|
| 2651 | n/a | |
|---|
| 2652 | n/a | int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, |
|---|
| 2653 | n/a | LPSTR lpszCmdLine, INT nCmdShow) |
|---|
| 2654 | n/a | { |
|---|
| 2655 | n/a | extern int __argc; |
|---|
| 2656 | n/a | extern char **__argv; |
|---|
| 2657 | n/a | char *basename; |
|---|
| 2658 | n/a | |
|---|
| 2659 | n/a | GetModuleFileName(NULL, modulename, sizeof(modulename)); |
|---|
| 2660 | n/a | GetModuleFileNameW(NULL, wmodulename, sizeof(wmodulename)/sizeof(wmodulename[0])); |
|---|
| 2661 | n/a | |
|---|
| 2662 | n/a | /* Map the executable file to memory */ |
|---|
| 2663 | n/a | arc_data = MapExistingFile(modulename, &arc_size); |
|---|
| 2664 | n/a | if (!arc_data) { |
|---|
| 2665 | n/a | SystemError(GetLastError(), "Could not open archive"); |
|---|
| 2666 | n/a | return 1; |
|---|
| 2667 | n/a | } |
|---|
| 2668 | n/a | |
|---|
| 2669 | n/a | /* OK. So this program can act as installer (self-extracting |
|---|
| 2670 | n/a | * zip-file, or as uninstaller when started with '-u logfile' |
|---|
| 2671 | n/a | * command line flags. |
|---|
| 2672 | n/a | * |
|---|
| 2673 | n/a | * The installer is usually started without command line flags, |
|---|
| 2674 | n/a | * and the uninstaller is usually started with the '-u logfile' |
|---|
| 2675 | n/a | * flag. What to do if some innocent user double-clicks the |
|---|
| 2676 | n/a | * exe-file? |
|---|
| 2677 | n/a | * The following implements a defensive strategy... |
|---|
| 2678 | n/a | */ |
|---|
| 2679 | n/a | |
|---|
| 2680 | n/a | /* Try to extract the configuration data into a temporary file */ |
|---|
| 2681 | n/a | if (ExtractInstallData(arc_data, arc_size, &exe_size, |
|---|
| 2682 | n/a | &ini_file, &pre_install_script)) |
|---|
| 2683 | n/a | return DoInstall(); |
|---|
| 2684 | n/a | |
|---|
| 2685 | n/a | if (!ini_file && __argc > 1) { |
|---|
| 2686 | n/a | return DoUninstall(__argc, __argv); |
|---|
| 2687 | n/a | } |
|---|
| 2688 | n/a | |
|---|
| 2689 | n/a | |
|---|
| 2690 | n/a | basename = strrchr(modulename, '\\'); |
|---|
| 2691 | n/a | if (basename) |
|---|
| 2692 | n/a | ++basename; |
|---|
| 2693 | n/a | |
|---|
| 2694 | n/a | /* Last guess about the purpose of this program */ |
|---|
| 2695 | n/a | if (basename && (0 == strncmp(basename, "Remove", 6))) |
|---|
| 2696 | n/a | SystemError(0, "This program is normally started by windows"); |
|---|
| 2697 | n/a | else |
|---|
| 2698 | n/a | SystemError(0, "Setup program invalid or damaged"); |
|---|
| 2699 | n/a | return 1; |
|---|
| 2700 | n/a | } |
|---|