~usb-creator-hackers/usb-creator/trunk

« back to all changes in this revision

Viewing changes to tools/pylauncher/header.c

  • Committer: Evan Dandrea
  • Date: 2009-07-23 13:58:53 UTC
  • mto: This revision was merged to the branch mainline in revision 133.
  • Revision ID: evan.dandrea@canonical.com-20090723135853-zjeg9r22xyuk1kz8
Support for building a Windows executable.  Run make build_windows or make test_windows.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2007, 2008 Agostino Russo
 
3
 *
 
4
 *  Written by Agostino Russo <agostino.russo@gmail.com>
 
5
 *  Heavily inspired by exemaker from Fredrik Lundh
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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/>.
 
19
 *
 
20
 * DESCRIPTION:
 
21
 * Pylauncher extracts a lzma archive that can be appended to this
 
22
 * executable and containing:
 
23
 *
 
24
 * ./main.pyo           # main script to run
 
25
 * ./data                  # data
 
26
 * ./lib                     # python modules, ./lib is added to PYTHONPATH
 
27
 * ./python.dll         # python dll
 
28
 *
 
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
 
31
 *
 
32
 * Note that the current implementation assumes that the python modules
 
33
 * are bytecompiled and  optimized (*.pyo)
 
34
 *
 
35
 */
 
36
 
 
37
#include <stdio.h>
 
38
#include <stdlib.h>
 
39
#include "windows.h"
 
40
#include "unpack.h"
 
41
#include "deletedir.h"
 
42
#include "str.h"
 
43
 
 
44
#define true 1
 
45
#define false 0
 
46
 
 
47
int __cdecl
 
48
main(int ac, char **av)
 
49
{
 
50
    //~ printf("header: starting\n");
 
51
    char *cmd;
 
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];
 
58
 
 
59
    GetModuleFileName(NULL, exefile, sizeof(exefile));
 
60
 
 
61
    //Create targetdir
 
62
    GetTempPath(MAX_PATH, tmpdir);
 
63
    GetTempFileName(tmpdir, "pyl", 0, targetdir);
 
64
    DeleteFile(targetdir);
 
65
    getcwd(currentdir, MAX_PATH);
 
66
    CreateDirectory(targetdir, NULL);
 
67
    chdir(targetdir);
 
68
 
 
69
    //Extract LZMA bundled archive
 
70
    if (unpack(exefile)) {
 
71
        sprintf(message, "Cannot unpack %s\n", exefile);
 
72
        goto error;
 
73
    }
 
74
 
 
75
    //Copy pylauncher.exe
 
76
    sprintf(pylauncher, "%s.exe", targetdir);
 
77
    if (!CopyFile("pylauncher.exe", pylauncher, FALSE)){
 
78
        sprintf(message, "Cannot copy %s\n", pylauncher);
 
79
        goto error;
 
80
    }
 
81
 
 
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);
 
84
 
 
85
    cmd = (char *)malloc((strlen(pylauncher) + strlen(targetdir) + strlen(exefile) + 9) * sizeof(char));
 
86
    sprintf(cmd, "\"%s\" \"%s\" \"%s\"", pylauncher, targetdir, exefile);
 
87
    DWORD i;
 
88
    for (i = 1; i < (DWORD) ac; i++){
 
89
        cmd = concat(cmd, " ", true);
 
90
        cmd = concat(cmd, av[i], true);
 
91
    }
 
92
    STARTUPINFO si;
 
93
    ZeroMemory(&si, sizeof(si));
 
94
    si.cb = 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");
 
99
        free(cmd);
 
100
        goto error;
 
101
    }
 
102
    free(cmd);
 
103
 
 
104
    Sleep(1000); // Give time to the new process to start
 
105
    //~ printf("header: finishing\n");
 
106
    CloseHandle(exe_handle);
 
107
    CloseHandle(pylauncher_handle);
 
108
    return 0;
 
109
 
 
110
error:
 
111
    MessageBox(NULL, message, "Internal error", MB_ICONERROR | MB_OK);
 
112
    //TBD We should delete the targetdir but might be risky
 
113
    return 1;
 
114
}
 
115
 
 
116
int WINAPI
 
117
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 
118
        LPSTR lpCmdLine, int nCmdShow)
 
119
{
 
120
    return main(__argc, __argv);
 
121
}