srcanamdw/codescanner/pyinstaller/source/windows/winmain.c
author noe\swadi
Thu, 18 Feb 2010 12:29:02 +0530
changeset 1 22878952f6e2
permissions -rw-r--r--
Committing the CodeScanner Core tool This component has been moved from the StaticAnaApps package. BUG : 5889 (http://developer.symbian.org/webbugs/show_bug.cgi?id=5889).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     1
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     2
 * Bootloader for a packed executable.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     3
 * Copyright (C) 2005, Giovanni Bajo
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     4
 * Based on previous work under copyright (c) 2002 McMillan Enterprises, Inc.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     5
 *
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     7
 * modify it under the terms of the GNU General Public License
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     8
 * as published by the Free Software Foundation; either version 2
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     9
 * of the License, or (at your option) any later version.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    10
 *
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    11
 * In addition to the permissions in the GNU General Public License, the
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    12
 * authors give you unlimited permission to link or embed the compiled
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    13
 * version of this file into combinations with other programs, and to
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    14
 * distribute those combinations without any restriction coming from the
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    15
 * use of this file. (The General Public License restrictions do apply in
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    16
 * other respects; for example, they cover modification of the file, and
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    17
 * distribution when not linked into a combine executable.)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    18
 *
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    19
 * This program is distributed in the hope that it will be useful,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    22
 * GNU General Public License for more details.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    23
 *
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    24
 * You should have received a copy of the GNU General Public License
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    25
 * along with this program; if not, write to the Free Software
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    26
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    27
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    28
#include "launch.h"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    29
#include <windows.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    30
#include <commctrl.h> // InitCommonControls
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    31
#include <signal.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    33
int relaunch(char *thisfile, char *workpath)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    34
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    35
	char envvar[_MAX_PATH + 12];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    36
	SECURITY_ATTRIBUTES sa;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    37
	STARTUPINFO si;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    38
	PROCESS_INFORMATION pi;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    39
	int rc = 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    40
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    41
	// the parent process should ignore all signals it can
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    42
	signal(SIGABRT, SIG_IGN);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    43
	signal(SIGINT, SIG_IGN);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    44
	signal(SIGTERM, SIG_IGN);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    45
	signal(SIGBREAK, SIG_IGN);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    46
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    47
	VS("Setting up to run child\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    48
	sa.nLength = sizeof(sa);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    49
	sa.lpSecurityDescriptor = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    50
	sa.bInheritHandle = TRUE;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    51
	GetStartupInfo(&si);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    52
	si.lpReserved = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    53
	si.lpDesktop = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    54
	si.lpTitle = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    55
	si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    56
	si.wShowWindow = SW_NORMAL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    57
	si.hStdInput = (void*)_get_osfhandle(fileno(stdin));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    58
	si.hStdOutput = (void*)_get_osfhandle(fileno(stdout));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    59
	si.hStdError = (void*)_get_osfhandle(fileno(stderr));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    60
	/* tell pass 2 where we extracted to */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    61
	VS("Setting magic environment var\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    62
	strcpy(envvar, "_MEIPASS2=");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    63
	strcat(envvar, workpath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    64
	_putenv(envvar);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    65
	VS("Creating child process\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    66
	if (CreateProcess( 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    67
			thisfile, // pointer to name of executable module 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    68
			GetCommandLine(),  // pointer to command line string 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    69
			&sa,  // pointer to process security attributes 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    70
			NULL,  // pointer to thread security attributes 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    71
			TRUE,  // handle inheritance flag 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    72
			0,  // creation flags 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    73
			NULL,  // pointer to new environment block 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    74
			NULL,  // pointer to current directory name 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    75
			&si,  // pointer to STARTUPINFO 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    76
			&pi  // pointer to PROCESS_INFORMATION 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    77
			)) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    78
		VS("Waiting for child process to finish...\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    79
		WaitForSingleObject(pi.hProcess, INFINITE);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    80
		GetExitCodeProcess(pi.hProcess, (unsigned long *)&rc);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    81
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    82
	else {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    83
		FATALERROR("Error creating child process!\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    84
		rc = -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    85
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    86
	return rc;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    87
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    88
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    89
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    90
#ifdef _CONSOLE
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    91
int main(int argc, char* argv[])
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    92
#else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    93
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    94
						LPSTR lpCmdLine, int nCmdShow )
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    95
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    96
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    97
	char here[_MAX_PATH + 1];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    98
	char thisfile[_MAX_PATH + 1];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    99
	int rc = 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   100
	char *workpath = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   101
	char *p;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   102
	int len;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   103
#ifndef _CONSOLE
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   104
	int argc = __argc;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   105
	char **argv = __argv;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   106
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   107
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   108
	// Initialize common controls (needed to link with commctrl32.dll and
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   109
	// obtain native XP look & feel).
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   110
	InitCommonControls();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   111
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   112
	// fill in thisfile
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   113
	if (!GetModuleFileNameA(NULL, thisfile, _MAX_PATH)) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   114
		FATALERROR("System error - unable to load!");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   115
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   116
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   117
	p = thisfile+strlen(thisfile) - 4;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   118
	if (strnicmp(p, ".exe", 4) != 0)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   119
		strcat(thisfile, ".exe");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   120
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   121
	// fill in here (directory of thisfile)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   122
	//GetModuleFileName returns an absolute path
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   123
	strcpy(here, thisfile);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   124
	for (p=here+strlen(here); *p != '\\' && p >= here+2; --p);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   125
	*++p = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   126
	len = p - here;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   127
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   128
	workpath = getenv( "_MEIPASS2" );
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   129
	rc = init(here, &thisfile[len], workpath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   130
	if (rc)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   131
		return rc;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   132
	if (workpath) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   133
		// we're the "child" process
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   134
		rc = doIt(argc, argv);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   135
		finalizePython();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   136
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   137
	else {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   138
		if (extractBinaries(&workpath)) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   139
			VS("Error extracting binaries");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   140
			return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   141
		}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   142
		// if workpath got set to non-NULL, we've extracted stuff
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   143
		if (workpath) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   144
			// run the "child" process, then clean up
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   145
			rc = relaunch(thisfile, workpath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   146
		}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   147
		else {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   148
			// no "child" process necessary
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   149
			rc = doIt(argc, argv);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   150
			finalizePython();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   151
		}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   152
		cleanUp();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   153
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   154
	return rc;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   155
}