odbccp32 SQLGetInstalledDrivers etc

Bill Medland billmedland at shaw.ca
Fri Jan 5 18:14:52 CST 2007


Bill Medland (billmedland at shaw.ca)
Implement SQLGetInstalledDrivers
Implement a basic SQLInstallerError
Add test framework and some initial tests

--- /dev/null 2007-01-01 22:33:51.313477112 -0800
+++ wine/dlls/odbccp32/tests/Makefile.in 2007-01-05 11:36:05.000000000 -0800
@@ -0,0 +1,14 @@
+TOPSRCDIR = @top_srcdir@
+TOPOBJDIR = ../../..
+SRCDIR    = @srcdir@
+VPATH     = @srcdir@
+TESTDLL   = odbccp32.dll
+IMPORTS   = odbccp32 user32 kernel32
+EXTRALIBS = 
+
+CTESTS = \
+ error.c 
+
+ at MAKE_TEST_RULES@
+
+ at DEPENDENCIES@  # everything below this line is overwritten by make depend
--- /dev/null 2007-01-01 22:33:51.313477112 -0800
+++ wine/dlls/odbccp32/tests/error.c 2007-01-05 15:31:52.000000000 -0800
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2007 Bill Medland
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include <wine/test.h>
+#include <stdarg.h>
+
+#include "windef.h"
+#include "winbase.h"
+#include "odbcinst.h"
+
+#define DEFINE_EXPECT(func) \
+    static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
+
+#define SET_EXPECT(func) \
+    expect_ ## func = TRUE
+
+#define CHECK_EXPECT(func) \
+    do { \
+        ok(expect_ ##func, "unexpected call " #func "\n"); \
+        expect_ ## func = FALSE; \
+        called_ ## func = TRUE; \
+    }while(0)
+
+#define CHECK_EXPECT2(func) \
+    do { \
+        ok(expect_ ##func, "unexpected call " #func  "\n"); \
+        called_ ## func = TRUE; \
+    }while(0)
+
+#define CHECK_CALLED(func) \
+    do { \
+        ok(called_ ## func, "expected " #func "\n"); \
+        expect_ ## func = called_ ## func = FALSE; \
+    }while(0)
+
+
+
+/* Test that SQLInstallerError handles invalid arguments appropriately */
+static void test_bad_args(void)
+{
+    RETCODE sql_ret;
+    sql_ret = SQLInstallerError(0, NULL, NULL, 0, NULL);
+    ok(sql_ret == SQL_ERROR, "SQLInstallerError(0...) failed with %d instead of SQL_ERROR\n", sql_ret);
+    sql_ret = SQLInstallerError(65535, NULL, NULL, 0, NULL);
+    ok(sql_ret == SQL_NO_DATA, "SQLInstallerError(>8...) failed with %d instead of SQL_NO_DATA\n", sql_ret);
+    /* I am currenly unsure as to whether it should return SQL_NO_DATA or the same as for error 8; I have never been able to generate 8 errors to test it */
+
+    /* Force an error to work with.  This will generate ODBC_ERROR_INVALID_BUFF_LEN */
+    ok(!SQLGetInstalledDrivers(0, 0, 0), "Failed to force an error for testing\n");
+
+    /* Null pointers are acceptable in all obvious places */
+    sql_ret = SQLInstallerError(1, NULL, NULL, 0, NULL);
+    ok(sql_ret == SQL_SUCCESS_WITH_INFO, "SQLInstallerError(null addresses) failed with %d instead of SQL_SUCCESS_WITH_INFO\n", sql_ret);
+}
+
+START_TEST(error)
+{
+    test_bad_args();
+}
Index: wine/dlls/odbccp32/odbccp32.c
===================================================================
RCS file: /home/wine/wine/dlls/odbccp32/odbccp32.c,v
retrieving revision 1.7
diff -u -r1.7 odbccp32.c
--- wine/dlls/odbccp32/odbccp32.c 29 Sep 2006 14:48:43 -0000 1.7
+++ wine/dlls/odbccp32/odbccp32.c 5 Jan 2007 23:38:17 -0000
@@ -3,6 +3,7 @@
  *
  * Copyright 2005 Mike McCormack for CodeWeavers
  * Copyright 2005 Hans Leidekker
+ * Copyright 2007 Bill Medland
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -19,6 +20,10 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
+/*
+ * This could get quite complex if we are not careful about the interaction between the underlying unixODBC/iODBC drivers and any native Windows drivers.
+ */
+#include <assert.h>
 #include <stdarg.h>
 
 #define COBJMACROS
@@ -34,6 +39,26 @@
 
 WINE_DEFAULT_DEBUG_CHANNEL(odbc);
 
+/* Confirmed on Windows 2000; the error collection is not per-thread */
+static int num_errors;
+static int error_code[8];
+static const WCHAR *error_msg[8];
+static const WCHAR odbc_error_general_err[] = {'G','e','n','e','r','a','l',' ','e','r','r','o','r',0};
+static const WCHAR odbc_error_invalid_buff_len[] = {'I','n','v','a','l','i','d',' ','b','u','f','f','e','r',' ','l','e','n','g','t','h',0};
+static const WCHAR odbc_error_component_not_found[] = {'C','o','m','p','o','n','e','n','t',' ','n','o','t',' ','f','o','u','n','d',0};
+static const WCHAR odbc_error_out_of_mem[] = {'O','u','t',' ','o','f',' ','m','e','m','o','r','y',0};
+
+
+static void push_error(int code, LPCWSTR msg)
+{
+    if (num_errors < sizeof error_code/sizeof error_code[0])
+    {
+        error_code[num_errors] = code;
+        error_msg[num_errors] = msg;
+        num_errors++;
+    }
+}
+
 BOOL WINAPI ODBCCPlApplet( LONG i, LONG j, LONG * p1, LONG * p2)
 {
     FIXME( "( %d %d %p %p) : stub!\n", i, j, p1, p2);
@@ -75,6 +100,70 @@
     return ret;
 }
 
+/* Convert the wide string or zero-length-terminated list of wide strings to a narrow string or zero-length-terminated list of narrow strings.
+ * Do not try to emulate windows undocumented excesses (e.g. adding a third \0 to a list)
+ * Arguments
+ *   mode Indicates the sort of string. 
+ *     1 denotes that the buffers contain strings terminated by a single nul character
+ *     2 denotes that the buffers contain zero-length-terminated lists 
+ *   buffer The narrow-character buffer into which to place the result.  This must be a non-null pointer to the first element of a buffer whose length is passed in buffer_length.
+ *   str The wide-character buffer containing the string or list of strings to be converted.  str_length defines how many wide characters in the buffer are to be converted, including all desired terminating nul characters.
+ *   str_length Effective length of str
+ *   buffer_length Length of buffer
+ *   returned_length A pointer to a variable that will receive the number of narrow characters placed into the buffer.  This pointer may be NULL.
+ */
+static BOOL SQLInstall_narrow(int mode, LPSTR buffer, LPCWSTR str, WORD str_length, WORD buffer_length, WORD *returned_length)
+{
+    LPSTR pbuf; /* allows us to allocate a temporary buffer only if needed */
+    int len; /* Length of the converted list */
+    BOOL success = FALSE;
+    assert(mode == 1 || mode == 2);
+    assert(buffer_length);
+    len = WideCharToMultiByte(CP_ACP, 0, str, str_length, 0, 0, NULL, NULL);
+    if (len > 0)
+    {
+        if (len > buffer_length)
+        {
+            pbuf = HeapAlloc(GetProcessHeap(), 0, len);
+        }
+        else
+        {
+            pbuf = buffer;
+        }
+        len = WideCharToMultiByte(CP_ACP, 0, str, str_length, pbuf, len, NULL, NULL);
+        if (len > 0)
+        {
+            if (pbuf != buffer)
+            {
+                if (buffer_length > (mode - 1))
+                {
+                    memcpy (buffer, pbuf, buffer_length-mode);
+                    *(buffer+buffer_length-mode) = '\0';
+                }
+                *(buffer+buffer_length-1) = '\0';
+            }
+            if (returned_length)
+            {
+                *returned_length = pbuf == buffer ? len : buffer_length;
+            }
+            success = TRUE;
+        }
+        else
+        {
+            ERR("transfering wide to narrow\n");
+        }
+        if (pbuf != buffer)
+        {
+            HeapFree(GetProcessHeap(), 0, pbuf);
+        }
+    }
+    else
+    {
+        ERR("measuring wide to narrow\n");
+    }
+    return success;
+}
+
 BOOL WINAPI SQLConfigDataSourceW(HWND hwndParent, WORD fRequest,
                LPCWSTR lpszDriver, LPCWSTR lpszAttributes)
 {
@@ -150,20 +239,88 @@
     return FALSE;
 }
 
+/* This is implemented sensibly rather than according to exact conformance to Microsoft's buggy implementations
+ * e.g. The Microsoft one occasionally actually adds a third nul character.
+ */
 BOOL WINAPI SQLGetInstalledDriversW(LPWSTR lpszBuf, WORD cbBufMax,
                WORD *pcbBufOut)
 {
-    FIXME("\n");
-    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-    return FALSE;
+    HKEY hDrivers;
+
+    LONG reg_ret;
+    BOOL success = FALSE;
+
+    if (!lpszBuf || cbBufMax == 0)
+    {
+        push_error(ODBC_ERROR_INVALID_BUFF_LEN, odbc_error_invalid_buff_len);
+    }
+    else if ((reg_ret = RegOpenKeyExA (HKEY_LOCAL_MACHINE,
+            "Software\\ODBC\\ODBCINST.INI\\ODBC Drivers", 0, KEY_READ /* Maybe overkill */,
+            &hDrivers)) == ERROR_SUCCESS)
+    {
+        DWORD index = 0;
+        cbBufMax--;
+        success = TRUE;
+        while (cbBufMax > 0)
+        {
+            DWORD size_name;
+            size_name = cbBufMax;
+            if ((reg_ret = RegEnumValueW(hDrivers, index, lpszBuf, &size_name, NULL, NULL, NULL, NULL)) == ERROR_SUCCESS)
+            {
+                index++;
+                assert (size_name < cbBufMax && *(lpszBuf + size_name) == 0); 
+                size_name++;
+                cbBufMax-= size_name;
+                lpszBuf+=size_name;
+            }
+            else
+            {
+                if (reg_ret != ERROR_NO_MORE_ITEMS)
+                {
+                    success = FALSE;
+                    push_error(ODBC_ERROR_GENERAL_ERR, odbc_error_general_err);
+                }
+                break;
+            }
+        }
+        *lpszBuf = 0;
+        if ((reg_ret = RegCloseKey (hDrivers)) != ERROR_SUCCESS)
+            TRACE ("Error %d closing ODBC Drivers key\n", reg_ret);
+    }
+    else
+    {
+        push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
+    }
+    return success;
 }
 
 BOOL WINAPI SQLGetInstalledDrivers(LPSTR lpszBuf, WORD cbBufMax,
                WORD *pcbBufOut)
 {
-    FIXME("\n");
-    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-    return FALSE;
+    BOOL ret;
+    int size_wbuf = cbBufMax;
+    LPWSTR wbuf;
+    WORD size_used;
+    wbuf = HeapAlloc(GetProcessHeap(), 0, size_wbuf*sizeof(WCHAR));
+    if (wbuf)
+    {
+        ret = SQLGetInstalledDriversW(wbuf, size_wbuf, &size_used);
+        if (ret)
+        {
+            if (!(ret = SQLInstall_narrow(2, lpszBuf, wbuf, size_used, cbBufMax, pcbBufOut)))
+            {
+                push_error(ODBC_ERROR_GENERAL_ERR, odbc_error_general_err);
+            }
+        }
+        HeapFree(GetProcessHeap(), 0, wbuf);
+        /* ignore failure; we have achieved the aim */
+    }
+    else
+    {
+        push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
+        ret = FALSE;
+    }
+    return ret;
 }
 
 int WINAPI SQLGetPrivateProfileStringW(LPCWSTR lpszSection, LPCWSTR lpszEntry,
@@ -381,28 +538,72 @@
     TRACE("%d %p %p %d %p\n", iError, pfErrorCode, lpszErrorMsg,
           cbErrorMsgMax, pcbErrorMsg);
 
-    if (pcbErrorMsg)
-        *pcbErrorMsg = 0;
+    if (iError == 0)
+        return SQL_ERROR;
+
+    if (iError <= num_errors)
+    {
+        BOOL truncated = FALSE;
+        WORD len;
+        LPCWSTR msg;
+        iError--;
+        if (pfErrorCode)
+            *pfErrorCode = error_code[iError];
+        msg = error_msg[iError];
+        len = msg ? lstrlenW(msg) : 0;
+        if (pcbErrorMsg)
+            *pcbErrorMsg = len;
+        len++;
+        if (cbErrorMsgMax < len)
+        {
+            len = cbErrorMsgMax;
+            truncated = TRUE;
+        }
+        if (lpszErrorMsg && len)
+        {
+            if (msg)
+            {
+                memcpy (lpszErrorMsg, msg, len * sizeof(WCHAR));
+            }
+            else
+            {
+                assert(len==1);
+                *lpszErrorMsg = 0;
+            }
+        }
+        else
+        {
+            /* Yes.  If you pass a null pointer and a large length it is not an error! */
+            truncated = TRUE;
+        }
 
-    if (lpszErrorMsg && cbErrorMsgMax > 0)
-        *lpszErrorMsg = '\0';
+        return truncated ? SQL_SUCCESS_WITH_INFO : SQL_SUCCESS;
+    }
 
+    /* At least on my system, the buffers are not altered in this case.  However that is a little too dangerous a test for just now */
     return SQL_NO_DATA;
 }
 
 SQLRETURN WINAPI SQLInstallerError(WORD iError, DWORD *pfErrorCode,
                LPSTR lpszErrorMsg, WORD cbErrorMsgMax, WORD *pcbErrorMsg)
 {
+    SQLRETURN ret;
+    LPWSTR wbuf;
+    WORD cbwbuf;
     TRACE("%d %p %p %d %p\n", iError, pfErrorCode, lpszErrorMsg,
           cbErrorMsgMax, pcbErrorMsg);
 
-    if (pcbErrorMsg)
-        *pcbErrorMsg = 0;
-
-    if (lpszErrorMsg && cbErrorMsgMax > 0)
-        *lpszErrorMsg = '\0';
-
-    return SQL_NO_DATA;
+    wbuf = (lpszErrorMsg && cbErrorMsgMax) ? HeapAlloc(GetProcessHeap(), 0, cbErrorMsgMax*sizeof(WCHAR)) : 0;
+    ret = SQLInstallerErrorW(iError, pfErrorCode, wbuf, cbErrorMsgMax, &cbwbuf);
+    if (wbuf)
+    {
+        WORD cbBuf;
+        SQLInstall_narrow(1, lpszErrorMsg, wbuf, cbwbuf+1, cbErrorMsgMax, &cbBuf);
+        HeapFree(GetProcessHeap(), 0, wbuf);
+        if (pcbErrorMsg)
+            *pcbErrorMsg = cbBuf-1;
+    }
+    return ret;
 }
 
 BOOL WINAPI SQLInstallTranslatorExW(LPCWSTR lpszTranslator, LPCWSTR lpszPathIn,



More information about the wine-patches mailing list