Index: include/config.h
===================================================================
RCS file: /cvsroot/squid/squid3/include/config.h,v
retrieving revision 1.3
diff -u -p -r1.3 config.h
--- include/config.h	18 Nov 2002 03:12:54 -0000	1.3
+++ include/config.h	23 Apr 2003 17:44:05 -0000
@@ -110,9 +110,11 @@
 
 #elif defined(__CYGWIN32__)  || defined(__CYGWIN__)
 #define _SQUID_CYGWIN_
+#define _SQUID_WIN32_
 
 #elif defined(WIN32) || defined(WINNT) || defined(__WIN32__) || defined(__WIN32)
 #define _SQUID_MSWIN_
+#define _SQUID_WIN32_
 
 #elif defined(__APPLE__)
 #define _SQUID_APPLE_
Index: src/dns_internal.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/dns_internal.cc,v
retrieving revision 1.11
diff -u -p -r1.11 dns_internal.cc
--- src/dns_internal.cc	23 Apr 2003 02:14:12 -0000	1.11
+++ src/dns_internal.cc	23 Apr 2003 17:44:06 -0000
@@ -42,7 +42,7 @@
    using external DNS process.
  */
 #ifndef USE_DNSSERVERS
-#if defined(_SQUID_MSWIN_) || defined(_SQUID_CYGWIN_)
+#ifdef _SQUID_WIN32_
 #include "squid_windows.h"
 #endif
 #ifndef _PATH_RESOLV_CONF
@@ -99,7 +99,7 @@ static void idnsParseNameservers(void);
 #ifndef _SQUID_MSWIN_
 static void idnsParseResolvConf(void);
 #endif
-#if defined(_SQUID_MSWIN_) || defined(_SQUID_CYGWIN_)
+#ifdef _SQUID_WIN32_
 static void idnsParseWIN32Registry(void);
 #endif
 static void idnsSendQuery(idns_query * q);
@@ -217,7 +217,7 @@ idnsParseResolvConf(void)
 
 #endif
 
-#if defined(_SQUID_MSWIN_) || defined(_SQUID_CYGWIN_)
+#ifdef _SQUID_WIN32_
 static void
 idnsParseWIN32Registry(void)
 {
@@ -798,7 +798,7 @@ idnsInit(void)
         idnsParseResolvConf();
 
 #endif
-#if defined(_SQUID_MSWIN_) || defined(_SQUID_CYGWIN_)
+#ifdef _SQUID_WIN32_
 
     if (0 == nns)
         idnsParseWIN32Registry();
@@ -807,7 +807,7 @@ idnsInit(void)
 
     if (0 == nns)
         fatal("Could not find any nameservers.\n"
-#if defined(_SQUID_MSWIN_) || defined(_SQUID_CYGWIN_)
+#ifdef _SQUID_WIN32_
               "       Please check your TCP-IP settings or /etc/resolv.conf file\n"
 #else
               "       Please check your /etc/resolv.conf file\n"
Index: src/main.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/main.cc,v
retrieving revision 1.19
diff -u -p -r1.19 main.cc
--- src/main.cc	23 Apr 2003 02:14:12 -0000	1.19
+++ src/main.cc	23 Apr 2003 17:44:06 -0000
@@ -607,7 +607,7 @@ mainInitialize(void)
                  version_string,
                  CONFIG_HOST_TYPE);
 
-#if defined(_SQUID_MSWIN_) || defined(_SQUID_CYGWIN_)
+#ifdef _SQUID_WIN32_
 
     if (WIN32_run_mode == _WIN_SQUID_RUN_MODE_SERVICE) {
         debug(1, 0) ("Running as %s Windows System Service on %s\n", WIN32_Service_name, WIN32_OS_string);
@@ -758,16 +758,18 @@ mainInitialize(void)
     configured_once = 1;
 }
 
+#if USE_WIN32_SERVICE
+/* When USE_WIN32_SERVICE is defined, the main function is placed in win32.cc */
+extern "C" void WINAPI
+    SquidMain(int argc, char **argv)
+#else
 int
 main(int argc, char **argv)
+#endif
 {
     int errcount = 0;
     int n;			/* # of GC'd objects */
     mode_t oldmask;
-#if defined(_SQUID_MSWIN_) || defined(_SQUID_CYGWIN_)
-
-    int WIN32_init_err;
-#endif
 
 #if HAVE_SBRK
 
@@ -779,10 +781,21 @@ main(int argc, char **argv)
     if (FD_SETSIZE < Squid_MaxFD)
         Squid_MaxFD = FD_SETSIZE;
 
-#if defined(_SQUID_MSWIN_) || defined(_SQUID_CYGWIN_)
+#ifdef _SQUID_WIN32_
+#ifdef USE_WIN32_SERVICE
+
+if (WIN32_Subsystem_Init(&argc, &argv))
+    return;
+
+#else
+
+{
+    int WIN32_init_err;
 
     if ((WIN32_init_err = WIN32_Subsystem_Init()))
         return WIN32_init_err;
+}
+#endif
 
 #endif
 
@@ -836,8 +849,33 @@ main(int argc, char **argv)
 
     failure_notify = fatal_dump;
 
+#if USE_WIN32_SERVICE
+
+    WIN32_svcstatusupdate(SERVICE_START_PENDING, 10000);
+
+#endif
+
     mainParseOptions(argc, argv);
 
+#if USE_WIN32_SERVICE
+
+    if (opt_install_service) {
+        WIN32_InstallService();
+        return;
+    }
+
+    if (opt_remove_service) {
+        WIN32_RemoveService();
+        return;
+    }
+
+    if (opt_command_line) {
+        WIN32_SetServiceCommandLine();
+        return;
+    }
+
+#endif
+
     /* parse configuration file
      * note: in "normal" case this used to be called from mainInitialize() */
     {
@@ -867,7 +905,16 @@ main(int argc, char **argv)
         parse_err = parseConfigFile(ConfigFile);
 
         if (opt_parse_cfg_only)
+#if USE_WIN32_SERVICE
+
+            return;
+
+#else
+
             return parse_err;
+
+#endif
+
     }
     if (-1 == opt_send_signal)
         if (checkRunningPid())
@@ -909,7 +956,14 @@ main(int argc, char **argv)
         setEffectiveUser();
         debug(0, 0) ("Creating Swap Directories\n");
         storeCreateSwapDirectories();
+#if USE_WIN32_SERVICE
+
+        return;
+#else
+
         return 0;
+#endif
+
     }
 
     if (!opt_no_daemon)
@@ -933,8 +987,20 @@ main(int argc, char **argv)
         fd_open(2, FD_LOG, "stderr");
     }
 
