1 | /*
|
---|
2 | Launch4j (http://launch4j.sourceforge.net/)
|
---|
3 | Cross-platform Java application wrapper for creating Windows native executables.
|
---|
4 |
|
---|
5 | Copyright (C) 2004, 2006 Grzegorz Kowal
|
---|
6 |
|
---|
7 | This library is free software; you can redistribute it and/or
|
---|
8 | modify it under the terms of the GNU Lesser General Public
|
---|
9 | License as published by the Free Software Foundation; either
|
---|
10 | version 2.1 of the License, or (at your option) any later version.
|
---|
11 |
|
---|
12 | This library 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 GNU
|
---|
15 | Lesser General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU Lesser General Public
|
---|
18 | License along with this library; if not, write to the Free Software
|
---|
19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
20 |
|
---|
21 |
|
---|
22 | Compiled with Mingw port of GCC,
|
---|
23 | Bloodshed Dev-C++ IDE (http://www.bloodshed.net/devcpp.html)
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "../resource.h"
|
---|
27 | #include "../head.h"
|
---|
28 | #include "guihead.h"
|
---|
29 |
|
---|
30 | extern PROCESS_INFORMATION pi;
|
---|
31 |
|
---|
32 | HWND hWnd;
|
---|
33 | DWORD dwExitCode = 0;
|
---|
34 | BOOL stayAlive = FALSE;
|
---|
35 | BOOL splash = FALSE;
|
---|
36 | BOOL splashTimeoutErr;
|
---|
37 | BOOL waitForWindow;
|
---|
38 | int splashTimeout; // tick count (s)
|
---|
39 |
|
---|
40 | int APIENTRY WinMain(HINSTANCE hInstance,
|
---|
41 | HINSTANCE hPrevInstance,
|
---|
42 | LPSTR lpCmdLine,
|
---|
43 | int nCmdShow) {
|
---|
44 | HMODULE hLibrary = NULL;
|
---|
45 | if (!prepare(hLibrary, lpCmdLine)) {
|
---|
46 | if (hLibrary != NULL) {
|
---|
47 | FreeLibrary(hLibrary);
|
---|
48 | }
|
---|
49 | return 1;
|
---|
50 | }
|
---|
51 |
|
---|
52 | splash = loadBoolString(hLibrary, SHOW_SPLASH);
|
---|
53 | stayAlive = loadBoolString(hLibrary, GUI_HEADER_STAYS_ALIVE);
|
---|
54 | if (splash || stayAlive) {
|
---|
55 | hWnd = CreateWindowEx(WS_EX_TOOLWINDOW, "STATIC", "",
|
---|
56 | WS_POPUP | SS_BITMAP,
|
---|
57 | 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
|
---|
58 | if (splash) {
|
---|
59 | char timeout[10] = {0};
|
---|
60 | if (!loadString(hLibrary, SPLASH_TIMEOUT, timeout)) {
|
---|
61 | msgBox("Cannot load splash timeout property.");
|
---|
62 | return 1;
|
---|
63 | }
|
---|
64 | splashTimeout = atoi(timeout);
|
---|
65 | if (splashTimeout <= 0 || splashTimeout > 60 * 15 /* 15 minutes */) {
|
---|
66 | msgBox("Invalid splash timeout property.");
|
---|
67 | return 1;
|
---|
68 | }
|
---|
69 | splashTimeoutErr = loadBoolString(hLibrary, SPLASH_TIMEOUT_ERR);
|
---|
70 | waitForWindow = loadBoolString(hLibrary, SPLASH_WAITS_FOR_WINDOW);
|
---|
71 | HANDLE hImage = LoadImage(hInstance, // handle of the instance containing the image
|
---|
72 | MAKEINTRESOURCE(SPLASH_BITMAP), // name or identifier of image
|
---|
73 | IMAGE_BITMAP, // type of image
|
---|
74 | 0, // desired width
|
---|
75 | 0, // desired height
|
---|
76 | LR_DEFAULTSIZE);
|
---|
77 | if (hImage == NULL) {
|
---|
78 | msgBox("Cannot load splash screen.");
|
---|
79 | return 1;
|
---|
80 | }
|
---|
81 | SendMessage(hWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM) hImage);
|
---|
82 | RECT rect;
|
---|
83 | GetWindowRect(hWnd, &rect);
|
---|
84 | int x = (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) / 2;
|
---|
85 | int y = (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) / 2;
|
---|
86 | SetWindowPos(hWnd, HWND_TOP, x, y, 0, 0, SWP_NOSIZE);
|
---|
87 | ShowWindow(hWnd, nCmdShow);
|
---|
88 | UpdateWindow (hWnd);
|
---|
89 | }
|
---|
90 | if (!SetTimer (hWnd, ID_TIMER, 1000 /* 1s */, TimerProc)) {
|
---|
91 | return 1;
|
---|
92 | }
|
---|
93 | }
|
---|
94 | FreeLibrary(hLibrary);
|
---|
95 | if (execute(FALSE) == -1) {
|
---|
96 | char errMsg[BIG_STR];
|
---|
97 | strcpy(errMsg, "Error occured while starting the application...\n");
|
---|
98 | strcat(errMsg, strerror(errno));
|
---|
99 | msgBox(errMsg);
|
---|
100 | return 1;
|
---|
101 | }
|
---|
102 | if (!(splash || stayAlive)) {
|
---|
103 | closeHandles();
|
---|
104 | return 0;
|
---|
105 | }
|
---|
106 | MSG msg;
|
---|
107 | while (GetMessage(&msg, NULL, 0, 0)) {
|
---|
108 | TranslateMessage(&msg);
|
---|
109 | DispatchMessage(&msg);
|
---|
110 | }
|
---|
111 | closeHandles();
|
---|
112 | return dwExitCode;
|
---|
113 | }
|
---|
114 |
|
---|
115 | BOOL CALLBACK enumwndfn(HWND hwnd, LPARAM lParam) {
|
---|
116 | DWORD processId;
|
---|
117 | GetWindowThreadProcessId(hwnd, &processId);
|
---|
118 | if (pi.dwProcessId == processId) {
|
---|
119 | LONG styles = GetWindowLong(hwnd, GWL_STYLE);
|
---|
120 | if ((styles & WS_VISIBLE) != 0) {
|
---|
121 | splash = FALSE;
|
---|
122 | ShowWindow(hWnd, SW_HIDE);
|
---|
123 | return FALSE;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | return TRUE;
|
---|
127 | }
|
---|
128 |
|
---|
129 | VOID CALLBACK TimerProc(
|
---|
130 | HWND hwnd, // handle of window for timer messages
|
---|
131 | UINT uMsg, // WM_TIMER message
|
---|
132 | UINT idEvent, // timer identifier
|
---|
133 | DWORD dwTime) { // current system time
|
---|
134 |
|
---|
135 | if (splash) {
|
---|
136 | if (splashTimeout == 0) {
|
---|
137 | splash = FALSE;
|
---|
138 | ShowWindow(hWnd, SW_HIDE);
|
---|
139 | if (waitForWindow && splashTimeoutErr) {
|
---|
140 | KillTimer(hwnd, ID_TIMER);
|
---|
141 | msgBox("Could not start the application, operation timed out.");
|
---|
142 | PostQuitMessage(0);
|
---|
143 | }
|
---|
144 | } else {
|
---|
145 | splashTimeout--;
|
---|
146 | if (waitForWindow) {
|
---|
147 | EnumWindows(enumwndfn, 0);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 | GetExitCodeProcess(pi.hProcess, &dwExitCode);
|
---|
152 | if (dwExitCode != STILL_ACTIVE
|
---|
153 | || !(splash || stayAlive)) {
|
---|
154 | KillTimer(hWnd, ID_TIMER);
|
---|
155 | PostQuitMessage(0);
|
---|
156 | return;
|
---|
157 | }
|
---|
158 | }
|
---|