server: Move socket async activation to sock_poll_event.
[wine] / dlls / odbccp32 / odbccp32.c
1 /*
2  * Implementation of the ODBC driver installer
3  *
4  * Copyright 2005 Mike McCormack for CodeWeavers
5  * Copyright 2005 Hans Leidekker
6  * Copyright 2007 Bill Medland
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include <assert.h>
24 #include <stdarg.h>
25
26 #define COBJMACROS
27 #define NONAMELESSUNION
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winreg.h"
32 #include "winnls.h"
33 #include "wine/debug.h"
34
35 #include "odbcinst.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(odbc);
38
39 /* Registry key names */
40 static const WCHAR drivers_key[] = {'S','o','f','t','w','a','r','e','\\','O','D','B','C','\\','O','D','B','C','I','N','S','T','.','I','N','I','\\','O','D','B','C',' ','D','r','i','v','e','r','s',0};
41
42 /* This config mode is known to be process-wide.
43  * MSDN documentation suggests that the value is hidden somewhere in the registry but I haven't found it yet.
44  * Although both the registry and the ODBC.ini files appear to be maintained together they are not maintained automatically through the registry's IniFileMapping.
45  */
46 static UWORD config_mode = ODBC_BOTH_DSN;
47
48 /* MSDN documentation suggests that the error subsystem handles errors 1 to 8
49  * only and experimentation (Windows 2000) shows that the errors are process-
50  * wide so go for the simple solution; static arrays.
51  */
52 static int num_errors;
53 static int error_code[8];
54 static const WCHAR *error_msg[8];
55 static const WCHAR odbc_error_general_err[] = {'G','e','n','e','r','a','l',' ','e','r','r','o','r',0};
56 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};
57 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};
58 static const WCHAR odbc_error_out_of_mem[] = {'O','u','t',' ','o','f',' ','m','e','m','o','r','y',0};
59 static const WCHAR odbc_error_invalid_param_sequence[] = {'I','n','v','a','l','i','d',' ','p','a','r','a','m','e','t','e','r',' ','s','e','q','u','e','n','c','e',0};
60
61 /* Push an error onto the error stack, taking care of ranges etc. */
62 static void push_error(int code, LPCWSTR msg)
63 {
64     if (num_errors < sizeof error_code/sizeof error_code[0])
65     {
66         error_code[num_errors] = code;
67         error_msg[num_errors] = msg;
68         num_errors++;
69     }
70 }
71
72 /* Clear the error stack */
73 static void clear_errors(void)
74 {
75     num_errors = 0;
76 }
77
78 BOOL WINAPI ODBCCPlApplet( LONG i, LONG j, LONG * p1, LONG * p2)
79 {
80     clear_errors();
81     FIXME( "( %d %d %p %p) : stub!\n", i, j, p1, p2);
82     return FALSE;
83 }
84
85 static LPWSTR SQLInstall_strdup_multi(LPCSTR str)
86 {
87     LPCSTR p;
88     LPWSTR ret = NULL;
89     DWORD len;
90
91     if (!str)
92         return ret;
93
94     for (p = str; *p; p += lstrlenA(p) + 1)
95         ;
96
97     len = MultiByteToWideChar(CP_ACP, 0, str, p - str, NULL, 0 );
98     ret = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
99     MultiByteToWideChar(CP_ACP, 0, str, p - str, ret, len );
100     ret[len] = 0;
101
102     return ret;
103 }
104
105 static LPWSTR SQLInstall_strdup(LPCSTR str)
106 {
107     DWORD len;
108     LPWSTR ret = NULL;
109
110     if (!str)
111         return ret;
112
113     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0 );
114     ret = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
115     MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len );
116
117     return ret;
118 }
119
120 /* Convert the wide string or zero-length-terminated list of wide strings to a
121  * narrow string or zero-length-terminated list of narrow strings.
122  * Do not try to emulate windows undocumented excesses (e.g. adding a third \0
123  * to a list)
124  * Arguments
125  *   mode Indicates the sort of string.
126  *     1 denotes that the buffers contain strings terminated by a single nul
127  *       character
128  *     2 denotes that the buffers contain zero-length-terminated lists
129  *       (frequently erroneously referred to as double-null-terminated)
130  *   buffer The narrow-character buffer into which to place the result.  This
131  *          must be a non-null pointer to the first element of a buffer whose
132  *          length is passed in buffer_length.
133  *   str The wide-character buffer containing the string or list of strings to
134  *       be converted.  str_length defines how many wide characters in the
135  *       buffer are to be converted, including all desired terminating nul
136  *       characters.
137  *   str_length Effective length of str
138  *   buffer_length Length of buffer
139  *   returned_length A pointer to a variable that will receive the number of
140  *                   narrow characters placed into the buffer.  This pointer
141  *                   may be NULL.
142  */
143 static BOOL SQLInstall_narrow(int mode, LPSTR buffer, LPCWSTR str, WORD str_length, WORD buffer_length, WORD *returned_length)
144 {
145     LPSTR pbuf; /* allows us to allocate a temporary buffer only if needed */
146     int len; /* Length of the converted list */
147     BOOL success = FALSE;
148     assert(mode == 1 || mode == 2);
149     assert(buffer_length);
150     len = WideCharToMultiByte(CP_ACP, 0, str, str_length, 0, 0, NULL, NULL);
151     if (len > 0)
152     {
153         if (len > buffer_length)
154         {
155             pbuf = HeapAlloc(GetProcessHeap(), 0, len);
156         }
157         else
158         {
159             pbuf = buffer;
160         }
161         len = WideCharToMultiByte(CP_ACP, 0, str, str_length, pbuf, len, NULL, NULL);
162         if (len > 0)
163         {
164             if (pbuf != buffer)
165             {
166                 if (buffer_length > (mode - 1))
167                 {
168                     memcpy (buffer, pbuf, buffer_length-mode);
169                     *(buffer+buffer_length-mode) = '\0';
170                 }
171                 *(buffer+buffer_length-1) = '\0';
172             }
173             if (returned_length)
174             {
175                 *returned_length = pbuf == buffer ? len : buffer_length;
176             }
177             success = TRUE;
178         }
179         else
180         {
181             ERR("transferring wide to narrow\n");
182         }
183         if (pbuf != buffer)
184         {
185             HeapFree(GetProcessHeap(), 0, pbuf);
186         }
187     }
188     else
189     {
190         ERR("measuring wide to narrow\n");
191     }
192     return success;
193 }
194
195 BOOL WINAPI SQLConfigDataSourceW(HWND hwndParent, WORD fRequest,
196                LPCWSTR lpszDriver, LPCWSTR lpszAttributes)
197 {
198     LPCWSTR p;
199
200     clear_errors();
201     FIXME("%p %d %s %s\n", hwndParent, fRequest, debugstr_w(lpszDriver),
202           debugstr_w(lpszAttributes));
203
204     for (p = lpszAttributes; *p; p += lstrlenW(p) + 1)
205         FIXME("%s\n", debugstr_w(p));
206
207     return TRUE;
208 }
209
210 BOOL WINAPI SQLConfigDataSource(HWND hwndParent, WORD fRequest,
211                LPCSTR lpszDriver, LPCSTR lpszAttributes)
212 {
213     FIXME("%p %d %s %s\n", hwndParent, fRequest, debugstr_a(lpszDriver),
214           debugstr_a(lpszAttributes));
215     clear_errors();
216     return TRUE;
217 }
218
219 BOOL WINAPI SQLConfigDriverW(HWND hwndParent, WORD fRequest, LPCWSTR lpszDriver,
220                LPCWSTR lpszArgs, LPWSTR lpszMsg, WORD cbMsgMax, WORD *pcbMsgOut)
221 {
222     clear_errors();
223     FIXME("(%p %d %s %s %p %d %p)\n", hwndParent, fRequest, debugstr_w(lpszDriver),
224           debugstr_w(lpszArgs), lpszMsg, cbMsgMax, pcbMsgOut);
225     return TRUE;
226 }
227
228 BOOL WINAPI SQLConfigDriver(HWND hwndParent, WORD fRequest, LPCSTR lpszDriver,
229                LPCSTR lpszArgs, LPSTR lpszMsg, WORD cbMsgMax, WORD *pcbMsgOut)
230 {
231     clear_errors();
232     FIXME("(%p %d %s %s %p %d %p)\n", hwndParent, fRequest, debugstr_a(lpszDriver),
233           debugstr_a(lpszArgs), lpszMsg, cbMsgMax, pcbMsgOut);
234     return TRUE;
235 }
236
237 BOOL WINAPI SQLCreateDataSourceW(HWND hwnd, LPCWSTR lpszDS)
238 {
239     clear_errors();
240     FIXME("\n");
241     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
242     return FALSE;
243 }
244
245 BOOL WINAPI SQLCreateDataSource(HWND hwnd, LPCSTR lpszDS)
246 {
247     clear_errors();
248     FIXME("\n");
249     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
250     return FALSE;
251 }
252
253 BOOL WINAPI SQLGetAvailableDriversW(LPCWSTR lpszInfFile, LPWSTR lpszBuf,
254                WORD cbBufMax, WORD *pcbBufOut)
255 {
256     clear_errors();
257     FIXME("\n");
258     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
259     return FALSE;
260 }
261
262 BOOL WINAPI SQLGetAvailableDrivers(LPCSTR lpszInfFile, LPSTR lpszBuf,
263                WORD cbBufMax, WORD *pcbBufOut)
264 {
265     clear_errors();
266     FIXME("\n");
267     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
268     return FALSE;
269 }
270
271 BOOL WINAPI SQLGetConfigMode(UWORD *pwConfigMode)
272 {
273     clear_errors();
274     if (pwConfigMode)
275         *pwConfigMode = config_mode;
276     return TRUE;
277 }
278
279 /* This is implemented sensibly rather than according to exact conformance to Microsoft's buggy implementations
280  * e.g. The Microsoft one occasionally actually adds a third nul character (possibly beyond the buffer).
281  * e.g. If the key has no drivers then version 3.525.1117.0 does not modify the buffer at all, not even a nul character.
282  */
283 BOOL WINAPI SQLGetInstalledDriversW(LPWSTR lpszBuf, WORD cbBufMax,
284                WORD *pcbBufOut)
285 {
286     HKEY hDrivers; /* Registry handle to the Drivers key */
287     LONG reg_ret; /* Return code from registry functions */
288     BOOL success = FALSE; /* The value we will return */
289
290     clear_errors();
291     if (!lpszBuf || cbBufMax == 0)
292     {
293         push_error(ODBC_ERROR_INVALID_BUFF_LEN, odbc_error_invalid_buff_len);
294     }
295     else if ((reg_ret = RegOpenKeyExW (HKEY_LOCAL_MACHINE /* The drivers does not depend on the config mode */,
296             drivers_key, 0, KEY_READ /* Maybe overkill */,
297             &hDrivers)) == ERROR_SUCCESS)
298     {
299         DWORD index = 0;
300         cbBufMax--;
301         success = TRUE;
302         while (cbBufMax > 0)
303         {
304             DWORD size_name;
305             size_name = cbBufMax;
306             if ((reg_ret = RegEnumValueW(hDrivers, index, lpszBuf, &size_name, NULL, NULL, NULL, NULL)) == ERROR_SUCCESS)
307             {
308                 index++;
309                 assert (size_name < cbBufMax && *(lpszBuf + size_name) == 0);
310                 size_name++;
311                 cbBufMax-= size_name;
312                 lpszBuf+=size_name;
313             }
314             else
315             {
316                 if (reg_ret != ERROR_NO_MORE_ITEMS)
317                 {
318                     success = FALSE;
319                     push_error(ODBC_ERROR_GENERAL_ERR, odbc_error_general_err);
320                 }
321                 break;
322             }
323         }
324         *lpszBuf = 0;
325         if ((reg_ret = RegCloseKey (hDrivers)) != ERROR_SUCCESS)
326             TRACE ("Error %d closing ODBC Drivers key\n", reg_ret);
327     }
328     else
329     {
330         /* MSDN states that it returns failure with COMPONENT_NOT_FOUND in this case.
331          * Version 3.525.1117.0 (Windows 2000) does not; it actually returns success.
332          * I doubt if it will actually be an issue.
333          */
334         push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
335     }
336     return success;
337 }
338
339 BOOL WINAPI SQLGetInstalledDrivers(LPSTR lpszBuf, WORD cbBufMax,
340                WORD *pcbBufOut)
341 {
342     BOOL ret;
343     int size_wbuf = cbBufMax;
344     LPWSTR wbuf;
345     WORD size_used;
346     wbuf = HeapAlloc(GetProcessHeap(), 0, size_wbuf*sizeof(WCHAR));
347     if (wbuf)
348     {
349         ret = SQLGetInstalledDriversW(wbuf, size_wbuf, &size_used);
350         if (ret)
351         {
352             if (!(ret = SQLInstall_narrow(2, lpszBuf, wbuf, size_used, cbBufMax, pcbBufOut)))
353             {
354                 push_error(ODBC_ERROR_GENERAL_ERR, odbc_error_general_err);
355             }
356         }
357         HeapFree(GetProcessHeap(), 0, wbuf);
358         /* ignore failure; we have achieved the aim */
359     }
360     else
361     {
362         push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
363         ret = FALSE;
364     }
365     return ret;
366 }
367
368 int WINAPI SQLGetPrivateProfileStringW(LPCWSTR lpszSection, LPCWSTR lpszEntry,
369                LPCWSTR lpszDefault, LPCWSTR RetBuffer, int cbRetBuffer,
370                LPCWSTR lpszFilename)
371 {
372     clear_errors();
373     FIXME("\n");
374     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
375     return FALSE;
376 }
377
378 int WINAPI SQLGetPrivateProfileString(LPCSTR lpszSection, LPCSTR lpszEntry,
379                LPCSTR lpszDefault, LPCSTR RetBuffer, int cbRetBuffer,
380                LPCSTR lpszFilename)
381 {
382     clear_errors();
383     FIXME("\n");
384     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
385     return FALSE;
386 }
387
388 BOOL WINAPI SQLGetTranslatorW(HWND hwndParent, LPWSTR lpszName, WORD cbNameMax,
389                WORD *pcbNameOut, LPWSTR lpszPath, WORD cbPathMax,
390                WORD *pcbPathOut, DWORD *pvOption)
391 {
392     clear_errors();
393     FIXME("\n");
394     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
395     return FALSE;
396 }
397
398 BOOL WINAPI SQLGetTranslator(HWND hwndParent, LPSTR lpszName, WORD cbNameMax,
399                WORD *pcbNameOut, LPSTR lpszPath, WORD cbPathMax,
400                WORD *pcbPathOut, DWORD *pvOption)
401 {
402     clear_errors();
403     FIXME("\n");
404     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
405     return FALSE;
406 }
407
408 BOOL WINAPI SQLInstallDriverW(LPCWSTR lpszInfFile, LPCWSTR lpszDriver,
409                LPWSTR lpszPath, WORD cbPathMax, WORD * pcbPathOut)
410 {
411     DWORD usage;
412
413     clear_errors();
414     TRACE("%s %s %p %d %p\n", debugstr_w(lpszInfFile),
415           debugstr_w(lpszDriver), lpszPath, cbPathMax, pcbPathOut);
416
417     if (lpszInfFile)
418         return FALSE;
419
420     return SQLInstallDriverExW(lpszDriver, NULL, lpszPath, cbPathMax,
421                                pcbPathOut, ODBC_INSTALL_COMPLETE, &usage);
422 }
423
424 BOOL WINAPI SQLInstallDriver(LPCSTR lpszInfFile, LPCSTR lpszDriver,
425                LPSTR lpszPath, WORD cbPathMax, WORD * pcbPathOut)
426 {
427     DWORD usage;
428
429     clear_errors();
430     TRACE("%s %s %p %d %p\n", debugstr_a(lpszInfFile),
431           debugstr_a(lpszDriver), lpszPath, cbPathMax, pcbPathOut);
432
433     if (lpszInfFile)
434         return FALSE;
435    
436     return SQLInstallDriverEx(lpszDriver, NULL, lpszPath, cbPathMax,
437                               pcbPathOut, ODBC_INSTALL_COMPLETE, &usage);
438 }
439
440 BOOL WINAPI SQLInstallDriverExW(LPCWSTR lpszDriver, LPCWSTR lpszPathIn,
441                LPWSTR lpszPathOut, WORD cbPathOutMax, WORD *pcbPathOut,
442                WORD fRequest, LPDWORD lpdwUsageCount)
443 {
444     UINT len;
445     LPCWSTR p;
446     WCHAR path[MAX_PATH];
447
448     clear_errors();
449     TRACE("%s %s %p %d %p %d %p\n", debugstr_w(lpszDriver),
450           debugstr_w(lpszPathIn), lpszPathOut, cbPathOutMax, pcbPathOut,
451           fRequest, lpdwUsageCount);
452
453     for (p = lpszDriver; *p; p += lstrlenW(p) + 1)
454         TRACE("%s\n", debugstr_w(p));
455
456     len = GetSystemDirectoryW(path, MAX_PATH);
457
458     if (pcbPathOut)
459         *pcbPathOut = len;
460
461     len = GetSystemDirectoryW(path, MAX_PATH);
462
463     if (lpszPathOut && cbPathOutMax > len)
464     {
465         lstrcpyW(lpszPathOut, path);
466         return TRUE;
467     }
468     return FALSE;
469 }
470
471 BOOL WINAPI SQLInstallDriverEx(LPCSTR lpszDriver, LPCSTR lpszPathIn,
472                LPSTR lpszPathOut, WORD cbPathOutMax, WORD *pcbPathOut,
473                WORD fRequest, LPDWORD lpdwUsageCount)
474 {
475     LPCSTR p;
476     LPWSTR driver, pathin;
477     WCHAR pathout[MAX_PATH];
478     BOOL ret;
479     WORD cbOut = 0;
480
481     clear_errors();
482     TRACE("%s %s %p %d %p %d %p\n", debugstr_a(lpszDriver),
483           debugstr_a(lpszPathIn), lpszPathOut, cbPathOutMax, pcbPathOut,
484           fRequest, lpdwUsageCount);
485
486     for (p = lpszDriver; *p; p += lstrlenA(p) + 1)
487         TRACE("%s\n", debugstr_a(p));
488
489     driver = SQLInstall_strdup_multi(lpszDriver);
490     pathin = SQLInstall_strdup(lpszPathIn);
491
492     ret = SQLInstallDriverExW(driver, pathin, pathout, MAX_PATH, &cbOut,
493                               fRequest, lpdwUsageCount);
494     if (ret)
495     {
496         int len =  WideCharToMultiByte(CP_ACP, 0, pathout, -1, lpszPathOut,
497                                        0, NULL, NULL);
498         if (len)
499         {
500             if (pcbPathOut)
501                 *pcbPathOut = len - 1;
502
503             if (!lpszPathOut || cbPathOutMax < len)
504             {
505                 ret = FALSE;
506                 goto out;
507             }
508             len =  WideCharToMultiByte(CP_ACP, 0, pathout, -1, lpszPathOut,
509                                        cbPathOutMax, NULL, NULL);
510         }
511     }
512
513 out:
514     HeapFree(GetProcessHeap(), 0, driver);
515     HeapFree(GetProcessHeap(), 0, pathin);
516     return ret;
517 }
518
519 BOOL WINAPI SQLInstallDriverManagerW(LPWSTR lpszPath, WORD cbPathMax,
520                WORD *pcbPathOut)
521 {
522     UINT len;
523     WCHAR path[MAX_PATH];
524
525     clear_errors();
526     TRACE("(%p %d %p)\n", lpszPath, cbPathMax, pcbPathOut);
527
528     len = GetSystemDirectoryW(path, MAX_PATH);
529
530     if (pcbPathOut)
531         *pcbPathOut = len;
532
533     if (lpszPath && cbPathMax > len)
534     {
535         lstrcpyW(lpszPath, path);
536         return TRUE;
537     }
538     return FALSE;
539 }
540
541 BOOL WINAPI SQLInstallDriverManager(LPSTR lpszPath, WORD cbPathMax,
542                WORD *pcbPathOut)
543 {
544     BOOL ret;
545     WORD len, cbOut = 0;
546     WCHAR path[MAX_PATH];
547
548     clear_errors();
549     TRACE("(%p %d %p)\n", lpszPath, cbPathMax, pcbPathOut);
550
551     ret = SQLInstallDriverManagerW(path, MAX_PATH, &cbOut);
552     if (ret)
553     {
554         len =  WideCharToMultiByte(CP_ACP, 0, path, -1, lpszPath, 0,
555                                    NULL, NULL);
556         if (len)
557         {
558             if (pcbPathOut)
559                 *pcbPathOut = len - 1;
560
561             if (!lpszPath || cbPathMax < len)
562                 return FALSE;
563
564             len =  WideCharToMultiByte(CP_ACP, 0, path, -1, lpszPath,
565                                        cbPathMax, NULL, NULL);
566         }
567     }
568     return ret;
569 }
570
571 BOOL WINAPI SQLInstallODBCW(HWND hwndParent, LPCWSTR lpszInfFile,
572                LPCWSTR lpszSrcPath, LPCWSTR lpszDrivers)
573 {
574     clear_errors();
575     FIXME("\n");
576     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
577     return FALSE;
578 }
579
580 BOOL WINAPI SQLInstallODBC(HWND hwndParent, LPCSTR lpszInfFile,
581                LPCSTR lpszSrcPath, LPCSTR lpszDrivers)
582 {
583     clear_errors();
584     FIXME("\n");
585     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
586     return FALSE;
587 }
588
589 SQLRETURN WINAPI SQLInstallerErrorW(WORD iError, DWORD *pfErrorCode,
590                LPWSTR lpszErrorMsg, WORD cbErrorMsgMax, WORD *pcbErrorMsg)
591 {
592     TRACE("%d %p %p %d %p\n", iError, pfErrorCode, lpszErrorMsg,
593           cbErrorMsgMax, pcbErrorMsg);
594
595     if (iError == 0)
596     {
597         return SQL_ERROR;
598     }
599     else if (iError <= num_errors)
600     {
601         BOOL truncated = FALSE;
602         WORD len;
603         LPCWSTR msg;
604         iError--;
605         if (pfErrorCode)
606             *pfErrorCode = error_code[iError];
607         msg = error_msg[iError];
608         len = msg ? lstrlenW(msg) : 0;
609         if (pcbErrorMsg)
610             *pcbErrorMsg = len;
611         len++;
612         if (cbErrorMsgMax < len)
613         {
614             len = cbErrorMsgMax;
615             truncated = TRUE;
616         }
617         if (lpszErrorMsg && len)
618         {
619             if (msg)
620             {
621                 memcpy (lpszErrorMsg, msg, len * sizeof(WCHAR));
622             }
623             else
624             {
625                 assert(len==1);
626                 *lpszErrorMsg = 0;
627             }
628         }
629         else
630         {
631             /* Yes.  If you pass a null pointer and a large length it is not an error! */
632             truncated = TRUE;
633         }
634
635         return truncated ? SQL_SUCCESS_WITH_INFO : SQL_SUCCESS;
636     }
637
638     /* At least on Windows 2000 , the buffers are not altered in this case.  However that is a little too dangerous a test for just now */
639     if (pcbErrorMsg)
640         *pcbErrorMsg = 0;
641
642     if (lpszErrorMsg && cbErrorMsgMax > 0)
643         *lpszErrorMsg = '\0';
644
645     return SQL_NO_DATA;
646 }
647
648 SQLRETURN WINAPI SQLInstallerError(WORD iError, DWORD *pfErrorCode,
649                LPSTR lpszErrorMsg, WORD cbErrorMsgMax, WORD *pcbErrorMsg)
650 {
651     SQLRETURN ret;
652     LPWSTR wbuf;
653     WORD cbwbuf;
654     TRACE("%d %p %p %d %p\n", iError, pfErrorCode, lpszErrorMsg,
655           cbErrorMsgMax, pcbErrorMsg);
656
657     wbuf = 0;
658     if (lpszErrorMsg && cbErrorMsgMax)
659     {
660         wbuf = HeapAlloc(GetProcessHeap(), 0, cbErrorMsgMax*sizeof(WCHAR));
661         if (!wbuf)
662             return SQL_ERROR;
663     }
664     ret = SQLInstallerErrorW(iError, pfErrorCode, wbuf, cbErrorMsgMax, &cbwbuf);
665     if (wbuf)
666     {
667         WORD cbBuf = 0;
668         SQLInstall_narrow(1, lpszErrorMsg, wbuf, cbwbuf+1, cbErrorMsgMax, &cbBuf);
669         HeapFree(GetProcessHeap(), 0, wbuf);
670         if (pcbErrorMsg)
671             *pcbErrorMsg = cbBuf-1;
672     }
673     return ret;
674 }
675
676 BOOL WINAPI SQLInstallTranslatorExW(LPCWSTR lpszTranslator, LPCWSTR lpszPathIn,
677                LPWSTR lpszPathOut, WORD cbPathOutMax, WORD *pcbPathOut,
678                WORD fRequest, LPDWORD lpdwUsageCount)
679 {
680     UINT len;
681     LPCWSTR p;
682     WCHAR path[MAX_PATH];
683
684     clear_errors();
685     TRACE("%s %s %p %d %p %d %p\n", debugstr_w(lpszTranslator),
686           debugstr_w(lpszPathIn), lpszPathOut, cbPathOutMax, pcbPathOut,
687           fRequest, lpdwUsageCount);
688
689     for (p = lpszTranslator; *p; p += lstrlenW(p) + 1)
690         TRACE("%s\n", debugstr_w(p));
691
692     len = GetSystemDirectoryW(path, MAX_PATH);
693
694     if (pcbPathOut)
695         *pcbPathOut = len;
696
697     if (lpszPathOut && cbPathOutMax > len)
698     {
699         lstrcpyW(lpszPathOut, path);
700         return TRUE;
701     }
702     return FALSE;
703 }
704
705 BOOL WINAPI SQLInstallTranslatorEx(LPCSTR lpszTranslator, LPCSTR lpszPathIn,
706                LPSTR lpszPathOut, WORD cbPathOutMax, WORD *pcbPathOut,
707                WORD fRequest, LPDWORD lpdwUsageCount)
708 {
709     LPCSTR p;
710     LPWSTR translator, pathin;
711     WCHAR pathout[MAX_PATH];
712     BOOL ret;
713     WORD cbOut = 0;
714
715     clear_errors();
716     TRACE("%s %s %p %d %p %d %p\n", debugstr_a(lpszTranslator),
717           debugstr_a(lpszPathIn), lpszPathOut, cbPathOutMax, pcbPathOut,
718           fRequest, lpdwUsageCount);
719
720     for (p = lpszTranslator; *p; p += lstrlenA(p) + 1)
721         TRACE("%s\n", debugstr_a(p));
722
723     translator = SQLInstall_strdup_multi(lpszTranslator);
724     pathin = SQLInstall_strdup(lpszPathIn);
725
726     ret = SQLInstallTranslatorExW(translator, pathin, pathout, MAX_PATH,
727                                   &cbOut, fRequest, lpdwUsageCount);
728     if (ret)
729     {
730         int len =  WideCharToMultiByte(CP_ACP, 0, pathout, -1, lpszPathOut,
731                                        0, NULL, NULL);
732         if (len)
733         {
734             if (pcbPathOut)
735                 *pcbPathOut = len - 1;
736
737             if (!lpszPathOut || cbPathOutMax < len)
738             {
739                 ret = FALSE;
740                 goto out;
741             }
742             len =  WideCharToMultiByte(CP_ACP, 0, pathout, -1, lpszPathOut,
743                                        cbPathOutMax, NULL, NULL);
744         }
745     }
746
747 out:
748     HeapFree(GetProcessHeap(), 0, translator);
749     HeapFree(GetProcessHeap(), 0, pathin);
750     return ret;
751 }
752
753 BOOL WINAPI SQLInstallTranslator(LPCSTR lpszInfFile, LPCSTR lpszTranslator,
754                LPCSTR lpszPathIn, LPSTR lpszPathOut, WORD cbPathOutMax,
755                WORD *pcbPathOut, WORD fRequest, LPDWORD lpdwUsageCount)
756 {
757     clear_errors();
758     TRACE("%s %s %s %p %d %p %d %p\n", debugstr_a(lpszInfFile),
759           debugstr_a(lpszTranslator), debugstr_a(lpszPathIn), lpszPathOut,
760           cbPathOutMax, pcbPathOut, fRequest, lpdwUsageCount);
761
762     if (lpszInfFile)
763         return FALSE;
764
765     return SQLInstallTranslatorEx(lpszTranslator, lpszPathIn, lpszPathOut,
766                        cbPathOutMax, pcbPathOut, fRequest, lpdwUsageCount);
767 }
768
769 BOOL WINAPI SQLInstallTranslatorW(LPCWSTR lpszInfFile, LPCWSTR lpszTranslator,
770               LPCWSTR lpszPathIn, LPWSTR lpszPathOut, WORD cbPathOutMax,
771               WORD *pcbPathOut, WORD fRequest, LPDWORD lpdwUsageCount)
772 {
773     clear_errors();
774     TRACE("%s %s %s %p %d %p %d %p\n", debugstr_w(lpszInfFile),
775           debugstr_w(lpszTranslator), debugstr_w(lpszPathIn), lpszPathOut,
776           cbPathOutMax, pcbPathOut, fRequest, lpdwUsageCount);
777
778     if (lpszInfFile)
779         return FALSE;
780
781     return SQLInstallTranslatorExW(lpszTranslator, lpszPathIn, lpszPathOut,
782                         cbPathOutMax, pcbPathOut, fRequest, lpdwUsageCount);
783 }
784
785 BOOL WINAPI SQLManageDataSources(HWND hwnd)
786 {
787     clear_errors();
788     FIXME("\n");
789     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
790     return FALSE;
791 }
792
793 SQLRETURN WINAPI SQLPostInstallerErrorW(DWORD fErrorCode, LPCWSTR szErrorMsg)
794 {
795     FIXME("\n");
796     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
797     return FALSE;
798 }
799
800 SQLRETURN WINAPI SQLPostInstallerError(DWORD fErrorCode, LPCSTR szErrorMsg)
801 {
802     FIXME("\n");
803     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
804     return FALSE;
805 }
806
807 BOOL WINAPI SQLReadFileDSNW(LPCWSTR lpszFileName, LPCWSTR lpszAppName,
808                LPCWSTR lpszKeyName, LPWSTR lpszString, WORD cbString,
809                WORD *pcbString)
810 {
811     clear_errors();
812     FIXME("\n");
813     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
814     return FALSE;
815 }
816
817 BOOL WINAPI SQLReadFileDSN(LPCSTR lpszFileName, LPCSTR lpszAppName,
818                LPCSTR lpszKeyName, LPSTR lpszString, WORD cbString,
819                WORD *pcbString)
820 {
821     clear_errors();
822     FIXME("\n");
823     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
824     return FALSE;
825 }
826
827 BOOL WINAPI SQLRemoveDefaultDataSource(void)
828 {
829     clear_errors();
830     FIXME("\n");
831     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
832     return FALSE;
833 }
834
835 BOOL WINAPI SQLRemoveDriverW(LPCWSTR lpszDriver, BOOL fRemoveDSN,
836                LPDWORD lpdwUsageCount)
837 {
838     clear_errors();
839     FIXME("\n");
840     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
841     return FALSE;
842 }
843
844 BOOL WINAPI SQLRemoveDriver(LPCSTR lpszDriver, BOOL fRemoveDSN,
845                LPDWORD lpdwUsageCount)
846 {
847     clear_errors();
848     FIXME("\n");
849     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
850     return FALSE;
851 }
852
853 BOOL WINAPI SQLRemoveDriverManager(LPDWORD pdwUsageCount)
854 {
855     clear_errors();
856     FIXME("\n");
857     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
858     return FALSE;
859 }
860
861 BOOL WINAPI SQLRemoveDSNFromIniW(LPCWSTR lpszDSN)
862 {
863     clear_errors();
864     FIXME("\n");
865     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
866     return FALSE;
867 }
868
869 BOOL WINAPI SQLRemoveDSNFromIni(LPCSTR lpszDSN)
870 {
871     clear_errors();
872     FIXME("\n");
873     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
874     return FALSE;
875 }
876
877 BOOL WINAPI SQLRemoveTranslatorW(LPCWSTR lpszTranslator, LPDWORD lpdwUsageCount)
878 {
879     clear_errors();
880     FIXME("\n");
881     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
882     return FALSE;
883 }
884
885 BOOL WINAPI SQLRemoveTranslator(LPCSTR lpszTranslator, LPDWORD lpdwUsageCount)
886 {
887     clear_errors();
888     FIXME("\n");
889     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
890     return FALSE;
891 }
892
893 BOOL WINAPI SQLSetConfigMode(UWORD wConfigMode)
894 {
895     clear_errors();
896     if (wConfigMode > ODBC_SYSTEM_DSN)
897     {
898         push_error(ODBC_ERROR_INVALID_PARAM_SEQUENCE, odbc_error_invalid_param_sequence);
899         return FALSE;
900     }
901     else
902     {
903         config_mode = wConfigMode;
904         return TRUE;
905     }
906 }
907
908 BOOL WINAPI SQLValidDSNW(LPCWSTR lpszDSN)
909 {
910     clear_errors();
911     FIXME("\n");
912     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
913     return FALSE;
914 }
915
916 BOOL WINAPI SQLValidDSN(LPCSTR lpszDSN)
917 {
918     clear_errors();
919     FIXME("\n");
920     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
921     return FALSE;
922 }
923
924 BOOL WINAPI SQLWriteDSNToIniW(LPCWSTR lpszDSN, LPCWSTR lpszDriver)
925 {
926     clear_errors();
927     FIXME("\n");
928     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
929     return FALSE;
930 }
931
932 BOOL WINAPI SQLWriteDSNToIni(LPCSTR lpszDSN, LPCSTR lpszDriver)
933 {
934     clear_errors();
935     FIXME("\n");
936     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
937     return FALSE;
938 }
939
940 BOOL WINAPI SQLWriteFileDSNW(LPCWSTR lpszFileName, LPCWSTR lpszAppName,
941                LPCWSTR lpszKeyName, LPCWSTR lpszString)
942 {
943     clear_errors();
944     FIXME("\n");
945     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
946     return FALSE;
947 }
948
949 BOOL WINAPI SQLWriteFileDSN(LPCSTR lpszFileName, LPCSTR lpszAppName,
950                LPCSTR lpszKeyName, LPCSTR lpszString)
951 {
952     clear_errors();
953     FIXME("\n");
954     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
955     return FALSE;
956 }
957
958 BOOL WINAPI SQLWritePrivateProfileStringW(LPCWSTR lpszSection, LPCWSTR lpszEntry,
959                LPCWSTR lpszString, LPCWSTR lpszFilename)
960 {
961     clear_errors();
962     FIXME("\n");
963     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
964     return FALSE;
965 }
966
967 BOOL WINAPI SQLWritePrivateProfileString(LPCSTR lpszSection, LPCSTR lpszEntry,
968                LPCSTR lpszString, LPCSTR lpszFilename)
969 {
970     clear_errors();
971     FIXME("\n");
972     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
973     return FALSE;
974 }