Windows service integration
authorDavid Schmidt <david__schmidt@users.sourceforge.net>
Sat, 12 Aug 2006 03:54:37 +0000 (03:54 +0000)
committerDavid Schmidt <david__schmidt@users.sourceforge.net>
Sat, 12 Aug 2006 03:54:37 +0000 (03:54 +0000)
GNUmakefile.in
parsers.c
w32svrapi.c [new file with mode: 0644]
w32svrapi.h [new file with mode: 0644]
win32.c

index 65c2b4b..7599c43 100644 (file)
@@ -1,6 +1,6 @@
 # Note:  Makefile is built automatically from Makefile.in
 #
 # Note:  Makefile is built automatically from Makefile.in
 #
-# $Id: GNUmakefile.in,v 1.104.2.28 2004/06/10 17:06:05 hal9 Exp $
+# $Id: GNUmakefile.in,v 1.140 2006/07/18 14:48:45 david__schmidt Exp $
 #
 # Written by and Copyright (C) 2001 - 2004 the SourceForge
 # Privoxy team. http://www.privoxy.org/
 #
 # Written by and Copyright (C) 2001 - 2004 the SourceForge
 # Privoxy team. http://www.privoxy.org/
@@ -192,10 +192,10 @@ C_SRC  = actions.c cgi.c cgiedit.c cgisimple.c deanimate.c encode.c \
 C_OBJS = $(C_SRC:.c=.@OBJEXT@)
 C_HDRS = $(C_SRC:.c=.h) project.h actionlist.h
 
 C_OBJS = $(C_SRC:.c=.@OBJEXT@)
 C_HDRS = $(C_SRC:.c=.h) project.h actionlist.h
 
-W32_SRC   = @WIN_ONLY@w32log.c w32taskbar.c win32.c
+W32_SRC   = @WIN_ONLY@w32log.c w32taskbar.c win32.c w32svrapi.c
 W32_FILES = @WIN_ONLY@w32.res
 W32_OBJS  = @WIN_ONLY@$(W32_SRC:.c=.@OBJEXT@) $(W32_FILES)
 W32_FILES = @WIN_ONLY@w32.res
 W32_OBJS  = @WIN_ONLY@$(W32_SRC:.c=.@OBJEXT@) $(W32_FILES)
-W32_HDRS  = @WIN_ONLY@w32log.h w32taskbar.h win32.h w32res.h
+W32_HDRS  = @WIN_ONLY@w32log.h w32taskbar.h win32.h w32res.h w32svrapi.h
 W32_LIB   = @WIN_ONLY@-lwsock32 -lcomctl32
 W32_INIS  = @WIN_ONLY@config.txt trust.txt
 
 W32_LIB   = @WIN_ONLY@-lwsock32 -lcomctl32
 W32_INIS  = @WIN_ONLY@config.txt trust.txt
 
@@ -961,7 +961,7 @@ errlog.@OBJEXT@:    errlog.c    errlog.h    config.h $(PROJECT_H_DEPS) @WIN_ONLY
 filters.@OBJEXT@:   filters.c   filters.h   config.h $(PROJECT_H_DEPS) errlog.h encode.h gateway.h jbsockets.h jcc.h loadcfg.h parsers.h ssplit.h cgi.h deanimate.h @WIN_ONLY@win32.h 
 gateway.@OBJEXT@:   gateway.c   gateway.h   config.h $(PROJECT_H_DEPS) errlog.h jbsockets.h jcc.h loadcfg.h
 jbsockets.@OBJEXT@: jbsockets.c jbsockets.h config.h $(PROJECT_H_DEPS) filters.h
 filters.@OBJEXT@:   filters.c   filters.h   config.h $(PROJECT_H_DEPS) errlog.h encode.h gateway.h jbsockets.h jcc.h loadcfg.h parsers.h ssplit.h cgi.h deanimate.h @WIN_ONLY@win32.h 
 gateway.@OBJEXT@:   gateway.c   gateway.h   config.h $(PROJECT_H_DEPS) errlog.h jbsockets.h jcc.h loadcfg.h
 jbsockets.@OBJEXT@: jbsockets.c jbsockets.h config.h $(PROJECT_H_DEPS) filters.h
-jcc.@OBJEXT@:       jcc.c       jcc.h       config.h $(PROJECT_H_DEPS) errlog.h filters.h gateway.h jbsockets.h killpopup.h loadcfg.h loaders.h miscutil.h parsers.h @WIN_ONLY@w32log.h win32.h cgi.h
+jcc.@OBJEXT@:       jcc.c       jcc.h       config.h $(PROJECT_H_DEPS) errlog.h filters.h gateway.h jbsockets.h killpopup.h loadcfg.h loaders.h miscutil.h parsers.h @WIN_ONLY@w32log.h win32.h w32svrapi.h cgi.h
 killpopup.@OBJEXT@: killpopup.c killpopup.h config.h $(PROJECT_H_DEPS) jcc.h loadcfg.h
 list.@OBJEXT@:      list.c      list.h      config.h $(PROJECT_H_DEPS) list.h miscutil.h
 loadcfg.@OBJEXT@:   loadcfg.c   loadcfg.h   config.h $(PROJECT_H_DEPS) errlog.h filters.h gateway.h jbsockets.h jcc.h killpopup.h loaders.h miscutil.h parsers.h @WIN_ONLY@w32log.h win32.h
 killpopup.@OBJEXT@: killpopup.c killpopup.h config.h $(PROJECT_H_DEPS) jcc.h loadcfg.h
 list.@OBJEXT@:      list.c      list.h      config.h $(PROJECT_H_DEPS) list.h miscutil.h
 loadcfg.@OBJEXT@:   loadcfg.c   loadcfg.h   config.h $(PROJECT_H_DEPS) errlog.h filters.h gateway.h jbsockets.h jcc.h killpopup.h loaders.h miscutil.h parsers.h @WIN_ONLY@w32log.h win32.h
@@ -995,7 +995,7 @@ pcre/dftables@EXEEXT@:       pcre/dftables.c pcre/maketables.c pcre/pcre.h pcre/
 # Win32
 w32log.@OBJEXT@: w32log.c errlog.h config.h jcc.h loadcfg.h miscutil.h pcre/pcre.h pcre/pcreposix.h pcrs.h project.h w32log.h w32taskbar.h win32.h
 w32taskbar.@OBJEXT@: w32taskbar.c config.h w32log.h w32taskbar.h
 # Win32
 w32log.@OBJEXT@: w32log.c errlog.h config.h jcc.h loadcfg.h miscutil.h pcre/pcre.h pcre/pcreposix.h pcrs.h project.h w32log.h w32taskbar.h win32.h
 w32taskbar.@OBJEXT@: w32taskbar.c config.h w32log.h w32taskbar.h
