Moved implementation of GetDefaultPrinter from ascii to unicode, added
authorStefan Leichter <Stefan.Leichter@camLine.com>
Sun, 7 Mar 2004 03:46:54 +0000 (03:46 +0000)
committerAlexandre Julliard <julliard@winehq.org>
Sun, 7 Mar 2004 03:46:54 +0000 (03:46 +0000)
tests for GetDefaultPrinterA.

dlls/winspool/info.c
dlls/winspool/tests/info.c

index 5dd9645fb326a7fc23ca9e6caba5c1336c27c9dc..c6a713b5886ded3e867e7dec4e027d7a563df03f 100644 (file)
@@ -101,7 +101,10 @@ static const WCHAR Separator_FileW[] = {'S','e','p','a','r','a','t','o','r',' ',
                                  'i','l','e',0};
 static const WCHAR Share_NameW[] = {'S','h','a','r','e',' ','N','a','m','e',0};
 static const WCHAR WinPrintW[] = {'W','i','n','P','r','i','n','t',0};
+static const WCHAR deviceW[]  = {'d','e','v','i','c','e',0};
 static const WCHAR devicesW[] = {'d','e','v','i','c','e','s',0};
+static const WCHAR windowsW[] = {'w','i','n','d','o','w','s',0};
+static const WCHAR emptyStringW[] = {0};
 
 static const WCHAR May_Delete_Value[] = {'W','i','n','e','M','a','y','D','e','l','e','t','e','M','e',0};
 
@@ -3114,66 +3117,102 @@ BOOL WINAPI EnumPortsA(LPSTR name,DWORD level,LPBYTE buffer,DWORD bufsize,
     return TRUE;
 }
 
+
 /******************************************************************************
- *             GetDefaultPrinterA   (WINSPOOL.@)
+ *             GetDefaultPrinterW   (WINSPOOL.@)
+ *
+ * FIXME
+ *     This function must read the value from data 'device' of key
+ *     HCU\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Windows
  */
-BOOL WINAPI GetDefaultPrinterA(LPSTR name, LPDWORD namesize)
+BOOL WINAPI GetDefaultPrinterW(LPWSTR name, LPDWORD namesize)
 {
-   char *ptr;
+    BOOL  retval = TRUE;
+    DWORD insize, len;
+    WCHAR *buffer, *ptr;
 
-   if (*namesize < 1)
-   {
-      SetLastError (ERROR_INSUFFICIENT_BUFFER);
-      return FALSE;
-   }
+    if (!namesize)
+    {
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return FALSE;
+    }
 
-   if (!GetProfileStringA ("windows", "device", "", name, *namesize))
-   {
-      SetLastError (ERROR_FILE_NOT_FOUND);
-      return FALSE;
-   }
+    /* make the buffer big enough for the stuff from the profile/registry,
+     * the content must fit into the local buffer to compute the correct
+     * size even if the extern buffer is to small or not given.
+     * (20 for ,driver,port) */
+    insize = *namesize;
+    len = max(100, (insize + 20));
+    buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR));
 
-   if ((ptr = strchr (name, ',')) == NULL)
-   {
-      SetLastError (ERROR_FILE_NOT_FOUND);
-      return FALSE;
-   }
+    if (!GetProfileStringW(windowsW, deviceW, emptyStringW, buffer, len))
+    {
+        SetLastError (ERROR_FILE_NOT_FOUND);
+        retval = FALSE;
+        goto end;
+    }
+    TRACE("%s\n", debugstr_w(buffer));
+
+    if ((ptr = strchrW(buffer, ',')) == NULL)
+    {
+        SetLastError(ERROR_INVALID_NAME);
+        retval = FALSE;
+        goto end;
+    }
 
-   *ptr = '\0';
-   *namesize = strlen (name) + 1;
-   return TRUE;
+    *ptr = 0;
+    *namesize = strlenW(buffer) + 1;
+    if(!name || (*namesize > insize))
+    {
+        SetLastError(ERROR_INSUFFICIENT_BUFFER);
+        retval = FALSE;
+        goto end;
+    }
+    strcpyW(name, buffer);
+
+end:
+    if(buffer) HeapFree( GetProcessHeap(), 0, buffer);
+    return retval;
 }
 
 
 /******************************************************************************
- *             GetDefaultPrinterW   (WINSPOOL.@)
+ *             GetDefaultPrinterA   (WINSPOOL.@)
  */