+#if USE_WIN32_SERVICE
+
+    WIN32_svcstatusupdate(SERVICE_START_PENDING, 10000);
+
+#endif
+
     mainInitialize();
 
+#if USE_WIN32_SERVICE
+
+    WIN32_svcstatusupdate(SERVICE_RUNNING, 0);
+
+#endif
+
     /* main loop */
 
     for (;;) {
@@ -952,6 +1018,11 @@ main(int argc, char **argv)
                          (int) wait);
             do_shutdown = 0;
             shutting_down = 1;
+#if USE_WIN32_SERVICE
+
+            WIN32_svcstatusupdate(SERVICE_STOP_PENDING, (wait + 1) * 1000);
+#endif
+
             serverConnectionsClose();
 #if USE_DNSSERVERS
 
@@ -1008,7 +1079,14 @@ main(int argc, char **argv)
     }
 
     /* NOTREACHED */
+#if USE_WIN32_SERVICE
+    return;
+
+#else
+
     return 0;
+
+#endif
 }
 
 static void
@@ -1019,14 +1097,35 @@ sendSignal(void)
     pid = readPidFile();
 
     if (pid > 1) {
+#if USE_WIN32_SERVICE
+
+        if (opt_signal_service)
+            WIN32_sendSignal(opt_send_signal);
+        else {
+#endif
+#if defined(_SQUID_MSWIN_) && defined(USE_WIN32_SERVICE)
+            fprintf(stderr, "%s: ERROR: Could not send ", appname);
+            fprintf(stderr, "signal to Squid Service:\n");
+            fprintf(stderr, "missing -n command line switch.\n");
+#else
+
         if (kill(pid, opt_send_signal) &&
                 /* ignore permissions if just running check */
                 !(opt_send_signal == 0 && errno == EPERM)) {
             fprintf(stderr, "%s: ERROR: Could not send ", appname);
             fprintf(stderr, "signal %d to process %d: %s\n",
                     opt_send_signal, (int) pid, xstrerror());
+#endif
+
             exit(1);
         }
+
+#if defined(USE_WIN32_SERVICE) && defined(_SQUID_CYGWIN_)
+
+    }
+
+#endif
+
     } else {
         fprintf(stderr, "%s: ERROR: No running copy\n", appname);
         exit(1);
@@ -1245,6 +1344,10 @@ watch_child(char *argv[])
 static void
 SquidShutdown(void *unused)
 {
+#if USE_WIN32_SERVICE
+    WIN32_svcstatusupdate(SERVICE_STOP_PENDING, 10000);
+#endif
+
     debug(1, 1) ("Shutting down...\n");
     icpConnectionClose();
 #if USE_HTCP
@@ -1271,6 +1374,10 @@ SquidShutdown(void *unused)
 #if USE_UNLINKD
 
     unlinkdClose();
+#endif
+#if USE_WIN32_SERVICE
+
+    WIN32_svcstatusupdate(SERVICE_STOP_PENDING, 10000);
 #endif
 
     storeDirSync();		/* Flush pending object writes/unlinks */
Index: src/stat.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/stat.cc,v
retrieving revision 1.13
diff -u -p -r1.13 stat.cc
--- src/stat.cc	23 Apr 2003 02:14:12 -0000	1.13
+++ src/stat.cc	23 Apr 2003 17:44:06 -0000
@@ -455,7 +455,7 @@ info_get(StoreEntry * sentry)
     storeAppendPrintf(sentry, "Squid Object Cache: Version %s\n",
                       version_string);
 
-#if defined(_SQUID_MSWIN_) || defined(_SQUID_CYGWIN_)
+#ifdef _SQUID_WIN32_
 
     if (WIN32_run_mode == _WIN_SQUID_RUN_MODE_SERVICE) {
         storeAppendPrintf(sentry,"\nRunning as %s Windows System Service on %s\n",
