Fix for `slow-startup-printer': use PRINTER_INFO_4 on NT-class systems, which
authorjacob <jacob@cda61777-01e9-0310-a592-d414129be87e>
Thu, 21 Aug 2003 19:48:45 +0000 (19:48 +0000)
committerjacob <jacob@cda61777-01e9-0310-a592-d414129be87e>
Thu, 21 Aug 2003 19:48:45 +0000 (19:48 +0000)
apparently tries less hard to find printers so won't slow the system down.

Tested on 2000 and 98; in both cases printer enumeration and printing worked
as well as they did in 2003-08-21.

Made a single shared copy of osVersion in winmisc.c so that printing.c can
find it. Made other users (window.c, pageant.c) use this copy.

git-svn-id: svn://svn.tartarus.org/sgt/putty@3411 cda61777-01e9-0310-a592-d414129be87e

pageant.c
printing.c
window.c
winmisc.c
winstuff.h

index e8428b0..0ca6b95 100644 (file)
--- a/pageant.c
+++ b/pageant.c
@@ -1810,7 +1810,6 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
 {
     WNDCLASS wndclass;
     MSG msg;
-    OSVERSIONINFO osi;
     HMODULE advapi;
     char *command = NULL;
     int added_keys = 0;
@@ -1821,9 +1820,11 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
      * Determine whether we're an NT system (should have security
      * APIs) or a non-NT system (don't do security).
      */
-    memset(&osi, 0, sizeof(OSVERSIONINFO));
-    osi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
-    if (GetVersionEx(&osi) && osi.dwPlatformId == VER_PLATFORM_WIN32_NT) {
+    if (!init_winver())
+    {
+       modalfatalbox("Windows refuses to report a version");
+    }
+    if (osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT) {
        has_security = TRUE;
     } else
        has_security = FALSE;
index fdd96bf..0b619f1 100644 (file)
@@ -4,35 +4,22 @@
 
 #include <windows.h>
 #include "putty.h"
-
-/*
- * Boggle. Flipping between the two branches of this #if appears to
- * make all the difference as to whether network printers show up
- * under PRINTER_ENUM_CONNECTIONS on NT 4. I don't pretend to
- * understand this...
- */
-#if 0
-#define ENUM_LEVEL 5
-#define ENUM_PTR LPPRINTER_INFO_5
-#define ENUM_TYPE PRINTER_INFO_5
-#define ENUM_MEMBER pPrinterName
-#else
-#define ENUM_LEVEL 1
-#define ENUM_PTR LPPRINTER_INFO_1
-#define ENUM_TYPE PRINTER_INFO_1
-#define ENUM_MEMBER pName
-#endif
+#include "winstuff.h"
 
 struct printer_enum_tag {
     int nprinters;
-    ENUM_PTR info;
+    DWORD enum_level;
+    union {
+       LPPRINTER_INFO_4 i4;
+       LPPRINTER_INFO_5 i5;
+    } info;
 };
 
 struct printer_job_tag {
     HANDLE hprinter;
 };
 
-static char *printer_add_enum(int param, char *buffer,
+static char *printer_add_enum(int param, DWORD level, char *buffer,
                               int offset, int *nprinters_ptr)
 {
     DWORD needed, nprinters;
@@ -44,7 +31,7 @@ static char *printer_add_enum(int param, char *buffer,
      * we'll need for the output. Discard the return value since it
      * will almost certainly be a failure due to lack of space.
      */
-    EnumPrinters(param, NULL, ENUM_LEVEL, buffer+offset, 512,
+    EnumPrinters(param, NULL, level, buffer+offset, 512,
                 &needed, &nprinters);
 
     if (needed < 512)
@@ -52,7 +39,7 @@ static char *printer_add_enum(int param, char *buffer,
 
     buffer = sresize(buffer, offset+needed, char);
 
-    if (EnumPrinters(param, NULL, ENUM_LEVEL, buffer+offset,
+    if (EnumPrinters(param, NULL, level, buffer+offset,
                      needed, &needed, &nprinters) == 0)
         return NULL;
 
@@ -69,14 +56,37 @@ printer_enum *printer_start_enum(int *nprinters_ptr)
     *nprinters_ptr = 0;                       /* default return value */
     buffer = snewn(512, char);
 
+    /*
+     * Determine what enumeration level to use.
+     * When enumerating printers, we need to use PRINTER_INFO_4 on
+     * NT-class systems to avoid Windows looking too hard for them and
+     * slowing things down; and we need to avoid PRINTER_INFO_5 as
+     * we've seen network printers not show up.
+     * On 9x-class systems, PRINTER_INFO_4 isn't available and
+     * PRINTER_INFO_5 is recommended.
+     * Bletch.
+     */
+    if (osVersion.dwPlatformId != VER_PLATFORM_WIN32_NT) {
+       ret->enum_level = 5;
+    } else {
+       ret->enum_level = 4;
+    }
+
     retval = printer_add_enum(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
-                             buffer, 0, nprinters_ptr);
+                             ret->enum_level, buffer, 0, nprinters_ptr);
     if (!retval)
         goto error;
     else
         buffer = retval;
 
-    ret->info = (ENUM_PTR)buffer;
+    switch (ret->enum_level) {
+      case 4:
+       ret->info.i4 = (LPPRINTER_INFO_4)buffer;
+       break;
+      case 5:
+       ret->info.i5 = (LPPRINTER_INFO_5)buffer;
+       break;
+    }
     ret->nprinters = *nprinters_ptr;
     
     return ret;
@@ -94,14 +104,28 @@ char *printer_get_name(printer_enum *pe, int i)
        return NULL;
     if (i < 0 || i >= pe->nprinters)
        return NULL;
-    return pe->info[i].ENUM_MEMBER;
+    switch (pe->enum_level) {
+      case 4:
+       return pe->info.i4[i].pPrinterName;
+      case 5:
+       return pe->info.i5[i].pPrinterName;
+      default:
+       return NULL;
+    }
 }
 
 void printer_finish_enum(printer_enum *pe)
 {
     if (!pe)
        return;
-    sfree(pe->info);
+    switch (pe->enum_level) {
+      case 4:
+       sfree(pe->info.i4);
+       break;
+      case 5:
+       sfree(pe->info.i5);
+       break;
+    }
     sfree(pe);
 }
 
index 794efc2..ea0077d 100644 (file)
--- a/window.c
+++ b/window.c
@@ -179,8 +179,6 @@ static int compose_state = 0;
 
 static int wsa_started = 0;
 
-static OSVERSIONINFO osVersion;
-
 static UINT wm_mousewheel = WM_MOUSEWHEEL;
 
 /* Dummy routine, only required in plink. */
@@ -221,16 +219,13 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
      * config box. */
     defuse_showwindow();
 
+    if (!init_winver())
     {
-       ZeroMemory(&osVersion, sizeof(osVersion));
-       osVersion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
-       if (!GetVersionEx ( (OSVERSIONINFO *) &osVersion)) {
-           char *str = dupprintf("%s Fatal Error", appname);
-            MessageBox(NULL, "Windows refuses to report a version",
-                       str, MB_OK | MB_ICONEXCLAMATION);
-           sfree(str);
-           return 1;
-        }
+       char *str = dupprintf("%s Fatal Error", appname);
+       MessageBox(NULL, "Windows refuses to report a version",
+                  str, MB_OK | MB_ICONEXCLAMATION);
+       sfree(str);
+       return 1;
     }
 
     /*
index 916e988..10c9fe3 100644 (file)
--- a/winmisc.c
+++ b/winmisc.c
@@ -8,6 +8,8 @@
 #include "putty.h"
 #include "winstuff.h"
 
+OSVERSIONINFO osVersion;
+
 void platform_get_x11_auth(char *display, int *proto,
                            unsigned char *data, int *datalen)
 {
@@ -88,6 +90,13 @@ void SaneEndDialog(HWND hwnd, int ret)
     SetWindowLong(hwnd, BOXFLAGS, DF_END);
 }
 
+BOOL init_winver(void)
+{
+    ZeroMemory(&osVersion, sizeof(osVersion));
+    osVersion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
+    return GetVersionEx ( (OSVERSIONINFO *) &osVersion);
+}
+
 #ifdef DEBUG
 static FILE *debug_fp = NULL;
 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
index 37c66bb..61b3479 100644 (file)
@@ -322,6 +322,9 @@ int SaneDialogBox(HINSTANCE hinst,
 
 void SaneEndDialog(HWND hwnd, int ret);
 
+extern OSVERSIONINFO osVersion;
+BOOL init_winver(void);
+
 /*
  * Exports from sizetip.c.
  */