2
* Copyright (c) 2007, 2008 Agostino Russo
4
* Written by Agostino Russo <agostino.russo@gmail.com>
5
* Heavily inspired by exemaker from Fredrik Lundh
7
* pylauncher is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as
9
* published by the Free Software Foundation; either version 2 of
10
* the License, or (at your option) any later version.
12
* pylauncher is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21
* Pylauncher extracts a lzma archive that can be appended to this
22
* executable and containing:
24
* ./main.pyo # main script to run
26
* ./lib # python modules, ./lib is added to PYTHONPATH
27
* ./python.dll # python dll
29
* then it loads python.dll and runs main.py within that. A python
30
* script can be packed in the appropriate format using pack.py
32
* Note that the current implementation assumes that the python modules
33
* are bytecompiled and optimized (*.pyo)
41
#include "deletedir.h"
48
main(int ac, char **av)
50
//~ printf("header: starting\n");
52
char message[MAX_PATH + 1000];
53
char pylauncher[MAX_PATH];
54
char currentdir[MAX_PATH];
55
char exefile[MAX_PATH];
56
char tmpdir[MAX_PATH];
57
char targetdir[MAX_PATH];
59
GetModuleFileName(NULL, exefile, sizeof(exefile));
62
GetTempPath(MAX_PATH, tmpdir);
63
GetTempFileName(tmpdir, "pyl", 0, targetdir);
64
DeleteFile(targetdir);
65
getcwd(currentdir, MAX_PATH);
66
CreateDirectory(targetdir, NULL);
69
//Extract LZMA bundled archive
70
if (unpack(exefile)) {
71
sprintf(message, "Cannot unpack %s\n", exefile);
76
sprintf(pylauncher, "%s.exe", targetdir);
77
if (!CopyFile("pylauncher.exe", pylauncher, FALSE)){
78
sprintf(message, "Cannot copy %s\n", pylauncher);
82
HANDLE exe_handle = OpenProcess(SYNCHRONIZE, TRUE, GetCurrentProcessId());
83
HANDLE pylauncher_handle = CreateFile(pylauncher, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, NULL);
85
cmd = (char *)malloc((strlen(pylauncher) + strlen(targetdir) + strlen(exefile) + 9) * sizeof(char));
86
sprintf(cmd, "\"%s\" \"%s\" \"%s\"", pylauncher, targetdir, exefile);
88
for (i = 1; i < (DWORD) ac; i++){
89
cmd = concat(cmd, " ", true);
90
cmd = concat(cmd, av[i], true);
93
ZeroMemory(&si, sizeof(si));
95
PROCESS_INFORMATION pi;
96
//~ printf("header running cmd: %s\n", cmd);
97
if(!CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)){
98
strcpy(message, "Failed to run pylauncher\n");
104
Sleep(1000); // Give time to the new process to start
105
//~ printf("header: finishing\n");
106
CloseHandle(exe_handle);
107
CloseHandle(pylauncher_handle);
111
MessageBox(NULL, message, "Internal error", MB_ICONERROR | MB_OK);
112
//TBD We should delete the targetdir but might be risky
117
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
118
LPSTR lpCmdLine, int nCmdShow)
120
return main(__argc, __argv);