1 const char win32_rcs[] = "$Id: win32.c,v 1.18 2011/09/04 11:10:56 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.
32 *********************************************************************/
45 /* Uncomment this if you want to build Win32 as a console app */
46 /* #define _WIN_CONSOLE */
56 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
57 /* Visual C++ Heap debugging */
59 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
63 const char win32_h_rcs[] = WIN32_H_VERSION;
66 * A short introductory text about Privoxy. Used for the "About" box
67 * or the console startup message.
69 const char win32_blurb[] =
70 "Privoxy version " VERSION " for Windows\n"
71 "Copyright (C) 2000-2010 the Privoxy Team (" HOME_PAGE_URL ")\n"
72 "Based on the Internet Junkbuster by Junkbusters Corp.\n"
73 "This is free software; it may be used and copied under the\n"
74 "GNU General Public License, version 2: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html\n"
75 "This program comes with ABSOLUTELY NO WARRANTY OF ANY KIND.\n";
80 * Hide the console. If set, the program will disconnect from the
81 * console and run in the background. This allows the command-prompt
87 #else /* ndef _WIN_CONSOLE */
91 * The application instance handle.
93 HINSTANCE g_hInstance;
97 * The command to show the window that was specified at startup.
101 static void __cdecl UserInterfaceThread(void *);
104 #endif /* ndef _WIN_CONSOLE */
106 /*********************************************************************
110 * Description : M$ Windows "main" routine:
111 * parse the `lpCmdLine' param into main's argc and argv variables,
112 * start the user interface thread (for the systray window), and
113 * call main (i.e. patch execution into normal startup).
116 * 1 : hInstance = instance handle of this execution
117 * 2 : hPrevInstance = instance handle of previous execution
118 * 3 : lpCmdLine = command line string which started us
119 * 4 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
121 * Returns : `main' never returns, so WinMain will also never return.
123 *********************************************************************/
124 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
126 #if 0 /* See comment about __argc & __argv below */
130 char szModule[MAX_PATH+1];
135 HANDLE hInitCompleteEvent = NULL;
139 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
141 /* Visual C++ Heap debugging */
143 /* Get current flag*/
144 int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
146 /* Turn on leak-checking bit */
147 tmpFlag |= _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF;
149 /* Turn off CRT block checking bit */
150 tmpFlag &= ~(_CRTDBG_CHECK_CRT_DF | _CRTDBG_DELAY_FREE_MEM_DF);
152 /* Set flag to the new value */
153 _CrtSetDbgFlag(tmpFlag);
155 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
159 * I couldn't figure out why the command line was being sorta parsed here
160 * instead of using the __argc & __argv globals usually defined in stdlib.h
162 * From what I can tell by looking at the MinWG source, it supports these
163 * globals, so i'd hope that the other compilers do so as well.
164 * Obviously, if i'm wrong i'll find out soon enough! :)
168 * Cheat in parsing the command line. We only ever have at most one
169 * paramater, which may optionally be specified inside double quotes.
172 if (lpCmdLine != NULL)
174 /* Make writable copy */
175 lpCmdLine = strdup(lpCmdLine);
177 if (lpCmdLine != NULL)
180 i = strlen(lpCmdLine);
181 if ((i >= 2) && (lpCmdLine[0] == '\"') && (lpCmdLine[i - 1] == '\"'))
183 lpCmdLine[i - 1] = '\0';
186 if (lpCmdLine[0] == '\0')
192 GetModuleFileName(hInstance, szModule, MAX_PATH);
196 argc = ((lpCmdLine != NULL) ? 2 : 1);
201 /* Create a user-interface thread and wait for it to initialise */
202 hInitCompleteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
203 g_hInstance = hInstance;
204 g_nCmdShow = nCmdShow;
205 _beginthread(UserInterfaceThread, 0, &hInitCompleteEvent);
206 WaitForSingleObject(hInitCompleteEvent, INFINITE);
207 DeleteObject(hInitCompleteEvent);
211 res = real_main(__argc, __argv);
213 res = main(__argc, __argv);
222 /*********************************************************************
224 * Function : InitWin32
226 * Description : Initialise windows, setting up the console or windows as appropriate.
232 *********************************************************************/
235 WORD wVersionRequested;
239 SetProcessShutdownParameters(0x100, SHUTDOWN_NORETRY);
245 wVersionRequested = MAKEWORD(2, 0);
246 if (WSAStartup(wVersionRequested, &wsaData) != 0)
249 MessageBox(NULL, "Cannot initialize WinSock library", "Privoxy Error",
250 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
266 /*********************************************************************
268 * Function : UserInterfaceThread
270 * Description : User interface thread. WinMain will wait for us to set
271 * the hInitCompleteEvent before patching over to `main'.
272 * This ensures the systray window is active before beginning
276 * 1 : pData = pointer to `hInitCompleteEvent'.
280 *********************************************************************/
281 static void __cdecl UserInterfaceThread(void *pData)
284 HANDLE hInitCompleteEvent = *((HANDLE *) pData);
288 SetEvent(hInitCompleteEvent);
290 /* Enter a message processing loop */
291 while (GetMessage(&msg, (HWND) NULL, 0, 0))
293 TranslateMessage(&msg);
294 DispatchMessage(&msg);
306 #endif /* ndef _WIN_CONSOLE */