-win32.@OBJEXT@: win32.c config.h jcc.h loadcfg.h pcre/pcre.h pcre/pcreposix.h pcrs.h project.h w32log.h win32.h
+win32.@OBJEXT@: win32.c config.h jcc.h loadcfg.h pcre/pcre.h pcre/pcreposix.h pcrs.h project.h w32log.h win32.h w32svrapi.h
 
 w32.res: w32.rc w32res.h icons/ico00001.ico icons/ico00002.ico icons/ico00003.ico icons/ico00004.ico icons/ico00005.ico icons/ico00006.ico icons/ico00007.ico icons/ico00008.ico icons/idle.ico icons/privoxy.ico config.h
        windres -D__MINGW32__=0.2 -O coff -i $< -o $@
 
 w32.res: w32.rc w32res.h icons/ico00001.ico icons/ico00002.ico icons/ico00003.ico icons/ico00004.ico icons/ico00005.ico icons/ico00006.ico icons/ico00007.ico icons/ico00008.ico icons/idle.ico icons/privoxy.ico config.h
        windres -D__MINGW32__=0.2 -O coff -i $< -o $@
@@ -1345,6 +1345,10 @@ coffee:
 ## end:
 
 # $Log: GNUmakefile.in,v $
 ## end:
 
 # $Log: GNUmakefile.in,v $
+# Revision 1.140  2006/07/18 14:48:45  david__schmidt
+# Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
+# with what was really the latest development (the v_3_0_branch branch)
+#
 # Revision 1.104.2.28  2004/06/10 17:06:05  hal9
 # Fix bug #959617, by moving TMPDIR var to create-snapshot target, which is the
 # only place it is used.
 # Revision 1.104.2.28  2004/06/10 17:06:05  hal9
 # Fix bug #959617, by moving TMPDIR var to create-snapshot target, which is the
 # only place it is used.
index ac5bfef..da4f317 100644 (file)
--- a/parsers.c
+++ b/parsers.c
@@ -1,4 +1,4 @@
-const char parsers_rcs[] = "$Id: parsers.c,v 1.58 2006/07/18 14:48:47 david__schmidt Exp $";
+const char parsers_rcs[] = "$Id: parsers.c,v 1.59 2006/08/03 02:46:41 david__schmidt Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/parsers.c,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/parsers.c,v $
@@ -40,6 +40,9 @@ const char parsers_rcs[] = "$Id: parsers.c,v 1.58 2006/07/18 14:48:47 david__sch
  *
  * Revisions   :
  *    $Log: parsers.c,v $
  *
  * Revisions   :
  *    $Log: parsers.c,v $
+ *    Revision 1.59  2006/08/03 02:46:41  david__schmidt
+ *    Incorporate Fabian Keil's patch work:\rhttp://www.fabiankeil.de/sourcecode/privoxy/
+ *
  *    Revision 1.58  2006/07/18 14:48:47  david__schmidt
  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
  *    with what was really the latest development (the v_3_0_branch branch)
  *    Revision 1.58  2006/07/18 14:48:47  david__schmidt
  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
  *    with what was really the latest development (the v_3_0_branch branch)
@@ -1464,7 +1467,11 @@ jb_err server_last_modified(struct client_state *csp, char **header)
          rtime = difftime(now, last_modified);
          if (rtime)
          {
          rtime = difftime(now, last_modified);
          if (rtime)
          {
+#if !defined(_WIN32) && !defined(__OS2__)
             rtime = random() % rtime + 1; 
             rtime = random() % rtime + 1; 
+#else
+            rtime = rand() % rtime + 1; 
+#endif /* (ifndef _WIN32 || __OS2__) */
             last_modified += rtime;
             timeptr = gmtime(&last_modified);
             strftime(newheader, sizeof(newheader), "%a, %d %b %Y %T GMT", timeptr);
             last_modified += rtime;
             timeptr = gmtime(&last_modified);
             strftime(newheader, sizeof(newheader), "%a, %d %b %Y %T GMT", timeptr);
@@ -2195,8 +2202,11 @@ jb_err client_if_modified_since(struct client_state *csp, char **header)
                *header, rtime, (rtime == 1 || rtime == -1) ? "r": "rs");
 
             rtime *= 3600;
                *header, rtime, (rtime == 1 || rtime == -1) ? "r": "rs");
 
             rtime *= 3600;
+#if !defined(_WIN32) && !defined(__OS2__)
             rtime = random() % rtime; 
             rtime = random() % rtime; 
