1 const char win32_rcs[] = "$Id: win32.c,v 1.6 2002/03/16 21:53:28 jongfoster 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://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.6 2002/03/16 21:53:28 jongfoster
35 * VC++ Heap debug option
37 * Revision 1.5 2002/03/04 23:47:30 jongfoster
38 * - Rewritten, simpler command-line pre-parser
39 * - not using raise(SIGINT) any more
41 * Revision 1.4 2001/11/30 21:29:33 jongfoster
44 * Revision 1.3 2001/11/16 00:46:31 jongfoster
45 * Fixing compiler warnings
47 * Revision 1.2 2001/07/29 19:32:00 jongfoster
48 * Renaming _main() [mingw32 only] to real_main(), for ANSI compliance.
50 * Revision 1.1.1.1 2001/05/15 13:59:08 oes
51 * Initial import of version 2.9.3 source tree
54 *********************************************************************/
67 /* Uncomment this if you want to build Win32 as a console app */
68 /* #define _WIN_CONSOLE */
75 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
76 /* Visual C++ Heap debugging */
78 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
82 const char win32_h_rcs[] = WIN32_H_VERSION;
84 const char win32_blurb[] =
85 "Privoxy version " VERSION " for Windows\n"
86 "Copyright (C) 2000-2002 by members of the Privoxy Team\n"
87 "Copyright (C) 1997-8 by Junkbusters Corp.\n"
88 "This is free software; it may be used and copied under the\n"
89 "GNU General Public License: http://www.gnu.org/copyleft/gpl.html .\n"
90 "This program comes with ABSOLUTELY NO WARRANTY OF ANY KIND.\n"
92 "For information about how to to configure the proxy and your browser, see\n"
93 " " HOME_PAGE_URL "\n"
102 HINSTANCE g_hInstance;
105 static void __cdecl UserInterfaceThread(void *);
110 /*********************************************************************
114 * Description : M$ Windows "main" routine:
115 * parse the `lpCmdLine' param into main's argc and argv variables,
116 * start the user interface thread (for the systray window), and
117 * call main (i.e. patch execution into normal startup).
120 * 1 : hInstance = instance handle of this execution
121 * 2 : hPrevInstance = instance handle of previous execution
122 * 3 : lpCmdLine = command line string which started us
123 * 4 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
125 * Returns : `main' never returns, so WinMain will also never return.
127 *********************************************************************/
128 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
134 char szModule[MAX_PATH+1];
136 HANDLE hInitCompleteEvent = NULL;
140 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
142 /* Visual C++ Heap debugging */
144 /* Get current flag*/
145 int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
147 /* Turn on leak-checking bit */
148 tmpFlag |= _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF;
150 /* Turn off CRT block checking bit */
151 tmpFlag &= ~(_CRTDBG_CHECK_CRT_DF | _CRTDBG_DELAY_FREE_MEM_DF);
153 /* Set flag to the new value */
154 _CrtSetDbgFlag( tmpFlag );
156 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
159 * Cheat in parsing the command line. We only ever have at most one
160 * paramater, which may optionally be specified inside double quotes.
163 if (lpCmdLine != NULL)
165 /* Make writable copy */
166 lpCmdLine = strdup(lpCmdLine);
168 if (lpCmdLine != NULL)
171 i = strlen(lpCmdLine);
172 if ((i >= 2) && (lpCmdLine[0] == '\"') && (lpCmdLine[i - 1] == '\"'))
174 lpCmdLine[i - 1] = '\0';
177 if (lpCmdLine[0] == '\0')
183 GetModuleFileName(hInstance, szModule, MAX_PATH);
187 argc = ((lpCmdLine != NULL) ? 2 : 1);
190 /* Create a user-interface thread and wait for it to initialise */
191 hInitCompleteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
192 g_hInstance = hInstance;
193 g_nCmdShow = nCmdShow;
194 _beginthread(UserInterfaceThread, 0, &hInitCompleteEvent);
195 WaitForSingleObject(hInitCompleteEvent, INFINITE);
196 DeleteObject(hInitCompleteEvent);
200 res = real_main( argc, argv );
202 res = main( argc, argv );
211 /*********************************************************************
213 * Function : InitWin32
215 * Description : Initialise windows, setting up the console or windows as appropriate.
221 *********************************************************************/
224 WORD wVersionRequested;
228 SetProcessShutdownParameters(0x100, SHUTDOWN_NORETRY);
234 wVersionRequested = MAKEWORD(2, 0);
235 if (WSAStartup(wVersionRequested, &wsaData) != 0)
238 MessageBox(NULL, "Cannot initialize WinSock library", "Privoxy Error",
239 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
255 /*********************************************************************
257 * Function : UserInterfaceThread
259 * Description : User interface thread. WinMain will wait for us to set
260 * the hInitCompleteEvent before patching over to `main'.
261 * This ensures the systray window is active before beginning
265 * 1 : pData = pointer to `hInitCompleteEvent'.
269 *********************************************************************/
270 static void __cdecl UserInterfaceThread(void *pData)
273 HANDLE hInitCompleteEvent = *((HANDLE *) pData);
277 SetEvent(hInitCompleteEvent);
279 /* Enter a message processing loop */
280 while (GetMessage(&msg, (HWND) NULL, 0, 0))
282 TranslateMessage(&msg);
283 DispatchMessage(&msg);