-BOOL WINAPI GetDefaultPrinterW(LPWSTR name, LPDWORD namesize)
+BOOL WINAPI GetDefaultPrinterA(LPSTR name, LPDWORD namesize)
 {
-   char *buf;
-   BOOL  ret;
-
-   if (*namesize < 1)
-   {
-      SetLastError (ERROR_INSUFFICIENT_BUFFER);
-      return FALSE;
-   }
-
-   buf = HeapAlloc (GetProcessHeap (), 0, *namesize);
-   ret = GetDefaultPrinterA (buf, namesize);
-   if (ret)
-   {
-       DWORD len = MultiByteToWideChar (CP_ACP, 0, buf, -1, name, *namesize);
-       if (!len)
-       {
-           SetLastError (ERROR_INSUFFICIENT_BUFFER);
-           ret = FALSE;
-       }
-       else *namesize = len;
-   }
-
-   HeapFree (GetProcessHeap (), 0, buf);
-   return ret;
+    BOOL  retval = TRUE;
+    DWORD insize = 0;
+    WCHAR *bufferW = NULL;
+
+    if (!namesize)
+    {
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return FALSE;
+    }
+
+    if(name && *namesize) {
+       insize = *namesize;
+       bufferW = HeapAlloc( GetProcessHeap(), 0, insize * sizeof(WCHAR));
+    }
+
+    if(!GetDefaultPrinterW( bufferW, namesize)) {
+       retval = FALSE;
+       goto end;
+    }
+
+    *namesize = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, name, insize,
+                                    NULL, NULL);
+    if (!*namesize)
+    {
+        *namesize = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
+        retval = FALSE;
+    }
+    TRACE("0x%08lx/0x%08lx:%s\n", *namesize, insize, debugstr_w(bufferW));
+
+end:
+    if(bufferW) HeapFree( GetProcessHeap(), 0, bufferW);
+    return retval;
 }
 
 
index 0aba4183ee5eb633d3925e8a4e8c5f5f3fc9f842..43266f8a2c555cda41fd3e651501b05cdbb05f74 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003 Stefan Leichter
+ * Copyright (C) 2003, 2004 Stefan Leichter
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
 #include "wingdi.h"
 #include "winspool.h"
 
-static void test_printer_directory(ivoid)
+static void test_default_printer(void)
+{
+#define DEFAULT_PRINTER_SIZE 1000
+    BOOL    retval;
+    DWORD   exact = DEFAULT_PRINTER_SIZE;
+    DWORD   size;
+    FARPROC func = NULL;
+    HMODULE lib = NULL;
+    char    buffer[DEFAULT_PRINTER_SIZE];
+
+    lib = GetModuleHandleA("winspool.drv");
+    if (!lib) {
+       ok( 0, "GetModuleHandleA(\"winspool.drv\") failed\n");
+       return;
+    }
+
+    func = GetProcAddress( lib, "GetDefaultPrinterA");
+    if (!func)
+       /* only supported on NT like OSes starting with win2k */
+       return;
+
+    retval = func( buffer, &exact);
+    if (ERROR_FILE_NOT_FOUND == GetLastError()) {
+       ok( 0, "this test requires a default printer to be set\n");
+       return;
+    }
+    if (!retval || !exact || !strlen(buffer)) {
+       ok( 0, "function call GetDefaultPrinterA failed unexpected!\n"
+               "function returned %s\n"
+               "returned buffer size 0x%08lx\n"
+               "returned buffer content %s\n",
+               retval ? "true" : "false", exact, buffer);
+       return;
+    }
+    SetLastError(ERROR_SUCCESS);
+    retval = func( NULL, NULL); 
+    ok( !retval, "function result wrong! False expected\n");
+    ok( ERROR_INVALID_PARAMETER == GetLastError(),
+       "Last error wrong! ERROR_INVALID_PARAMETER expected, got 0x%08lx\n",
+       GetLastError());
+
+    SetLastError(ERROR_SUCCESS);
+    retval = func( buffer, NULL); 
+    ok( !retval, "function result wrong! False expected\n");
+    ok( ERROR_INVALID_PARAMETER == GetLastError(),
+       "Last error wrong! ERROR_INVALID_PARAMETER expected, got 0x%08lx\n",
+       GetLastError());
+
+    SetLastError(ERROR_SUCCESS);
+    size = 0;
+    retval = func( NULL, &size); 
+    ok( !retval, "function result wrong! False expected\n");
+    ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
+       "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
+       GetLastError());
+    ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
+       exact, size);
+
+    SetLastError(ERROR_SUCCESS);
+    size = DEFAULT_PRINTER_SIZE;
+    retval = func( NULL, &size); 
+    ok( !retval, "function result wrong! False expected\n");
+    ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
+       "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
+       GetLastError());
+    ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
+       exact, size);
+
+    size = 0;
+    retval = func( buffer, &size); 
+    ok( !retval, "function result wrong! False expected\n");
+    ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
+       "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
+       GetLastError());
+    ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
+       exact, size);
+
+    size = exact;
+    retval = func( buffer, &size); 
+    ok( retval, "function result wrong! True expected\n");
+    ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
+       exact, size);
+}
+
+static void test_printer_directory(void)
 {   LPBYTE buffer = NULL;
     DWORD  cbBuf, pcbNeeded;
     BOOL   res;
@@ -79,5 +163,6 @@ static void test_printer_directory(ivoid)
 
 START_TEST(info)
 {
+    test_default_printer();
     test_printer_directory();
 }