-
+#else
+            rtime = rand() % rtime; 
+#endif /* (_WIN32 || __OS2__) */
             if(newval[0] == '-')
             {
                rtime *= -1;      
             if(newval[0] == '-')
             {
                rtime *= -1;      
diff --git a/w32svrapi.c b/w32svrapi.c
new file mode 100644 (file)
index 0000000..e6f2363
--- /dev/null
@@ -0,0 +1,945 @@
+const char w32_svrapi_rcs[] = "$Id$";
+/*********************************************************************
+ *
+ * File        :  $Source$
+ *
+ * Purpose     :  Win32 Services API for Privoxy.
+ *                Provides the implementation of an Win32 service to
+ *                allow the code to directly register and run as a
+ *                native Windows service application.
+ *
+ *                Since Win9x/ME platforms don't provide or support
+ *                running programs as services, this code uses runtime
+ *                loading and calling of the Win32 Service API, to
+ *                prevent the possibility of getting "entry point not
+ *                found" type errors on unsupported platforms. This adds
+ *                a little more complexity to the code, but it is worth
+ *                doing to provide that isolation.
+ *
+ * Copyright   :  Written by and Copyright (C) 2003 members of
+ *                the Privoxy team.  http://www.privoxy.org/
+ *
+ *                Written by and Copyright (C) 2003 Ian Cummings
+ *                <ian_a_c@hotmail.com>
+ *
+ *                Special thanks to Mates Dolák <matesek@post.cz> for
+ *                some very helpful feedback and suggestions during the
+ *                development of this code.
+ *
+ *                This program is free software; you can redistribute it 
+ *                and/or modify it under the terms of the GNU General
+ *                Public License as published by the Free Software
+ *                Foundation; either version 2 of the License, or (at
+ *                your option) any later version.
+ *
+ *                This program is distributed in the hope that it will
+ *                be useful, but WITHOUT ANY WARRANTY; without even the
+ *                implied warranty of MERCHANTABILITY or FITNESS FOR A
+ *                PARTICULAR PURPOSE.  See the GNU General Public
+ *                License for more details.
+ *
+ *                The GNU General Public License should be included with
+ *                this file.  If not, you can view it at
+ *                http://www.gnu.org/copyleft/gpl.html
+ *                or write to the Free Software Foundation, Inc., 59
+ *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * Revisions   :
+ *    $Log$
+ *
+ *
+ *********************************************************************/
+\f
+
+#include "config.h"
+
+#ifdef _WIN32
+
+#include <stdio.h>
+
+#ifndef STRICT
+#define STRICT
+#endif
+#include <windows.h>
+#include <process.h>
+
+#ifndef _WIN_CONSOLE
+#  include "w32log.h"
+#endif /* ndef _WIN_CONSOLE */
+
+#include "w32svrapi.h"
+const char w32_svrapi_h_rcs[] = W32_SVRAPI_H_VERSION;
+
+
+/* Only the ANSI Win32 APIs are used at this time. If for some
+ * reason, we're building under unicode then we must stop
+ */
+#ifdef UNICODE
+#error "Privoxy interface to Win32 Services only runs under ANSI builds. Unicode is not supported at present, but you can volunteer for the job if you like! :)"
+#endif
+
+
+/* Default to not running as service, unless the command line says so */
+BOOL bRunAsService = FALSE;
+
+/* According to the Win32 docs for CreateService,
+ * the max length for the service name is 256 chars
+ */
+char szThisServiceName[260];
+
+static BOOL get_service_description(const char *pszServiceName, char *pszDisplayName, DWORD dwDispSize);
+static void WINAPI privoxy_w32_service_start(DWORD dw, LPSTR* psz);
+static void WINAPI privoxy_w32_service_handler(DWORD dwOpcode);
+SERVICE_TABLE_ENTRY w32ServiceDispatchTable[] = {{"", privoxy_w32_service_start}, {NULL, NULL}};
+static SERVICE_STATUS_HANDLE hSrv_status = 0;
+static SERVICE_STATUS srv_status;
+
+
+
+/*********************************************************************
+ * This function returns TRUE if we are running on an OS that can
+ * support services, like NT, etc. It returns FALSE for Win9x/ME.
+ *********************************************************************/
+static BOOL HasServiceControlManager()
+{
+   HMODULE     hDll;
+   FARPROC     pFunc;
+   SC_HANDLE   hScm;
+
+   /* Load the DLL with the SCM functions or return a failure status */
+   hDll = LoadLibrary("Advapi32.dll");
+   if (hDll == NULL)
+   {
+      printf("Can't load Advapi32.dll -- LoadLibrary failed!\n");
+      return FALSE;
+   }
+
+   /* Get the address of the ANSI OpenSCManager function, or return a failure status */
+   pFunc = GetProcAddress(hDll, "OpenSCManagerA");
+   if (pFunc == NULL)
+   {
+      printf("Can't find OpenSCManagerA -- GetProcAddress failed!\n");
+      FreeLibrary(hDll);
+      return FALSE;
+   }
+
+   /* Try and connect to the SCM. If it fails check and see if the error 
+    * code is ERROR_CALL_NOT_IMPLEMENTED, which means:
+    *    "This function is not supported on this system."
+    */
+   hScm = (SC_HANDLE)(*pFunc)(NULL, NULL, SC_MANAGER_CONNECT);
+   if (hScm == NULL)
+   {
+      DWORD dwErr = GetLastError();
+      if (dwErr == ERROR_CALL_NOT_IMPLEMENTED)
+      {
+         /* Expected error under Win9x/Me, so don't print any debug info
+          * here as we'll leave that up to the calling function to do
+          */
+         FreeLibrary(hDll);
+         return FALSE;
+      }
+
+      printf("Call to OpenSCManager failed -- GetLastError() returned %lu!\n", dwErr);
+      FreeLibrary(hDll);
+      return FALSE;
+   }
+
+   w32_close_service_handle(hScm);
+
+   /* OpenSCManager function exists and works, so we're on an NT type platform */
+   FreeLibrary(hDll);
+
+   return TRUE;
+
+} /* -END- HasServiceControlManager */
+
+
+BOOL CanSystemSupportServices()
+{
+   BOOL bHasScm = HasServiceControlManager();
+   return bHasScm;
+
+} /* -END- CanSystemSupportServices */
+
+
+
+/*********************************************************************
+ *
+ * The Service functions are defined in <winsvc.h> which is where
+ * the declarations used in this file are taken from
+ *
+ *********************************************************************/ 
+
+
+/*********************************************************************
+ * Open a connection to the service control manager
+ *********************************************************************/ 
+SC_HANDLE w32_open_sc_manager(
+  LPCTSTR lpMachineName,   /* computer name */
+  LPCTSTR lpDatabaseName,  /* SCM database name */
+  DWORD dwDesiredAccess)   /* access type */
+{
+   HMODULE     hDll = NULL;
+   SC_HANDLE   hScm = NULL;
+   FARPROC     pFunc = NULL;
+   DWORD       dwLastErr = 0;
+
+   /* Load the DLL with the SCM functions or return failure */
+   hDll = LoadLibrary("Advapi32.dll");
+   if (hDll == NULL)
+   {
+      return NULL;
+   }
+
+   /* Get the address of the ANSI OpenSCManager function, or return failure */
+   pFunc = GetProcAddress(hDll, "OpenSCManagerA");
+   if (pFunc == NULL)
+   {
+      FreeLibrary(hDll);
+      return NULL;
+   }
+
+   /* Call the SCM function, and save the error code */
+   hScm = (SC_HANDLE)(*pFunc)(lpMachineName, lpDatabaseName, dwDesiredAccess);
+   dwLastErr = GetLastError();
+
+   /* Release the library and then restore the last error
+    * code, in case FreeLibrary altered it.
+    */
+   FreeLibrary(hDll);
+   SetLastError(dwLastErr);
+     
+   return hScm;
+
+} /* -END- w32_open_sc_manager */
+
+
+
+BOOL w32_close_service_handle(
+  SC_HANDLE hSCObject)   /* handle to service or SCM object */
+{
+   HMODULE     hDll = NULL;
+   FARPROC     pFunc = NULL;
+   DWORD       dwLastErr = 0;
+   BOOL        bRet;
+
+   /* Load the DLL with the SCM functions or return a failure status */
+   hDll = LoadLibrary("Advapi32.dll");
+   if (hDll == NULL)
+   {
+      return FALSE;
+   }
+
+   /* Get the address of the CloseServiceHandle function, or return a failure status */
+   pFunc = GetProcAddress(hDll, "CloseServiceHandle");
+   if (pFunc == NULL)
+   {
+      FreeLibrary(hDll);
+      return FALSE;
+   }
+
+   /* Close the handle, and save the error code */
+   bRet = (BOOL)(*pFunc)(hSCObject);
+   dwLastErr = GetLastError();
+
+   /* Release the library and then restore the last error
+    * code, in case FreeLibrary altered it.
+    */
+   FreeLibrary(hDll);
+   SetLastError(dwLastErr);
+
+   return bRet;
+
+} /* -END- w32_close_service_handle */
+
+
+
+/*********************************************************************
+ * Open a service
+ *********************************************************************/ 
+SC_HANDLE w32_open_service(
+  SC_HANDLE hSCManager,   /* handle to SCM database */
+  LPCTSTR lpServiceName,  /* service name */
+  DWORD dwDesiredAccess)  /* access */
+{
+   HMODULE     hDll = NULL;
+   SC_HANDLE   hSrv = NULL;
+   FARPROC     pFunc = NULL;
+   DWORD       dwLastErr = 0;
+
+   /* Load the DLL with the SCM functions or return failure */
+   hDll = LoadLibrary("Advapi32.dll");
+   if (hDll == NULL)
+   {
+      return NULL;
+   }
+
+   /* Get the address of the ANSI OpenService function, or return failure */
+   pFunc = GetProcAddress(hDll, "OpenServiceA");
+   if (pFunc == NULL)
+   {
+      FreeLibrary(hDll);
+      return NULL;
+   }
+
+   /* Call the SCM function, and save the error code */
+   hSrv = (SC_HANDLE)(*pFunc)(hSCManager, lpServiceName, dwDesiredAccess);
+   dwLastErr = GetLastError();
+
+   /* Release the library and then restore the last error
+    * code, in case FreeLibrary altered it.
+    */
+   FreeLibrary(hDll);
+   SetLastError(dwLastErr);
+     
+   return hSrv;
+
+} /* -END- w32_open_service */
+
+
+
+SC_HANDLE w32_create_service(
+  SC_HANDLE hSCManager,       /* handle to SCM database */
+  LPCTSTR lpServiceName,      /* name of service to start */
+  LPCTSTR lpDisplayName,      /* display name */
+  DWORD dwDesiredAccess,      /* type of access to service */
+  DWORD dwServiceType,        /* type of service */
+  DWORD dwStartType,          /* when to start service */
+  DWORD dwErrorControl,       /* severity of service failure */
+  LPCTSTR lpBinaryPathName,   /* name of binary file */
+  LPCTSTR lpLoadOrderGroup,   /* name of load ordering group */
+  LPDWORD lpdwTagId,          /* tag identifier */
+  LPCTSTR lpDependencies,     /* array of dependency names */
+  LPCTSTR lpServiceStartName, /* account name */
+  LPCTSTR lpPassword)         /* account password */
+{
+   HMODULE     hDll = NULL;
+   SC_HANDLE   hSrv = NULL;
+   FARPROC     pFunc = NULL;
+   DWORD       dwLastErr = 0;
+
+   /* Load the DLL with the SCM functions or return failure */
+   hDll = LoadLibrary("Advapi32.dll");
+   if (hDll == NULL)
+   {
+      return NULL;
+   }
+
+   /* Get the address of the ANSI CreateService function, or return failure */
+   pFunc = GetProcAddress(hDll, "CreateServiceA");
+   if (pFunc == NULL)
+   {
+      FreeLibrary(hDll);
+      return NULL;
+   }
+
+   /* Call the SCM function, and save the error code */
+   hSrv = (SC_HANDLE)(*pFunc)(hSCManager,          /* handle to SCM database */
+                              lpServiceName,       /* name of service to start */
+                              lpDisplayName,       /* display name */
+                              dwDesiredAccess,     /* type of access to service */
+                              dwServiceType,       /* type of service */
+                              dwStartType,         /* when to start service */
+                              dwErrorControl,      /* severity of service failure */
+                              lpBinaryPathName,    /* name of binary file */
+                              lpLoadOrderGroup,    /* name of load ordering group */
+                              lpdwTagId,           /* tag identifier */
+                              lpDependencies,      /* array of dependency names */
+                              lpServiceStartName,  /* account name */
+                              lpPassword);         /* account password */
+   dwLastErr = GetLastError();
+
+   /* Release the library and then restore the last error
+    * code, in case FreeLibrary altered it.
+    */
+   FreeLibrary(hDll);
+   SetLastError(dwLastErr);
+     
+   return hSrv;
+
+} /* -END- w32_create_service */
+
+
+
+BOOL w32_delete_service(
+  SC_HANDLE hService)   /* handle to service */
+{
+   HMODULE     hDll = NULL;
+   FARPROC     pFunc = NULL;
+   DWORD       dwLastErr = 0;
+   BOOL        bRet;
+
+   /* Load the DLL with the SCM functions or return a failure status */
+   hDll = LoadLibrary("Advapi32.dll");
+   if (hDll == NULL)
+   {
+      return FALSE;
+   }
+
+   /* Get the address of the DeleteService function, or return a failure status */
+   pFunc = GetProcAddress(hDll, "DeleteService");
+   if (pFunc == NULL)
+   {
+      FreeLibrary(hDll);
+      return FALSE;
+   }
+
+   /* Close the handle, and save the error code */
+   bRet = (BOOL)(*pFunc)(hService);
+   dwLastErr = GetLastError();
+
+   /* Release the library and then restore the last error
+    * code, in case FreeLibrary altered it.
+    */
+   FreeLibrary(hDll);
+   SetLastError(dwLastErr);
+
+   return bRet;
+
+} /* -END- w32_delete_service */
+
+
+
+BOOL w32_query_service_config(
+  SC_HANDLE hService,                     /* handle to service */
+  LPQUERY_SERVICE_CONFIG lpServiceConfig, /* buffer */
+  DWORD cbBufSize,                        /* size of buffer */
+  LPDWORD pcbBytesNeeded)                 /* bytes needed */
+{
+   HMODULE     hDll = NULL;
+   FARPROC     pFunc = NULL;
+   DWORD       dwLastErr = 0;
+   BOOL        bRet;
+
+   /* Load the DLL with the SCM functions or return a failure status */
+   hDll = LoadLibrary("Advapi32.dll");
+   if (hDll == NULL)
+   {
+      return FALSE;
+   }
+
+   /* Get the address of the QueryServiceConfig function, or return a failure status */
+   pFunc = GetProcAddress(hDll, "QueryServiceConfigA");
+   if (pFunc == NULL)
+   {
+      FreeLibrary(hDll);
+      return FALSE;
+   }
+
+   /* Close the handle, and save the error code */
+   bRet = (BOOL)(*pFunc)(hService, lpServiceConfig, cbBufSize, pcbBytesNeeded);
+   dwLastErr = GetLastError();
+
+   /* Release the library and then restore the last error
+    * code, in case FreeLibrary altered it.
+    */
+   FreeLibrary(hDll);
+   SetLastError(dwLastErr);
+
+   return bRet;
+
+} /* -END- w32_query_service_config */
+
+
+BOOL w32_start_service_ctrl_dispatcher(
+  CONST LPSERVICE_TABLE_ENTRY lpServiceTable)   /* service table */
+{
+   HMODULE     hDll = NULL;
+   FARPROC     pFunc = NULL;
+   DWORD       dwLastErr = 0;
+   BOOL        bRet;
+
+   /* Load the DLL with the SCM functions or return a failure status */
+   hDll = LoadLibrary("Advapi32.dll");
+   if (hDll == NULL)
+   {
+      return FALSE;
+   }
+
+   /* Get the address of the StartServiceCtrlDispatcher function, or return a failure status */
+   pFunc = GetProcAddress(hDll, "StartServiceCtrlDispatcherA");
+   if (pFunc == NULL)
+   {
+      FreeLibrary(hDll);
+      return FALSE;
+   }
+
+   /* Close the handle, and save the error code */
+   bRet = (BOOL)(*pFunc)(lpServiceTable);
+   dwLastErr = GetLastError();
+
+   /* Release the library and then restore the last error
+    * code, in case FreeLibrary altered it.
+    */
+   FreeLibrary(hDll);
+   SetLastError(dwLastErr);
+
+   return bRet;
+
+} /* -END- w32_start_service_ctrl_dispatcher */
+
+
+
+SERVICE_STATUS_HANDLE w32_register_service_ctrl_handler(
+  LPCTSTR lpServiceName,               /* service name */
+  LPHANDLER_FUNCTION lpHandlerProc)    /* handler function */
+{
+   HMODULE     hDll = NULL;
+   FARPROC     pFunc = NULL;
+   DWORD       dwLastErr = 0;
+   BOOL        bRet;
+
+   /* Load the DLL with the SCM functions or return a failure status */
+   hDll = LoadLibrary("Advapi32.dll");
+   if (hDll == NULL)
+   {
+      return FALSE;
+   }
+
+   /* Get the address of the RegisterServiceCtrlHandler function, or return a failure status */
+   pFunc = GetProcAddress(hDll, "RegisterServiceCtrlHandlerA");
+   if (pFunc == NULL)
+   {
+      FreeLibrary(hDll);
+      return FALSE;
+   }
+
+   /* Close the handle, and save the error code */
+   bRet = (BOOL)(*pFunc)(lpServiceName, lpHandlerProc);
+   dwLastErr = GetLastError();
+
+   /* Release the library and then restore the last error
+    * code, in case FreeLibrary altered it.
+    */
+   FreeLibrary(hDll);
+   SetLastError(dwLastErr);
+
+   return bRet;
+
+} /* -END- w32_register_service_ctrl_handler */
+
+
+
+BOOL w32_set_service_status(
+  SERVICE_STATUS_HANDLE hServiceStatus,   /* service status handle */
+  LPSERVICE_STATUS lpServiceStatus)       /* status buffer */
+{
+   HMODULE     hDll = NULL;
+   FARPROC     pFunc = NULL;
+   DWORD       dwLastErr = 0;
+   BOOL        bRet;
+
+   /* Load the DLL with the SCM functions or return a failure status */
+   hDll = LoadLibrary("Advapi32.dll");
+   if (hDll == NULL)
+   {
+      return FALSE;
+   }
+
+   /* Get the address of the SetServiceStatus function, or return a failure status */
+   pFunc = GetProcAddress(hDll, "SetServiceStatus");
+   if (pFunc == NULL)
+   {
+      FreeLibrary(hDll);
+      return FALSE;
+   }
+
+   /* Close the handle, and save the error code */
+   bRet = (BOOL)(*pFunc)(hServiceStatus, lpServiceStatus);
+   dwLastErr = GetLastError();
+
+   /* Release the library and then restore the last error
+    * code, in case FreeLibrary altered it.
+    */
+   FreeLibrary(hDll);
+   SetLastError(dwLastErr);
+
+   return bRet;
+
+} /* -END- w32_set_service_status */
+
+
+static void display_win32_msg(BOOL bIsError, char *msg)
+{
+#ifdef _WIN_CONSOLE
+   printf("%s", msg);
+#else
+   if (bIsError)
+   {
+      MessageBox(NULL, msg, "Privoxy Error", 
+         MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
+   }
+   else
+   {
+      MessageBox(NULL, msg, "Privoxy Information", 
+         MB_OK | MB_ICONINFORMATION | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
+   }
+#endif
+
+} /* -END- display_win32_msg */
+
+
+static BOOL get_service_description(const char *pszServiceName, char *pszDisplayName, DWORD dwDispSize)
+{
+   /*********************************************************************
+    * Create a simple display name
+    *********************************************************************/
+   strcpy(pszDisplayName, "Privoxy (");
+   strncat(pszDisplayName, pszServiceName, dwDispSize - strlen(pszDisplayName) - 2);
+   strcat(pszDisplayName, ")");
+
+   return TRUE;
+}
+
+
+
+BOOL install_service(const char *service_name)
+{
+   char szModule[(MAX_PATH*2)+1];
+   char szDisplayName[MAX_PATH+2];
+   SC_HANDLE hSCM;
+   SC_HANDLE hService;
+
+   /*********************************************************************
+    * First check if this system can support a service architecture
+    *********************************************************************/
+   if (!CanSystemSupportServices())
+   {
+      display_win32_msg(TRUE, "This system doesn't support installing Privoxy as a service.\nWinNT/2000/XP are required for this feature.\n");
+      return FALSE;
+   }
+
+   /* Use a default service name if none was supplied */
+   if ((service_name == NULL) || (strlen(service_name) == 0))
+   {
+      service_name = "privoxy";
+   }
+
+   /*********************************************************************
+    * Open a handle to the Service Control Manager with full access rights
+    *********************************************************************/
+   hSCM = w32_open_sc_manager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
+   if (hSCM == NULL)
+   {
+      display_win32_msg(TRUE, "Can't open Service Control Manager - Service install failed!\n  Administrator rights are required to create a service.\n");
+      return FALSE;
+   }
+
+
+   /*********************************************************************
+    * Work out the full image path plus command line for the service
+    * We'll temporarily use szDisplayName as a second buffer.
+    *********************************************************************/
+   GetModuleFileName(NULL, szDisplayName, MAX_PATH);
+   sprintf(szModule, "\"%s\" --service", szDisplayName); 
+
+   
+   /*********************************************************************
+    * Get the display name for the service
+    *********************************************************************/
+   get_service_description(service_name, szDisplayName, sizeof(szDisplayName)/sizeof(char));
+
+
+   /*********************************************************************
+    * Create the service
+    *********************************************************************/
+   hService = w32_create_service(hSCM, 
+                           service_name,              /* the internal service name */
+                           szDisplayName,             /* the display name */
+                           SERVICE_ALL_ACCESS,        /* get full access during creation */
+                           SERVICE_WIN32_OWN_PROCESS  /* run in our own process */
+#ifndef _WIN_CONSOLE
+                         + SERVICE_INTERACTIVE_PROCESS /* GUI also wants interactive rights */
+#endif
+                           ,
+                           SERVICE_DEMAND_START,      /* For now, only start when asked to */
+                           SERVICE_ERROR_NORMAL,      /* Normal error handling by the SCM */
+                           szModule,                  /* The executable service file */
+                           NULL,                      /* No load order info needed */
+                           NULL,                      /* No load order info needed */
+                           NULL,                      /* No dependencies */
+                           NULL,                      /* Default to LocalSystem... */
+                           NULL);                     /* ...which doesn't require a password */
+   if (hService == NULL)
+   {
+      display_win32_msg(TRUE, "Can't install service!\n");
+      w32_close_service_handle(hSCM);
+      return FALSE;
+   }
+
+   display_win32_msg(FALSE, "Service was successfully created.\n*** IMPORTANT NOTE: You should now use the Services control panel to\n*** configure the startup type and user account details for the service.\n\n");
+
+   /* tidy up */
+   w32_close_service_handle(hService);
+   w32_close_service_handle(hSCM);
+   return TRUE;
+
+} /* -END- install_service */
+
+
+
+BOOL uninstall_service(const char *service_name)
+{
+   char szDisplayName[MAX_PATH+2];
+   SC_HANDLE hSCM;
+   SC_HANDLE hService;
+   BOOL bResult = FALSE;
+
+
+   /*********************************************************************
+    * First check if this system can support a service architecture
+    *********************************************************************/
+   if (!CanSystemSupportServices())
+   {
+      display_win32_msg(TRUE, "This system doesn't support installing Privoxy as a service.\nWinNT/2000/XP are required for this feature.\n");
+      return FALSE;
+   }
+
+
+   /* Use a default service name if none was supplied */
+   if ((service_name == NULL) || (strlen(service_name) == 0))
+   {
+      service_name = "privoxy";
+   }
+
+
+   /*********************************************************************
+    * Open a handle to the Service Control Manager with full access rights
+    *********************************************************************/
+   hSCM = w32_open_sc_manager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
+   if (hSCM == NULL)
+   {
+      display_win32_msg(TRUE, "Can't open Service Control Manager - Service uninstall failed!\n  Administrator rights are required to delete a service.\n");
+      return FALSE;
+   }
+
+
+   /*********************************************************************
+    * Get the display name for the service
+    *********************************************************************/
+   get_service_description(service_name, szDisplayName, sizeof(szDisplayName)/sizeof(char));
+
+
+   /*********************************************************************
+    * Open and then delete the service
+    *********************************************************************/
+   hService = w32_open_service(hSCM, service_name, DELETE);
+   if (hService == NULL)
+   {
+      display_win32_msg(TRUE, "Can't open service for delete access rights!\n");
+      w32_close_service_handle(hSCM);
+      return FALSE;
+   }
+
+   if (w32_delete_service(hService))
+   {
+      display_win32_msg(FALSE, "Service was deleted successfully.\n");
+      bResult = TRUE;
+   }
+   else
+   {
+      display_win32_msg(TRUE, "Service could not be deleted!\n");
+      bResult = FALSE;
+   }
+
+   w32_close_service_handle(hService);
+   w32_close_service_handle(hSCM);
+   return bResult;
+
+} /* -END- uninstall_service */
+
+
+
+/*********************************************************************
+ *
+ * Function    :  privoxy_w32_service_start
+ *
+ * Description :  This is the entry point function for the service.
+ *                In other words, it's the ServiceMain function.
+ *
+ * Parameters  :  Defined by the Win32 API, but not used here
+ *
+ * Returns     :  void
+ *
+ *********************************************************************/
+static void WINAPI privoxy_w32_service_start(DWORD dw, LPSTR* pszArgs)
+{
+   int child_id;
+
+   /* Arg zero is always the service name, and we need to 
+    * know it when we call RegisterServiceCtrlHandler.
+    */
+   strcpy(szThisServiceName, pszArgs[0]);
+
+   /* Tell the SCM we are running */
+   srv_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
+   srv_status.dwCurrentState = SERVICE_RUNNING;
+   srv_status.dwCheckPoint = 0;
+   srv_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
+   srv_status.dwWin32ExitCode = NO_ERROR;
+   srv_status.dwServiceSpecificExitCode = 0;
+   srv_status.dwWaitHint = 0;
+
+   hSrv_status = w32_register_service_ctrl_handler(szThisServiceName, privoxy_w32_service_handler);
+   if (!hSrv_status)
+   {
+      return;
+   }
+   w32_set_service_status(hSrv_status, &srv_status);
+
+#ifndef FEATURE_PTHREAD
+   child_id = _beginthread(w32_service_listen_loop, 0, NULL);
+   if (child_id > 0)
+#else
+#error "FIXME: Do pthread stuff here!"
+#endif
+   {
+      w32_set_service_status(hSrv_status, &srv_status);
+   }
+   else
+   {
+      srv_status.dwCurrentState = SERVICE_STOPPED;
+      srv_status.dwCheckPoint = 0;
+      srv_status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
+      srv_status.dwServiceSpecificExitCode = ERROR_SERVICE_NO_THREAD;
+      w32_set_service_status(hSrv_status, &srv_status);
+   }
+}
+
+
+/*********************************************************************
+ *
+ * Function    :  w32_set_service_cwd
+ *
+ * Description :  Simple function to change the current directory to
+ *                the same location as the service executable.
+ *
+ * Parameters  :  void
+ *
+ * Returns     :  void
+ *
+ *********************************************************************/
+void w32_set_service_cwd(void)
+{
+   char exe_name[MAX_PATH+1];
+   char dir_name[MAX_PATH+1];
+   char *pszFile = NULL;
+
+   /* Get the exe name and path of the service */
+   if (GetModuleFileName(NULL, exe_name, MAX_PATH))
+   {
+      /* Ask the API to tell us where the filename portion starts */
+      if (GetFullPathName(exe_name, MAX_PATH, dir_name, &pszFile))
+      {
+         /* remove the filename from the string */
+         if (pszFile != NULL)
+         {
+            *pszFile = '\0';
+            /* We have just a directory path now, so make it current */
+            SetCurrentDirectory(dir_name);
+         }
+      }
+   }
+}
+
+
+/*********************************************************************
+ *
+ * Function    :  w32_service_exit_notify
+ *
+ * Description :  This is a simple atexit function that is called by the
+ *                C runtime after exit has been called. It allows the
+ *                service code to detect when the app is about to die and
+ *                send an quick notification to the SCM that the service
+ *                now stopped.
+ *
+ * Parameters  :  void
+ *
+ * Returns     :  void
+ *
+ *********************************************************************/
+void w32_service_exit_notify(void)
+{
+   if (hSrv_status != 0)
+   {
+      if (srv_status.dwCurrentState != SERVICE_STOPPED)
+      {
+         srv_status.dwCurrentState = SERVICE_STOPPED;
+         srv_status.dwCheckPoint = 0;
+         srv_status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
+         srv_status.dwServiceSpecificExitCode = ERROR_PROCESS_ABORTED;
+         w32_set_service_status(hSrv_status, &srv_status);
+      }
+   }
+}
+
+
+static void w32_mini_exit(void *p)
+{
+   Sleep(100);
+#ifdef _WIN_CONSOLE
+   exit(0);
+#else
+   PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
+#endif /* def _WIN_CONSOLE */
+}
+
+
+/*********************************************************************
+ *
+ * Function    :  privoxy_w32_service_handler
+ *
+ * Description :  This is the control message handler function for
+ *                the service.
+ *
+ * Parameters  :  dwOpcode
+ *                   requested control code sent by SCM
+ *
+ * Returns     :  void
+ *
+ *********************************************************************/
+static void WINAPI privoxy_w32_service_handler(DWORD dwOpcode)
+{
+   switch(dwOpcode)
+   {
+      case SERVICE_CONTROL_STOP:
+         /* We've stopped
+          */
+         srv_status.dwCurrentState = SERVICE_STOPPED;
+         srv_status.dwCheckPoint = 0;
+         srv_status.dwWin32ExitCode = NO_ERROR;
+         srv_status.dwServiceSpecificExitCode = 0;
+
+         /* Maybe there is a more friendly way to stop, but this will do for now! */
+         w32_set_service_status(hSrv_status, &srv_status);
+
+         /* During testing, I kept getting error 109 (ERROR_BROKEN_PIPE) and
+          * as far as the SCM was concerned the service was still stopping,
+          * even after the process had disappeared.
+          *
+          * It seems that if we call exit in the ServiceMain thread, it causes
+          * the SCM to not recieve the status we sent in the line above. The
+          * simple fix was to create a new thread to actually call exit for us
+          * whilst this thread continues and returns to its caller.
+          */
+
+         if (_beginthread(w32_mini_exit, 0, NULL) < 0)
+         {
+            /* we failed to create the exit thread, so just force an exit here
+             * and the SCM will just have to go and whistle!
+             */
+            exit(0);
+         }
+         break;
+
+      default:
+         break;
+   }
+
+   w32_set_service_status(hSrv_status, &srv_status);
+}
+
+
+#endif /* ifdef _WIN32 */
+
diff --git a/w32svrapi.h b/w32svrapi.h
new file mode 100644 (file)
index 0000000..ca69a61
--- /dev/null
@@ -0,0 +1,143 @@
+#ifndef W32_SVRAPI_H_INCLUDED
+#define W32_SVRAPI_H_INCLUDED
+#define W32_SVRAPI_H_VERSION "$Id$"
+/*********************************************************************
+ *
+ * File        :  $Source$
+ *
+ * Purpose     :  Win32 Services API for Privoxy.
+ *                Provides the implementation of an Win32 service to
+ *                allow the code to directly register and run as a
+ *                native Windows service application.
+ *
+ *                Since Win9x/ME platforms don't provide or support
+ *                running programs as services, this code uses runtime
+ *                loading and calling of the Win32 Service API, to
+ *                prevent the possibility of getting "entry point not
+ *                found" type errors on unsupported platforms. This adds
+ *                a little more complexity to the code, but it is worth
+ *                doing to provide that isolation.
+ *
+ * Copyright   :  Written by and Copyright (C) 2003 members of
+ *                the Privoxy team.  http://www.privoxy.org/
+ *
+ *                Written by and Copyright (C) 2003 Ian Cummings
+ *                <ian_a_c@hotmail.com>
+ *
+ *                Special thanks to Mates Dolák <matesek@post.cz> for
+ *                some very helpful feedback and suggestions during the
+ *                development of this code.
+ *
+ *
+ *                This program is free software; you can redistribute it 
+ *                and/or modify it under the terms of the GNU General
+ *                Public License as published by the Free Software
+ *                Foundation; either version 2 of the License, or (at
+ *                your option) any later version.
+ *
+ *                This program is distributed in the hope that it will
+ *                be useful, but WITHOUT ANY WARRANTY; without even the
+ *                implied warranty of MERCHANTABILITY or FITNESS FOR A
+ *                PARTICULAR PURPOSE.  See the GNU General Public
+ *                License for more details.
+ *
+ *                The GNU General Public License should be included with
+ *                this file.  If not, you can view it at
+ *                http://www.gnu.org/copyleft/gpl.html
+ *                or write to the Free Software Foundation, Inc., 59
+ *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * Revisions   :
+ *    $Log$
+ *
+ *
+ *********************************************************************/
+\f
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef _WIN32
+
+
+extern char szThisServiceName[];
+extern BOOL bRunAsService;
+extern SERVICE_TABLE_ENTRY w32ServiceDispatchTable[];
+
+extern BOOL install_service(const char *service_name);
+extern BOOL uninstall_service(const char *service_name);
+extern void w32_service_exit_notify(void);
+extern void w32_set_service_cwd(void);
+extern void w32_service_listen_loop(void *p);
+
+
+extern BOOL CanSystemSupportServices();
+
+
+extern SC_HANDLE w32_open_sc_manager(
+  LPCTSTR lpMachineName,   /* computer name */
+  LPCTSTR lpDatabaseName,  /* SCM database name */
+  DWORD dwDesiredAccess);  /* access type */
+
+
+extern BOOL w32_close_service_handle(
+  SC_HANDLE hSCObject);    /* handle to service or SCM object */
+
+
+extern SC_HANDLE w32_open_service(
+  SC_HANDLE hSCManager,    /* handle to SCM database */
+  LPCTSTR lpServiceName,   /* service name */
+  DWORD dwDesiredAccess);  /* access */
+
+
+extern SC_HANDLE w32_create_service(
+  SC_HANDLE hSCManager,       /* handle to SCM database */
+  LPCTSTR lpServiceName,      /* name of service to start */
+  LPCTSTR lpDisplayName,      /* display name */
+  DWORD dwDesiredAccess,      /* type of access to service */
+  DWORD dwServiceType,        /* type of service */
+  DWORD dwStartType,          /* when to start service */
+  DWORD dwErrorControl,       /* severity of service failure */
+  LPCTSTR lpBinaryPathName,   /* name of binary file */
+  LPCTSTR lpLoadOrderGroup,   /* name of load ordering group */
+  LPDWORD lpdwTagId,          /* tag identifier */
+  LPCTSTR lpDependencies,     /* array of dependency names */
+  LPCTSTR lpServiceStartName, /* account name */
+  LPCTSTR lpPassword);        /* account password */
+
+
+extern BOOL w32_delete_service(
+  SC_HANDLE hService);   /* handle to service */
+
+
+extern BOOL w32_query_service_config(
+  SC_HANDLE hService,                     /* handle to service */
+  LPQUERY_SERVICE_CONFIG lpServiceConfig, /* buffer */
+  DWORD cbBufSize,                        /* size of buffer */
+  LPDWORD pcbBytesNeeded);                /* bytes needed */
+
+
+extern BOOL w32_start_service_ctrl_dispatcher(
+  CONST LPSERVICE_TABLE_ENTRY lpServiceTable);   /* service table */
+
+
+extern SERVICE_STATUS_HANDLE w32_register_service_ctrl_handler(
+  LPCTSTR lpServiceName,               /* service name */
+  LPHANDLER_FUNCTION lpHandlerProc);   /* handler function */
+
+
+extern BOOL w32_set_service_status(
+  SERVICE_STATUS_HANDLE hServiceStatus,   /* service status handle */
+  LPSERVICE_STATUS lpServiceStatus);      /* status buffer */
+
+
+#endif  /* def _WIN32 */
+
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* ndef W32_SVRAPI_H_INCLUDED */
+  
diff --git a/win32.c b/win32.c
index 3115b05..750dc44 100644 (file)
--- a/win32.c
+++ b/win32.c
@@ -1,7 +1,7 @@
-const char win32_rcs[] = "$Id: win32.c,v 1.9.2.2 2002/08/27 18:03:40 oes Exp $";
+const char win32_rcs[] = "$Id: win32.c,v 1.11 2006/07/18 14:48:48 david__schmidt Exp $";
 /*********************************************************************
  *
 /*********************************************************************
  *
- * File        :  $Source: /cvsroot/ijbswa/current/Attic/win32.c,v $
+ * File        :  $Source: /cvsroot/ijbswa/current/win32.c,v $
  *
  * Purpose     :  Win32 User Interface initialization and message loop
  *
  *
  * Purpose     :  Win32 User Interface initialization and message loop
  *
@@ -31,6 +31,10 @@ const char win32_rcs[] = "$Id: win32.c,v 1.9.2.2 2002/08/27 18:03:40 oes Exp $";
  *
  * Revisions   :
  *    $Log: win32.c,v $
  *
  * Revisions   :
  *    $Log: win32.c,v $
+ *    Revision 1.11  2006/07/18 14:48:48  david__schmidt
+ *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
+ *    with what was really the latest development (the v_3_0_branch branch)
+ *
  *    Revision 1.9.2.2  2002/08/27 18:03:40  oes
  *    Fixed stupid typo
  *
  *    Revision 1.9.2.2  2002/08/27 18:03:40  oes
  *    Fixed stupid typo
  *
@@ -99,6 +103,10 @@ const char win32_rcs[] = "$Id: win32.c,v 1.9.2.2 2002/08/27 18:03:40 oes Exp $";
 
 const char win32_h_rcs[] = WIN32_H_VERSION;
 
 
 const char win32_h_rcs[] = WIN32_H_VERSION;
 
+/**
+ * A short introductory text about Privoxy.  Used for the "About" box
+ * or the console startup message.
+ */
 const char win32_blurb[] =
 "Privoxy version " VERSION " for Windows\n"
 "Copyright (C) 2000-2002 the Privoxy Team (" HOME_PAGE_URL ")\n"
 const char win32_blurb[] =
 "Privoxy version " VERSION " for Windows\n"
 "Copyright (C) 2000-2002 the Privoxy Team (" HOME_PAGE_URL ")\n"
@@ -109,17 +117,32 @@ const char win32_blurb[] =
 
 #ifdef _WIN_CONSOLE
 
 
 #ifdef _WIN_CONSOLE
 
+/**
+ * Hide the console.  If set, the program will disconnect from the 
+ * console and run in the background.  This allows the command-prompt
+ * window to close.
+ */
 int hideConsole     = 0;
 
 int hideConsole     = 0;
 
-#else
 
 
+#else /* ndef _WIN_CONSOLE */
+
+
+/**
+ * The application instance handle.
+ */
 HINSTANCE g_hInstance;
 HINSTANCE g_hInstance;
+
+
+/**
+ * The command to show the window that was specified at startup.
+ */
 int g_nCmdShow;
 
 static void  __cdecl UserInterfaceThread(void *);
 
 int g_nCmdShow;
 
 static void  __cdecl UserInterfaceThread(void *);
 
-#endif
 
 
+#endif /* ndef _WIN_CONSOLE */
 
 /*********************************************************************
  *
 
 /*********************************************************************
  *
@@ -141,11 +164,14 @@ static void  __cdecl UserInterfaceThread(void *);
  *********************************************************************/
 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
 {
  *********************************************************************/
 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
 {
+#if 0   /* See comment about __argc & __argv below */
    int i;
    int i;
-   int res;
    int argc = 1;
    const char *argv[3];
    char szModule[MAX_PATH+1];
    int argc = 1;
    const char *argv[3];
    char szModule[MAX_PATH+1];
+#endif
+
+   int res;
 #ifndef _WIN_CONSOLE
    HANDLE hInitCompleteEvent = NULL;
 #endif
 #ifndef _WIN_CONSOLE
    HANDLE hInitCompleteEvent = NULL;
 #endif
@@ -169,6 +195,16 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
 #endif
 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
 
 #endif
 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
 
+
+/************
+ * I couldn't figure out why the command line was being sorta parsed here
+ * instead of using the __argc & __argv globals usually defined in stdlib.h
+ *
+ * From what I can tell by looking at the MinWG source, it supports these
+ * globals, so i'd hope that the other compilers do so as well.
+ * Obviously, if i'm wrong i'll find out soon enough!  :)
+ ************/
+#if 0
    /*
     * Cheat in parsing the command line.  We only ever have at most one
     * paramater, which may optionally be specified inside double quotes.
    /*
     * Cheat in parsing the command line.  We only ever have at most one
     * paramater, which may optionally be specified inside double quotes.
@@ -199,6 +235,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
    argv[1] = lpCmdLine;
    argv[2] = NULL;
    argc = ((lpCmdLine != NULL) ? 2 : 1);
    argv[1] = lpCmdLine;
    argv[2] = NULL;
    argc = ((lpCmdLine != NULL) ? 2 : 1);
+#endif /* -END- 0 */
+
 
 #ifndef _WIN_CONSOLE
    /* Create a user-interface thread and wait for it to initialise */
 
 #ifndef _WIN_CONSOLE
    /* Create a user-interface thread and wait for it to initialise */
@@ -211,9 +249,9 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
 #endif
 
 #ifdef __MINGW32__
 #endif
 
 #ifdef __MINGW32__
-   res = real_main( argc, argv );
+   res = real_main( __argc, __argv );
 #else
 #else
-   res = main( argc, argv );
+   res = main( __argc, __argv );
 #endif
 
    return res;
 #endif
 
    return res;
@@ -306,7 +344,7 @@ static void __cdecl UserInterfaceThread(void *pData)
 }
 
 
 }
 
 
-#endif
+#endif /* ndef _WIN_CONSOLE */
 
 
 /*
 
 
 /*