1 const char win32_rcs[] = "$Id: win32.c,v 1.14 2008/03/02 18:15:41 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/win32.c,v $
6 * Purpose : Win32 User Interface initialization and message loop
8 * Copyright : Written by and Copyright (C) 2001-2002 members of
9 * the Privoxy team. http://www.privoxy.org/
11 * Written by and Copyright (C) 1999 Adam Lock
14 * This program is free software; you can redistribute it
15 * and/or modify it under the terms of the GNU General
16 * Public License as published by the Free Software
17 * Foundation; either version 2 of the License, or (at
18 * your option) any later version.
20 * This program is distributed in the hope that it will
21 * be useful, but WITHOUT ANY WARRANTY; without even the
22 * implied warranty of MERCHANTABILITY or FITNESS FOR A
23 * PARTICULAR PURPOSE. See the GNU General Public
24 * License for more details.
26 * The GNU General Public License should be included with
27 * this file. If not, you can view it at
28 * http://www.gnu.org/copyleft/gpl.html
29 * or write to the Free Software Foundation, Inc., 59
30 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 * Revision 1.14 2008/03/02 18:15:41 fabiankeil
35 * Update copyright year in win32_blurb[].
37 * Revision 1.13 2007/01/31 16:25:24 fabiankeil
38 * Update copyright range for the About message.
40 * Revision 1.12 2006/08/12 03:54:37 david__schmidt
41 * Windows service integration
43 * Revision 1.11 2006/07/18 14:48:48 david__schmidt
44 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
45 * with what was really the latest development (the v_3_0_branch branch)
47 * Revision 1.9.2.2 2002/08/27 18:03:40 oes
50 * Revision 1.9.2.1 2002/08/21 17:59:27 oes
51 * Sync win32_blurb[] with our standard blurb
53 * Revision 1.9 2002/03/31 17:19:00 jongfoster
54 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
56 * Revision 1.8 2002/03/26 22:57:10 jongfoster
57 * Web server name should begin www.
59 * Revision 1.7 2002/03/24 12:03:47 jongfoster
62 * Revision 1.6 2002/03/16 21:53:28 jongfoster
63 * VC++ Heap debug option
65 * Revision 1.5 2002/03/04 23:47:30 jongfoster
66 * - Rewritten, simpler command-line pre-parser
67 * - not using raise(SIGINT) any more
69 * Revision 1.4 2001/11/30 21:29:33 jongfoster
72 * Revision 1.3 2001/11/16 00:46:31 jongfoster
73 * Fixing compiler warnings
75 * Revision 1.2 2001/07/29 19:32:00 jongfoster
76 * Renaming _main() [mingw32 only] to real_main(), for ANSI compliance.
78 * Revision 1.1.1.1 2001/05/15 13:59:08 oes
79 * Initial import of version 2.9.3 source tree
82 *********************************************************************/
95 /* Uncomment this if you want to build Win32 as a console app */
96 /* #define _WIN_CONSOLE */
106 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
107 /* Visual C++ Heap debugging */
109 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
113 const char win32_h_rcs[] = WIN32_H_VERSION;
116 * A short introductory text about Privoxy. Used for the "About" box
117 * or the console startup message.
119 const char win32_blurb[] =
120 "Privoxy version " VERSION " for Windows\n"
121 "Copyright (C) 2000-2009 the Privoxy Team (" HOME_PAGE_URL ")\n"
122 "Based on the Internet Junkbuster by Junkbusters Corp.\n"
123 "This is free software; it may be used and copied under the\n"
124 "GNU General Public License: http://www.gnu.org/copyleft/gpl.html .\n"
125 "This program comes with ABSOLUTELY NO WARRANTY OF ANY KIND.\n";
130 * Hide the console. If set, the program will disconnect from the
131 * console and run in the background. This allows the command-prompt
137 #else /* ndef _WIN_CONSOLE */
141 * The application instance handle.
143 HINSTANCE g_hInstance;
147 * The command to show the window that was specified at startup.
151 static void __cdecl UserInterfaceThread(void *);
154 #endif /* ndef _WIN_CONSOLE */
156 /*********************************************************************
160 * Description : M$ Windows "main" routine:
161 * parse the `lpCmdLine' param into main's argc and argv variables,
162 * start the user interface thread (for the systray window), and
163 * call main (i.e. patch execution into normal startup).
166 * 1 : hInstance = instance handle of this execution
167 * 2 : hPrevInstance = instance handle of previous execution
168 * 3 : lpCmdLine = command line string which started us
169 * 4 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
171 * Returns : `main' never returns, so WinMain will also never return.
173 *********************************************************************/
174 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
176 #if 0 /* See comment about __argc & __argv below */
180 char szModule[MAX_PATH+1];
185 HANDLE hInitCompleteEvent = NULL;
189 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
191 /* Visual C++ Heap debugging */
193 /* Get current flag*/
194 int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
196 /* Turn on leak-checking bit */
197 tmpFlag |= _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF;
199 /* Turn off CRT block checking bit */
200 tmpFlag &= ~(_CRTDBG_CHECK_CRT_DF | _CRTDBG_DELAY_FREE_MEM_DF);
202 /* Set flag to the new value */
203 _CrtSetDbgFlag( tmpFlag );
205 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
209 * I couldn't figure out why the command line was being sorta parsed here
210 * instead of using the __argc & __argv globals usually defined in stdlib.h
212 * From what I can tell by looking at the MinWG source, it supports these
213 * globals, so i'd hope that the other compilers do so as well.
214 * Obviously, if i'm wrong i'll find out soon enough! :)
218 * Cheat in parsing the command line. We only ever have at most one
219 * paramater, which may optionally be specified inside double quotes.
222 if (lpCmdLine != NULL)
224 /* Make writable copy */
225 lpCmdLine = strdup(lpCmdLine);
227 if (lpCmdLine != NULL)
230 i = strlen(lpCmdLine);
231 if ((i >= 2) && (lpCmdLine[0] == '\"') && (lpCmdLine[i - 1] == '\"'))
233 lpCmdLine[i - 1] = '\0';
236 if (lpCmdLine[0] == '\0')
242 GetModuleFileName(hInstance, szModule, MAX_PATH);
246 argc = ((lpCmdLine != NULL) ? 2 : 1);
251 /* Create a user-interface thread and wait for it to initialise */
252 hInitCompleteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
253 g_hInstance = hInstance;
254 g_nCmdShow = nCmdShow;
255 _beginthread(UserInterfaceThread, 0, &hInitCompleteEvent);
256 WaitForSingleObject(hInitCompleteEvent, INFINITE);
257 DeleteObject(hInitCompleteEvent);
261 res = real_main( __argc, __argv );
263 res = main( __argc, __argv );
272 /*********************************************************************
274 * Function : InitWin32
276 * Description : Initialise windows, setting up the console or windows as appropriate.
282 *********************************************************************/
285 WORD wVersionRequested;
289 SetProcessShutdownParameters(0x100, SHUTDOWN_NORETRY);
295 wVersionRequested = MAKEWORD(2, 0);
296 if (WSAStartup(wVersionRequested, &wsaData) != 0)
299 MessageBox(NULL, "Cannot initialize WinSock library", "Privoxy Error",
300 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
316 /*********************************************************************
318 * Function : UserInterfaceThread
320 * Description : User interface thread. WinMain will wait for us to set
321 * the hInitCompleteEvent before patching over to `main'.
322 * This ensures the systray window is active before beginning
326 * 1 : pData = pointer to `hInitCompleteEvent'.
330 *********************************************************************/
331 static void __cdecl UserInterfaceThread(void *pData)
334 HANDLE hInitCompleteEvent = *((HANDLE *) pData);
338 SetEvent(hInitCompleteEvent);
340 /* Enter a message processing loop */
341 while (GetMessage(&msg, (HWND) NULL, 0, 0))
343 TranslateMessage(&msg);
344 DispatchMessage(&msg);
356 #endif /* ndef _WIN_CONSOLE */