win32.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*********************************************************************
  2. *
  3. * File : $Source: /cvsroot/ijbswa/current/win32.c,v $
  4. *
  5. * Purpose : Win32 User Interface initialization and message loop
  6. *
  7. * Copyright : Written by and Copyright (C) 2001-2002 members of
  8. * the Privoxy team. https://www.privoxy.org/
  9. *
  10. * Written by and Copyright (C) 1999 Adam Lock
  11. * <locka@iol.ie>
  12. *
  13. * This program is free software; you can redistribute it
  14. * and/or modify it under the terms of the GNU General
  15. * Public License as published by the Free Software
  16. * Foundation; either version 2 of the License, or (at
  17. * your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will
  20. * be useful, but WITHOUT ANY WARRANTY; without even the
  21. * implied warranty of MERCHANTABILITY or FITNESS FOR A
  22. * PARTICULAR PURPOSE. See the GNU General Public
  23. * License for more details.
  24. *
  25. * The GNU General Public License should be included with
  26. * this file. If not, you can view it at
  27. * http://www.gnu.org/copyleft/gpl.html
  28. * or write to the Free Software Foundation, Inc., 59
  29. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  30. *
  31. *********************************************************************/
  32. #include "config.h"
  33. #ifdef _WIN32
  34. #include <stdio.h>
  35. #include "project.h"
  36. #include "jcc.h"
  37. #include "miscutil.h"
  38. /* Uncomment this if you want to build Win32 as a console app */
  39. /* #define _WIN_CONSOLE */
  40. #ifndef STRICT
  41. #define STRICT
  42. #endif
  43. #include <windows.h>
  44. #include <stdarg.h>
  45. #include <process.h>
  46. #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
  47. /* Visual C++ Heap debugging */
  48. #include <crtdbg.h>
  49. #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
  50. #include "win32.h"
  51. /**
  52. * A short introductory text about Privoxy. Used for the "About" box
  53. * or the console startup message.
  54. */
  55. const char win32_blurb[] =
  56. "Privoxy version " VERSION " for Windows\n"
  57. "Copyright (C) 2000-2019 the Privoxy Team (" HOME_PAGE_URL ")\n"
  58. "Based on the Internet Junkbuster by Junkbusters Corp.\n"
  59. "This is free software; it may be used and copied under the\n"
  60. "GNU General Public License, version 2: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html\n"
  61. "This program comes with ABSOLUTELY NO WARRANTY OF ANY KIND.\n";
  62. #ifdef _WIN_CONSOLE
  63. /**
  64. * Hide the console. If set, the program will disconnect from the
  65. * console and run in the background. This allows the command-prompt
  66. * window to close.
  67. */
  68. int hideConsole = 0;
  69. #else /* ndef _WIN_CONSOLE */
  70. /**
  71. * The application instance handle.
  72. */
  73. HINSTANCE g_hInstance;
  74. /**
  75. * The command to show the window that was specified at startup.
  76. */
  77. int g_nCmdShow;
  78. static void __cdecl UserInterfaceThread(void *);
  79. #endif /* ndef _WIN_CONSOLE */
  80. /*********************************************************************
  81. *
  82. * Function : WinMain
  83. *
  84. * Description : M$ Windows "main" routine:
  85. * parse the `lpCmdLine' param into main's argc and argv variables,
  86. * start the user interface thread (for the systray window), and
  87. * call main (i.e. patch execution into normal startup).
  88. *
  89. * Parameters :
  90. * 1 : hInstance = instance handle of this execution
  91. * 2 : hPrevInstance = instance handle of previous execution
  92. * 3 : lpCmdLine = command line string which started us
  93. * 4 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
  94. *
  95. * Returns : `main' never returns, so WinMain will also never return.
  96. *
  97. *********************************************************************/
  98. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  99. {
  100. #if 0 /* See comment about __argc & __argv below */
  101. int i;
  102. int argc = 1;
  103. const char *argv[3];
  104. char szModule[MAX_PATH+1];
  105. #endif
  106. int res;
  107. #ifndef _WIN_CONSOLE
  108. HANDLE hInitCompleteEvent = NULL;
  109. #endif
  110. #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
  111. #if 0
  112. /* Visual C++ Heap debugging */
  113. /* Get current flag*/
  114. int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
  115. /* Turn on leak-checking bit */
  116. tmpFlag |= _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF;
  117. /* Turn off CRT block checking bit */
  118. tmpFlag &= ~(_CRTDBG_CHECK_CRT_DF | _CRTDBG_DELAY_FREE_MEM_DF);
  119. /* Set flag to the new value */
  120. _CrtSetDbgFlag(tmpFlag);
  121. #endif
  122. #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
  123. /************
  124. * I couldn't figure out why the command line was being sorta parsed here
  125. * instead of using the __argc & __argv globals usually defined in stdlib.h
  126. *
  127. * From what I can tell by looking at the MinWG source, it supports these
  128. * globals, so i'd hope that the other compilers do so as well.
  129. * Obviously, if i'm wrong i'll find out soon enough! :)
  130. ************/
  131. #if 0
  132. /*
  133. * Cheat in parsing the command line. We only ever have at most one
  134. * parameter, which may optionally be specified inside double quotes.
  135. */
  136. if (lpCmdLine != NULL)
  137. {
  138. /* Make writable copy */
  139. lpCmdLine = strdup(lpCmdLine);
  140. }
  141. if (lpCmdLine != NULL)
  142. {
  143. chomp(lpCmdLine);
  144. i = strlen(lpCmdLine);
  145. if ((i >= 2) && (lpCmdLine[0] == '\"') && (lpCmdLine[i - 1] == '\"'))
  146. {
  147. lpCmdLine[i - 1] = '\0';
  148. lpCmdLine++;
  149. }
  150. if (lpCmdLine[0] == '\0')
  151. {
  152. lpCmdLine = NULL;
  153. }
  154. }
  155. GetModuleFileName(hInstance, szModule, MAX_PATH);
  156. argv[0] = szModule;
  157. argv[1] = lpCmdLine;
  158. argv[2] = NULL;
  159. argc = ((lpCmdLine != NULL) ? 2 : 1);
  160. #endif /* -END- 0 */
  161. #ifndef _WIN_CONSOLE
  162. /* Create a user-interface thread and wait for it to initialise */
  163. hInitCompleteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  164. g_hInstance = hInstance;
  165. g_nCmdShow = nCmdShow;
  166. _beginthread(UserInterfaceThread, 0, &hInitCompleteEvent);
  167. WaitForSingleObject(hInitCompleteEvent, INFINITE);
  168. CloseHandle(hInitCompleteEvent);
  169. #endif
  170. #ifdef __MINGW32__
  171. res = real_main(__argc, __argv);
  172. #else
  173. res = main(__argc, __argv);
  174. #endif
  175. return res;
  176. }
  177. #endif
  178. /*********************************************************************
  179. *
  180. * Function : InitWin32
  181. *
  182. * Description : Initialise windows, setting up the console or windows as appropriate.
  183. *
  184. * Parameters : None
  185. *
  186. * Returns : N/A
  187. *
  188. *********************************************************************/
  189. void InitWin32(void)
  190. {
  191. WORD wVersionRequested;
  192. WSADATA wsaData;
  193. #ifdef _WIN_CONSOLE
  194. SetProcessShutdownParameters(0x100, SHUTDOWN_NORETRY);
  195. if (hideConsole)
  196. {
  197. FreeConsole();
  198. }
  199. #endif
  200. wVersionRequested = MAKEWORD(2, 0);
  201. if (WSAStartup(wVersionRequested, &wsaData) != 0)
  202. {
  203. #ifndef _WIN_CONSOLE
  204. MessageBox(NULL, "Cannot initialize WinSock library", "Privoxy Error",
  205. MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
  206. #endif
  207. exit(1);
  208. }
  209. }
  210. #ifndef _WIN_CONSOLE
  211. #include <signal.h>
  212. #include <assert.h>
  213. #include "win32.h"
  214. #include "w32log.h"
  215. /*********************************************************************
  216. *
  217. * Function : UserInterfaceThread
  218. *
  219. * Description : User interface thread. WinMain will wait for us to set
  220. * the hInitCompleteEvent before patching over to `main'.
  221. * This ensures the systray window is active before beginning
  222. * operations.
  223. *
  224. * Parameters :
  225. * 1 : pData = pointer to `hInitCompleteEvent'.
  226. *
  227. * Returns : N/A
  228. *
  229. *********************************************************************/
  230. static void __cdecl UserInterfaceThread(void *pData)
  231. {
  232. MSG msg;
  233. HANDLE hInitCompleteEvent = *((HANDLE *) pData);
  234. /* Initialise */
  235. InitLogWindow();
  236. SetEvent(hInitCompleteEvent);
  237. /* Enter a message processing loop */
  238. while (GetMessage(&msg, (HWND) NULL, 0, 0))
  239. {
  240. TranslateMessage(&msg);
  241. DispatchMessage(&msg);
  242. }
  243. /* Cleanup */
  244. TermLogWindow();
  245. /* Time to die... */
  246. exit(0);
  247. }
  248. #endif /* ndef _WIN_CONSOLE */
  249. /*
  250. Local Variables:
  251. tab-width: 3
  252. end:
  253. */