comctl32/tests: Flush events before testing edit control IME messages.
[wine.git] / dlls / odbccp32 / odbccp32.c
blob131cb158ed691d84c6109cae378a217508033fb5
1 /*
2 * Implementation of the ODBC driver installer
4 * Copyright 2005 Mike McCormack for CodeWeavers
5 * Copyright 2005 Hans Leidekker
6 * Copyright 2007 Bill Medland
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.
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.
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
23 #include <assert.h>
24 #include <stdarg.h>
26 #define COBJMACROS
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "winnls.h"
32 #include "sqlext.h"
33 #include "wine/debug.h"
34 #include "wine/heap.h"
36 #include "odbcinst.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(odbc);
40 /* Registry key names */
41 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};
42 static const WCHAR odbcW[] = {'S','o','f','t','w','a','r','e','\\','O','D','B','C',0};
43 static const WCHAR odbcini[] = {'S','o','f','t','w','a','r','e','\\','O','D','B','C','\\','O','D','B','C','I','N','S','T','.','I','N','I','\\',0};
44 static const WCHAR odbcdrivers[] = {'O','D','B','C',' ','D','r','i','v','e','r','s',0};
45 static const WCHAR odbctranslators[] = {'O','D','B','C',' ','T','r','a','n','s','l','a','t','o','r','s',0};
47 /* This config mode is known to be process-wide.
48 * MSDN documentation suggests that the value is hidden somewhere in the registry but I haven't found it yet.
49 * Although both the registry and the ODBC.ini files appear to be maintained together they are not maintained automatically through the registry's IniFileMapping.
51 static UWORD config_mode = ODBC_BOTH_DSN;
53 /* MSDN documentation suggests that the error subsystem handles errors 1 to 8
54 * only and experimentation (Windows 2000) shows that the errors are process-
55 * wide so go for the simple solution; static arrays.
57 static int num_errors;
58 static int error_code[8];
59 static const WCHAR *error_msg[8];
60 static const WCHAR odbc_error_general_err[] = {'G','e','n','e','r','a','l',' ','e','r','r','o','r',0};
61 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};
62 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};
63 static const WCHAR odbc_error_out_of_mem[] = {'O','u','t',' ','o','f',' ','m','e','m','o','r','y',0};
64 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};
65 static const WCHAR odbc_error_invalid_param_string[] = {'I','n','v','a','l','i','d',' ','p','a','r','a','m','e','t','e','r',' ','s','t','r','i','n','g',0};
66 static const WCHAR odbc_error_invalid_dsn[] = {'I','n','v','a','l','i','d',' ','D','S','N',0};
67 static const WCHAR odbc_error_load_lib_failed[] = {'L','o','a','d',' ','L','i','b','r','a','r','y',' ','F','a','i','l','e','d',0};
68 static const WCHAR odbc_error_request_failed[] = {'R','e','q','u','e','s','t',' ','F','a','i','l','e','d',0};
69 static const WCHAR odbc_error_invalid_keyword[] = {'I','n','v','a','l','i','d',' ','k','e','y','w','o','r','d',' ','v','a','l','u','e',0};
71 static BOOL (WINAPI *pConfigDSN)(HWND hwnd, WORD request, const char *driver, const char *attr);
72 static BOOL (WINAPI *pConfigDSNW)(HWND hwnd, WORD request, const WCHAR *driver, const WCHAR *attr);
74 /* Push an error onto the error stack, taking care of ranges etc. */
75 static void push_error(int code, LPCWSTR msg)
77 if (num_errors < ARRAY_SIZE(error_code))
79 error_code[num_errors] = code;
80 error_msg[num_errors] = msg;
81 num_errors++;
85 /* Clear the error stack */
86 static void clear_errors(void)
88 num_errors = 0;
91 static inline WCHAR *heap_strdupAtoW(const char *str)
93 LPWSTR ret = NULL;
95 if(str) {
96 DWORD len;
98 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
99 ret = heap_alloc(len*sizeof(WCHAR));
100 if(ret)
101 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
104 return ret;
108 BOOL WINAPI ODBCCPlApplet( LONG i, LONG j, LONG * p1, LONG * p2)
110 clear_errors();
111 FIXME( "( %ld %ld %p %p) : stub!\n", i, j, p1, p2);
112 return FALSE;
115 static LPWSTR SQLInstall_strdup_multi(LPCSTR str)
117 LPCSTR p;
118 LPWSTR ret = NULL;
119 DWORD len;
121 if (!str)
122 return ret;
124 for (p = str; *p; p += lstrlenA(p) + 1)
127 len = MultiByteToWideChar(CP_ACP, 0, str, p - str, NULL, 0 );
128 ret = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
129 MultiByteToWideChar(CP_ACP, 0, str, p - str, ret, len );
130 ret[len] = 0;
132 return ret;
135 static LPWSTR SQLInstall_strdup(LPCSTR str)
137 DWORD len;
138 LPWSTR ret = NULL;
140 if (!str)
141 return ret;
143 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0 );
144 ret = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
145 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len );
147 return ret;
150 /* Convert the wide string or zero-length-terminated list of wide strings to a
151 * narrow string or zero-length-terminated list of narrow strings.
152 * Do not try to emulate windows undocumented excesses (e.g. adding a third \0
153 * to a list)
154 * Arguments
155 * mode Indicates the sort of string.
156 * 1 denotes that the buffers contain strings terminated by a single nul
157 * character
158 * 2 denotes that the buffers contain zero-length-terminated lists
159 * (frequently erroneously referred to as double-null-terminated)
160 * buffer The narrow-character buffer into which to place the result. This
161 * must be a non-null pointer to the first element of a buffer whose
162 * length is passed in buffer_length.
163 * str The wide-character buffer containing the string or list of strings to
164 * be converted. str_length defines how many wide characters in the
165 * buffer are to be converted, including all desired terminating nul
166 * characters.
167 * str_length Effective length of str
168 * buffer_length Length of buffer
169 * returned_length A pointer to a variable that will receive the number of
170 * narrow characters placed into the buffer. This pointer
171 * may be NULL.
173 static BOOL SQLInstall_narrow(int mode, LPSTR buffer, LPCWSTR str, WORD str_length, WORD buffer_length, WORD *returned_length)
175 LPSTR pbuf; /* allows us to allocate a temporary buffer only if needed */
176 int len; /* Length of the converted list */
177 BOOL success = FALSE;
178 assert(mode == 1 || mode == 2);
179 assert(buffer_length);
180 len = WideCharToMultiByte(CP_ACP, 0, str, str_length, 0, 0, NULL, NULL);
181 if (len > 0)
183 if (len > buffer_length)
185 pbuf = HeapAlloc(GetProcessHeap(), 0, len);
187 else
189 pbuf = buffer;
191 len = WideCharToMultiByte(CP_ACP, 0, str, str_length, pbuf, len, NULL, NULL);
192 if (len > 0)
194 if (pbuf != buffer)
196 if (buffer_length > (mode - 1))
198 memcpy (buffer, pbuf, buffer_length-mode);
199 *(buffer+buffer_length-mode) = '\0';
201 *(buffer+buffer_length-1) = '\0';
203 if (returned_length)
205 *returned_length = pbuf == buffer ? len : buffer_length;
207 success = TRUE;
209 else
211 ERR("transferring wide to narrow\n");
213 if (pbuf != buffer)
215 HeapFree(GetProcessHeap(), 0, pbuf);
218 else
220 ERR("measuring wide to narrow\n");
222 return success;
225 static HMODULE load_config_driver(const WCHAR *driver)
227 static WCHAR reg_driver[] = {'d','r','i','v','e','r',0};
228 long ret;
229 HMODULE hmod;
230 WCHAR *filename = NULL;
231 DWORD size = 0, type;
232 HKEY hkey;
234 if ((ret = RegOpenKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey)) == ERROR_SUCCESS)
236 HKEY hkeydriver;
238 if ((ret = RegOpenKeyW(hkey, driver, &hkeydriver)) == ERROR_SUCCESS)
240 ret = RegGetValueW(hkeydriver, NULL, reg_driver, RRF_RT_REG_SZ, &type, NULL, &size);
241 if(ret != ERROR_SUCCESS || type != REG_SZ)
243 RegCloseKey(hkeydriver);
244 RegCloseKey(hkey);
245 push_error(ODBC_ERROR_INVALID_DSN, odbc_error_invalid_dsn);
247 return NULL;
250 filename = HeapAlloc(GetProcessHeap(), 0, size);
251 if(!filename)
253 RegCloseKey(hkeydriver);
254 RegCloseKey(hkey);
255 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
257 return NULL;
259 ret = RegGetValueW(hkeydriver, NULL, reg_driver, RRF_RT_REG_SZ, &type, filename, &size);
261 RegCloseKey(hkeydriver);
264 RegCloseKey(hkey);
267 if(ret != ERROR_SUCCESS)
269 HeapFree(GetProcessHeap(), 0, filename);
270 push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
271 return NULL;
274 hmod = LoadLibraryW(filename);
275 HeapFree(GetProcessHeap(), 0, filename);
277 if(!hmod)
278 push_error(ODBC_ERROR_LOAD_LIB_FAILED, odbc_error_load_lib_failed);
280 return hmod;
283 static BOOL write_config_value(const WCHAR *driver, const WCHAR *args)
285 long ret;
286 HKEY hkey, hkeydriver;
287 WCHAR *name = NULL;
289 if(!args)
290 return FALSE;
292 if((ret = RegOpenKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey)) == ERROR_SUCCESS)
294 if((ret = RegOpenKeyW(hkey, driver, &hkeydriver)) == ERROR_SUCCESS)
296 WCHAR *divider, *value;
298 name = heap_alloc( (lstrlenW(args) + 1) * sizeof(WCHAR));
299 if(!name)
301 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
302 goto fail;
304 lstrcpyW(name, args);
306 divider = wcschr(name,'=');
307 if(!divider)
309 push_error(ODBC_ERROR_INVALID_KEYWORD_VALUE, odbc_error_invalid_keyword);
310 goto fail;
313 value = divider + 1;
314 *divider = '\0';
316 TRACE("Write pair: %s = %s\n", debugstr_w(name), debugstr_w(value));
317 if(RegSetValueExW(hkeydriver, name, 0, REG_SZ, (BYTE*)value,
318 (lstrlenW(value)+1) * sizeof(WCHAR)) != ERROR_SUCCESS)
319 ERR("Failed to write registry installed key\n");
320 heap_free(name);
322 RegCloseKey(hkeydriver);
325 RegCloseKey(hkey);
328 if(ret != ERROR_SUCCESS)
329 push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
331 return ret == ERROR_SUCCESS;
333 fail:
334 RegCloseKey(hkeydriver);
335 RegCloseKey(hkey);
336 heap_free(name);
338 return FALSE;
341 static WORD map_request(WORD request)
343 switch (request)
345 case ODBC_ADD_DSN:
346 case ODBC_ADD_SYS_DSN:
347 return ODBC_ADD_DSN;
349 case ODBC_CONFIG_DSN:
350 case ODBC_CONFIG_SYS_DSN:
351 return ODBC_CONFIG_DSN;
353 case ODBC_REMOVE_DSN:
354 case ODBC_REMOVE_SYS_DSN:
355 return ODBC_REMOVE_DSN;
357 default:
358 FIXME("unhandled request %u\n", request);
359 return 0;
363 static UWORD get_config_mode(WORD request)
365 if (request == ODBC_ADD_DSN || request == ODBC_CONFIG_DSN || request == ODBC_REMOVE_DSN) return ODBC_USER_DSN;
366 return ODBC_SYSTEM_DSN;
369 BOOL WINAPI SQLConfigDataSourceW(HWND hwnd, WORD request, LPCWSTR driver, LPCWSTR attributes)
371 HMODULE mod;
372 BOOL ret = FALSE;
373 UWORD config_mode_prev = config_mode;
374 WORD mapped_request;
376 TRACE("%p, %d, %s, %s\n", hwnd, request, debugstr_w(driver), debugstr_w(attributes));
377 if (TRACE_ON(odbc))
379 const WCHAR *p;
380 for (p = attributes; *p; p += lstrlenW(p) + 1)
381 TRACE("%s\n", debugstr_w(p));
384 clear_errors();
386 mapped_request = map_request(request);
387 if (!mapped_request)
388 return FALSE;
390 mod = load_config_driver(driver);
391 if (!mod)
392 return FALSE;
394 config_mode = get_config_mode(request);
396 pConfigDSNW = (void*)GetProcAddress(mod, "ConfigDSNW");
397 if(pConfigDSNW)
398 ret = pConfigDSNW(hwnd, mapped_request, driver, attributes);
399 else
400 ERR("Failed to find ConfigDSNW\n");
402 config_mode = config_mode_prev;
404 if (!ret)
405 push_error(ODBC_ERROR_REQUEST_FAILED, odbc_error_request_failed);
407 FreeLibrary(mod);
409 return ret;
412 BOOL WINAPI SQLConfigDataSource(HWND hwnd, WORD request, LPCSTR driver, LPCSTR attributes)
414 HMODULE mod;
415 BOOL ret = FALSE;
416 WCHAR *driverW;
417 UWORD config_mode_prev = config_mode;
418 WORD mapped_request;
420 TRACE("%p, %d, %s, %s\n", hwnd, request, debugstr_a(driver), debugstr_a(attributes));
422 if (TRACE_ON(odbc))
424 const char *p;
425 for (p = attributes; *p; p += lstrlenA(p) + 1)
426 TRACE("%s\n", debugstr_a(p));
429 clear_errors();
431 mapped_request = map_request(request);
432 if (!mapped_request)
433 return FALSE;
435 driverW = heap_strdupAtoW(driver);
436 if (!driverW)
438 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
439 return FALSE;
442 mod = load_config_driver(driverW);
443 if (!mod)
445 heap_free(driverW);
446 return FALSE;
449 config_mode = get_config_mode(request);
451 pConfigDSN = (void*)GetProcAddress(mod, "ConfigDSN");
452 if (pConfigDSN)
454 TRACE("Calling ConfigDSN\n");
455 ret = pConfigDSN(hwnd, mapped_request, driver, attributes);
457 else
459 pConfigDSNW = (void*)GetProcAddress(mod, "ConfigDSNW");
460 if (pConfigDSNW)
462 WCHAR *attr = NULL;
463 TRACE("Calling ConfigDSNW\n");
465 attr = SQLInstall_strdup_multi(attributes);
466 if(attr)
467 ret = pConfigDSNW(hwnd, mapped_request, driverW, attr);
468 heap_free(attr);
472 config_mode = config_mode_prev;
474 if (!ret)
475 push_error(ODBC_ERROR_REQUEST_FAILED, odbc_error_request_failed);
477 heap_free(driverW);
478 FreeLibrary(mod);
480 return ret;
483 BOOL WINAPI SQLConfigDriverW(HWND hwnd, WORD request, LPCWSTR driver,
484 LPCWSTR args, LPWSTR msg, WORD msgmax, WORD *msgout)
486 BOOL (WINAPI *pConfigDriverW)(HWND hwnd, WORD request, const WCHAR *driver, const WCHAR *args, const WCHAR *msg, WORD msgmax, WORD *msgout);
487 HMODULE hmod;
488 BOOL funcret = FALSE;
490 clear_errors();
491 TRACE("(%p %d %s %s %p %d %p)\n", hwnd, request, debugstr_w(driver),
492 debugstr_w(args), msg, msgmax, msgout);
494 if(request == ODBC_CONFIG_DRIVER)
496 return write_config_value(driver, args);
499 hmod = load_config_driver(driver);
500 if(!hmod)
501 return FALSE;
503 pConfigDriverW = (void*)GetProcAddress(hmod, "ConfigDriverW");
504 if(pConfigDriverW)
505 funcret = pConfigDriverW(hwnd, request, driver, args, msg, msgmax, msgout);
507 if(!funcret)
508 push_error(ODBC_ERROR_REQUEST_FAILED, odbc_error_request_failed);
510 FreeLibrary(hmod);
512 return funcret;
515 BOOL WINAPI SQLConfigDriver(HWND hwnd, WORD request, LPCSTR driver,
516 LPCSTR args, LPSTR msg, WORD msgmax, WORD *msgout)
518 BOOL (WINAPI *pConfigDriverA)(HWND hwnd, WORD request, const char *driver, const char *args, const char *msg, WORD msgmax, WORD *msgout);
519 HMODULE hmod;
520 WCHAR *driverW;
521 BOOL funcret = FALSE;
523 clear_errors();
524 TRACE("(%p %d %s %s %p %d %p)\n", hwnd, request, debugstr_a(driver),
525 debugstr_a(args), msg, msgmax, msgout);
527 driverW = heap_strdupAtoW(driver);
528 if(!driverW)
530 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
531 return FALSE;
533 if(request == ODBC_CONFIG_DRIVER)
535 BOOL ret = FALSE;
536 WCHAR *argsW = heap_strdupAtoW(args);
537 if(argsW)
539 ret = write_config_value(driverW, argsW);
540 HeapFree(GetProcessHeap(), 0, argsW);
542 else
544 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
547 HeapFree(GetProcessHeap(), 0, driverW);
549 return ret;
552 hmod = load_config_driver(driverW);
553 HeapFree(GetProcessHeap(), 0, driverW);
554 if(!hmod)
555 return FALSE;
557 pConfigDriverA = (void*)GetProcAddress(hmod, "ConfigDriver");
558 if(pConfigDriverA)
559 funcret = pConfigDriverA(hwnd, request, driver, args, msg, msgmax, msgout);
561 if(!funcret)
562 push_error(ODBC_ERROR_REQUEST_FAILED, odbc_error_request_failed);
564 FreeLibrary(hmod);
566 return funcret;
569 BOOL WINAPI SQLCreateDataSourceW(HWND hwnd, LPCWSTR lpszDS)
571 clear_errors();
572 FIXME("%p %s\n", hwnd, debugstr_w(lpszDS));
573 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
574 return FALSE;
577 BOOL WINAPI SQLCreateDataSource(HWND hwnd, LPCSTR lpszDS)
579 clear_errors();
580 FIXME("%p %s\n", hwnd, debugstr_a(lpszDS));
581 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
582 return FALSE;
585 BOOL WINAPI SQLGetAvailableDriversW(LPCWSTR lpszInfFile, LPWSTR lpszBuf,
586 WORD cbBufMax, WORD *pcbBufOut)
588 clear_errors();
589 FIXME("%s %p %d %p\n", debugstr_w(lpszInfFile), lpszBuf, cbBufMax, pcbBufOut);
590 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
591 return FALSE;
594 BOOL WINAPI SQLGetAvailableDrivers(LPCSTR lpszInfFile, LPSTR lpszBuf,
595 WORD cbBufMax, WORD *pcbBufOut)
597 clear_errors();
598 FIXME("%s %p %d %p\n", debugstr_a(lpszInfFile), lpszBuf, cbBufMax, pcbBufOut);
599 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
600 return FALSE;
603 BOOL WINAPI SQLGetConfigMode(UWORD *pwConfigMode)
605 clear_errors();
606 TRACE("%p\n", pwConfigMode);
607 if (pwConfigMode)
608 *pwConfigMode = config_mode;
609 return TRUE;
612 BOOL WINAPI SQLGetInstalledDriversW(WCHAR *buf, WORD size, WORD *sizeout)
614 WORD written = 0;
615 DWORD index = 0;
616 BOOL ret = TRUE;
617 DWORD valuelen;
618 WCHAR *value;
619 HKEY drivers;
620 DWORD len;
621 LONG res;
623 clear_errors();
625 TRACE("%p %d %p\n", buf, size, sizeout);
627 if (!buf || !size)
629 push_error(ODBC_ERROR_INVALID_BUFF_LEN, odbc_error_invalid_buff_len);
630 return FALSE;
633 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, drivers_key, 0, KEY_QUERY_VALUE, &drivers);
634 if (res)
636 push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
637 return FALSE;
640 valuelen = 256;
641 value = heap_alloc(valuelen * sizeof(WCHAR));
643 size--;
645 while (1)
647 len = valuelen;
648 res = RegEnumValueW(drivers, index, value, &len, NULL, NULL, NULL, NULL);
649 while (res == ERROR_MORE_DATA)
651 value = heap_realloc(value, ++len * sizeof(WCHAR));
652 res = RegEnumValueW(drivers, index, value, &len, NULL, NULL, NULL, NULL);
654 if (res == ERROR_SUCCESS)
656 lstrcpynW(buf + written, value, size - written);
657 written += min(len + 1, size - written);
659 else if (res == ERROR_NO_MORE_ITEMS)
660 break;
661 else
663 push_error(ODBC_ERROR_GENERAL_ERR, odbc_error_general_err);
664 ret = FALSE;
665 break;
667 index++;
670 buf[written++] = 0;
672 heap_free(value);
673 RegCloseKey(drivers);
674 if (sizeout)
675 *sizeout = written;
676 return ret;
679 BOOL WINAPI SQLGetInstalledDrivers(char *buf, WORD size, WORD *sizeout)
681 WORD written;
682 WCHAR *wbuf;
683 BOOL ret;
685 TRACE("%p %d %p\n", buf, size, sizeout);
687 if (!buf || !size)
689 push_error(ODBC_ERROR_INVALID_BUFF_LEN, odbc_error_invalid_buff_len);
690 return FALSE;
693 wbuf = heap_alloc(size * sizeof(WCHAR));
694 if (!wbuf)
696 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
697 return FALSE;
700 ret = SQLGetInstalledDriversW(wbuf, size, &written);
701 if (!ret)
703 heap_free(wbuf);
704 return FALSE;
707 if (sizeout)
708 *sizeout = WideCharToMultiByte(CP_ACP, 0, wbuf, written, NULL, 0, NULL, NULL);
709 WideCharToMultiByte(CP_ACP, 0, wbuf, written, buf, size, NULL, NULL);
711 heap_free(wbuf);
712 return TRUE;
715 static HKEY get_privateprofile_sectionkey(LPCWSTR section, LPCWSTR filename)
717 HKEY hkey, hkeyfilename, hkeysection;
718 LONG ret;
720 if (RegOpenKeyW(HKEY_CURRENT_USER, odbcW, &hkey))
721 return NULL;
723 ret = RegOpenKeyW(hkey, filename, &hkeyfilename);
724 RegCloseKey(hkey);
725 if (ret)
726 return NULL;
728 ret = RegOpenKeyW(hkeyfilename, section, &hkeysection);
729 RegCloseKey(hkeyfilename);
731 return ret ? NULL : hkeysection;
734 int WINAPI SQLGetPrivateProfileStringW(LPCWSTR section, LPCWSTR entry,
735 LPCWSTR defvalue, LPWSTR buff, int buff_len, LPCWSTR filename)
737 BOOL usedefault = TRUE;
738 HKEY sectionkey;
739 LONG ret = 0;
741 TRACE("%s %s %s %p %d %s\n", debugstr_w(section), debugstr_w(entry),
742 debugstr_w(defvalue), buff, buff_len, debugstr_w(filename));
744 clear_errors();
746 if (buff_len <= 0 || !section)
747 return 0;
749 if(buff)
750 buff[0] = 0;
752 if (!defvalue || !buff)
753 return 0;
755 sectionkey = get_privateprofile_sectionkey(section, filename);
756 if (sectionkey)
758 DWORD type, size;
760 if (entry)
762 size = buff_len * sizeof(*buff);
763 if (RegGetValueW(sectionkey, NULL, entry, RRF_RT_REG_SZ, &type, buff, &size) == ERROR_SUCCESS)
765 usedefault = FALSE;
766 ret = (size / sizeof(*buff)) - 1;
769 else
771 WCHAR name[MAX_PATH];
772 DWORD index = 0;
773 DWORD namelen;
775 usedefault = FALSE;
777 memset(buff, 0, buff_len);
779 namelen = sizeof(name);
780 while (RegEnumValueW(sectionkey, index, name, &namelen, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
782 if ((ret + namelen+1) > buff_len)
783 break;
785 lstrcpyW(buff+ret, name);
786 ret += namelen+1;
787 namelen = sizeof(name);
788 index++;
792 RegCloseKey(sectionkey);
794 else
795 usedefault = entry != NULL;
797 if (usedefault)
799 lstrcpynW(buff, defvalue, buff_len);
800 ret = lstrlenW(buff);
803 return ret;
806 int WINAPI SQLGetPrivateProfileString(LPCSTR section, LPCSTR entry,
807 LPCSTR defvalue, LPSTR buff, int buff_len, LPCSTR filename)
809 WCHAR *sectionW, *filenameW;
810 BOOL usedefault = TRUE;
811 HKEY sectionkey;
812 LONG ret = 0;
814 TRACE("%s %s %s %p %d %s\n", debugstr_a(section), debugstr_a(entry),
815 debugstr_a(defvalue), buff, buff_len, debugstr_a(filename));
817 clear_errors();
819 if (buff_len <= 0)
820 return 0;
822 if (buff)
823 buff[0] = 0;
825 if (!section || !defvalue || !buff)
826 return 0;
828 sectionW = heap_strdupAtoW(section);
829 filenameW = heap_strdupAtoW(filename);
831 sectionkey = get_privateprofile_sectionkey(sectionW, filenameW);
833 heap_free(sectionW);
834 heap_free(filenameW);
836 if (sectionkey)
838 DWORD type, size;
840 if (entry)
842 size = buff_len * sizeof(*buff);
843 if (RegGetValueA(sectionkey, NULL, entry, RRF_RT_REG_SZ, &type, buff, &size) == ERROR_SUCCESS)
845 usedefault = FALSE;
846 ret = (size / sizeof(*buff)) - 1;
849 else
851 char name[MAX_PATH] = {0};
852 DWORD index = 0;
853 DWORD namelen;
855 usedefault = FALSE;
857 memset(buff, 0, buff_len);
859 namelen = sizeof(name);
860 while (RegEnumValueA(sectionkey, index, name, &namelen, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
862 if ((ret + namelen+1) > buff_len)
863 break;
865 lstrcpyA(buff+ret, name);
867 ret += namelen+1;
868 namelen = sizeof(name);
869 index++;
873 RegCloseKey(sectionkey);
875 else
876 usedefault = entry != NULL;
878 if (usedefault)
880 lstrcpynA(buff, defvalue, buff_len);
881 ret = strlen(buff);
884 return ret;
887 BOOL WINAPI SQLGetTranslatorW(HWND hwndParent, LPWSTR lpszName, WORD cbNameMax,
888 WORD *pcbNameOut, LPWSTR lpszPath, WORD cbPathMax,
889 WORD *pcbPathOut, DWORD *pvOption)
891 clear_errors();
892 FIXME("%p %s %d %p %p %d %p %p\n", hwndParent, debugstr_w(lpszName), cbNameMax,
893 pcbNameOut, lpszPath, cbPathMax, pcbPathOut, pvOption);
894 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
895 return FALSE;
898 BOOL WINAPI SQLGetTranslator(HWND hwndParent, LPSTR lpszName, WORD cbNameMax,
899 WORD *pcbNameOut, LPSTR lpszPath, WORD cbPathMax,
900 WORD *pcbPathOut, DWORD *pvOption)
902 clear_errors();
903 FIXME("%p %s %d %p %p %d %p %p\n", hwndParent, debugstr_a(lpszName), cbNameMax,
904 pcbNameOut, lpszPath, cbPathMax, pcbPathOut, pvOption);
905 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
906 return FALSE;
909 BOOL WINAPI SQLInstallDriverW(LPCWSTR lpszInfFile, LPCWSTR lpszDriver,
910 LPWSTR lpszPath, WORD cbPathMax, WORD * pcbPathOut)
912 DWORD usage;
914 clear_errors();
915 TRACE("%s %s %p %d %p\n", debugstr_w(lpszInfFile),
916 debugstr_w(lpszDriver), lpszPath, cbPathMax, pcbPathOut);
918 if (lpszInfFile)
919 return FALSE;
921 return SQLInstallDriverExW(lpszDriver, NULL, lpszPath, cbPathMax,
922 pcbPathOut, ODBC_INSTALL_COMPLETE, &usage);
925 BOOL WINAPI SQLInstallDriver(LPCSTR lpszInfFile, LPCSTR lpszDriver,
926 LPSTR lpszPath, WORD cbPathMax, WORD * pcbPathOut)
928 DWORD usage;
930 clear_errors();
931 TRACE("%s %s %p %d %p\n", debugstr_a(lpszInfFile),
932 debugstr_a(lpszDriver), lpszPath, cbPathMax, pcbPathOut);
934 if (lpszInfFile)
935 return FALSE;
937 return SQLInstallDriverEx(lpszDriver, NULL, lpszPath, cbPathMax,
938 pcbPathOut, ODBC_INSTALL_COMPLETE, &usage);
941 static void write_registry_values(const WCHAR *regkey, const WCHAR *driver, const WCHAR *path_in, WCHAR *path,
942 DWORD *usage_count)
944 static const WCHAR installed[] = {'I','n','s','t','a','l','l','e','d',0};
945 static const WCHAR slash[] = {'\\', 0};
946 static const WCHAR driverW[] = {'D','r','i','v','e','r',0};
947 static const WCHAR setupW[] = {'S','e','t','u','p',0};
948 static const WCHAR translator[] = {'T','r','a','n','s','l','a','t','o','r',0};
949 HKEY hkey, hkeydriver;
951 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey) == ERROR_SUCCESS)
953 if (RegCreateKeyW(hkey, regkey, &hkeydriver) == ERROR_SUCCESS)
955 if(RegSetValueExW(hkeydriver, driver, 0, REG_SZ, (BYTE*)installed, sizeof(installed)) != ERROR_SUCCESS)
956 ERR("Failed to write registry installed key\n");
958 RegCloseKey(hkeydriver);
961 if (RegCreateKeyW(hkey, driver, &hkeydriver) == ERROR_SUCCESS)
963 WCHAR entry[1024];
964 const WCHAR *p;
965 DWORD usagecount = 0;
966 DWORD type, size;
968 /* Skip name entry */
969 p = driver;
970 p += lstrlenW(p) + 1;
972 if (!path_in)
973 GetSystemDirectoryW(path, MAX_PATH);
974 else
975 lstrcpyW(path, path_in);
977 /* Store Usage */
978 size = sizeof(usagecount);
979 RegGetValueA(hkeydriver, NULL, "UsageCount", RRF_RT_DWORD, &type, &usagecount, &size);
980 TRACE("Usage count %ld\n", usagecount);
982 for (; *p; p += lstrlenW(p) + 1)
984 WCHAR *divider = wcschr(p,'=');
986 if (divider)
988 WCHAR *value;
989 int len;
991 /* Write pair values to the registry. */
992 lstrcpynW(entry, p, divider - p + 1);
994 divider++;
995 TRACE("Writing pair %s,%s\n", debugstr_w(entry), debugstr_w(divider));
997 /* Driver, Setup, Translator entries use the system path unless a path is specified. */
998 if(lstrcmpiW(driverW, entry) == 0 || lstrcmpiW(setupW, entry) == 0 ||
999 lstrcmpiW(translator, entry) == 0)
1001 len = lstrlenW(path) + lstrlenW(slash) + lstrlenW(divider) + 1;
1002 value = heap_alloc(len * sizeof(WCHAR));
1003 if(!value)
1005 ERR("Out of memory\n");
1006 return;
1009 lstrcpyW(value, path);
1010 lstrcatW(value, slash);
1011 lstrcatW(value, divider);
1013 else
1015 len = lstrlenW(divider) + 1;
1016 value = heap_alloc(len * sizeof(WCHAR));
1017 lstrcpyW(value, divider);
1020 if (RegSetValueExW(hkeydriver, entry, 0, REG_SZ, (BYTE*)value,
1021 (lstrlenW(value)+1)*sizeof(WCHAR)) != ERROR_SUCCESS)
1022 ERR("Failed to write registry data %s %s\n", debugstr_w(entry), debugstr_w(value));
1023 heap_free(value);
1025 else
1027 ERR("No pair found. %s\n", debugstr_w(p));
1028 break;
1032 /* Set Usage Count */
1033 usagecount++;
1034 if (RegSetValueExA(hkeydriver, "UsageCount", 0, REG_DWORD, (BYTE*)&usagecount, sizeof(usagecount)) != ERROR_SUCCESS)
1035 ERR("Failed to write registry UsageCount key\n");
1037 if (usage_count)
1038 *usage_count = usagecount;
1040 RegCloseKey(hkeydriver);
1043 RegCloseKey(hkey);
1047 BOOL WINAPI SQLInstallDriverExW(LPCWSTR lpszDriver, LPCWSTR lpszPathIn,
1048 LPWSTR lpszPathOut, WORD cbPathOutMax, WORD *pcbPathOut,
1049 WORD fRequest, LPDWORD lpdwUsageCount)
1051 UINT len;
1052 WCHAR path[MAX_PATH];
1054 clear_errors();
1055 TRACE("%s %s %p %d %p %d %p\n", debugstr_w(lpszDriver),
1056 debugstr_w(lpszPathIn), lpszPathOut, cbPathOutMax, pcbPathOut,
1057 fRequest, lpdwUsageCount);
1059 write_registry_values(odbcdrivers, lpszDriver, lpszPathIn, path, lpdwUsageCount);
1061 len = lstrlenW(path);
1063 if (pcbPathOut)
1064 *pcbPathOut = len;
1066 if (lpszPathOut && cbPathOutMax > len)
1068 lstrcpyW(lpszPathOut, path);
1069 return TRUE;
1071 return FALSE;
1074 BOOL WINAPI SQLInstallDriverEx(LPCSTR lpszDriver, LPCSTR lpszPathIn,
1075 LPSTR lpszPathOut, WORD cbPathOutMax, WORD *pcbPathOut,
1076 WORD fRequest, LPDWORD lpdwUsageCount)
1078 LPWSTR driver, pathin;
1079 WCHAR pathout[MAX_PATH];
1080 BOOL ret;
1081 WORD cbOut = 0;
1083 clear_errors();
1084 TRACE("%s %s %p %d %p %d %p\n", debugstr_a(lpszDriver),
1085 debugstr_a(lpszPathIn), lpszPathOut, cbPathOutMax, pcbPathOut,
1086 fRequest, lpdwUsageCount);
1088 driver = SQLInstall_strdup_multi(lpszDriver);
1089 pathin = SQLInstall_strdup(lpszPathIn);
1091 ret = SQLInstallDriverExW(driver, pathin, pathout, MAX_PATH, &cbOut,
1092 fRequest, lpdwUsageCount);
1093 if (ret)
1095 int len = WideCharToMultiByte(CP_ACP, 0, pathout, -1, lpszPathOut,
1096 0, NULL, NULL);
1097 if (len)
1099 if (pcbPathOut)
1100 *pcbPathOut = len - 1;
1102 if (!lpszPathOut || cbPathOutMax < len)
1104 ret = FALSE;
1105 goto out;
1107 len = WideCharToMultiByte(CP_ACP, 0, pathout, -1, lpszPathOut,
1108 cbPathOutMax, NULL, NULL);
1112 out:
1113 HeapFree(GetProcessHeap(), 0, driver);
1114 HeapFree(GetProcessHeap(), 0, pathin);
1115 return ret;
1118 BOOL WINAPI SQLInstallDriverManagerW(LPWSTR lpszPath, WORD cbPathMax,
1119 WORD *pcbPathOut)
1121 UINT len;
1122 WCHAR path[MAX_PATH];
1124 TRACE("(%p %d %p)\n", lpszPath, cbPathMax, pcbPathOut);
1126 if (cbPathMax < MAX_PATH)
1127 return FALSE;
1129 clear_errors();
1131 len = GetSystemDirectoryW(path, MAX_PATH);
1133 if (pcbPathOut)
1134 *pcbPathOut = len;
1136 if (lpszPath && cbPathMax > len)
1138 lstrcpyW(lpszPath, path);
1139 return TRUE;
1141 return FALSE;
1144 BOOL WINAPI SQLInstallDriverManager(LPSTR lpszPath, WORD cbPathMax,
1145 WORD *pcbPathOut)
1147 BOOL ret;
1148 WORD len, cbOut = 0;
1149 WCHAR path[MAX_PATH];
1151 TRACE("(%p %d %p)\n", lpszPath, cbPathMax, pcbPathOut);
1153 if (cbPathMax < MAX_PATH)
1154 return FALSE;
1156 clear_errors();
1158 ret = SQLInstallDriverManagerW(path, MAX_PATH, &cbOut);
1159 if (ret)
1161 len = WideCharToMultiByte(CP_ACP, 0, path, -1, lpszPath, 0,
1162 NULL, NULL);
1163 if (len)
1165 if (pcbPathOut)
1166 *pcbPathOut = len - 1;
1168 if (!lpszPath || cbPathMax < len)
1169 return FALSE;
1171 len = WideCharToMultiByte(CP_ACP, 0, path, -1, lpszPath,
1172 cbPathMax, NULL, NULL);
1175 return ret;
1178 BOOL WINAPI SQLInstallODBCW(HWND hwndParent, LPCWSTR lpszInfFile,
1179 LPCWSTR lpszSrcPath, LPCWSTR lpszDrivers)
1181 clear_errors();
1182 FIXME("%p %s %s %s\n", hwndParent, debugstr_w(lpszInfFile),
1183 debugstr_w(lpszSrcPath), debugstr_w(lpszDrivers));
1184 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1185 return FALSE;
1188 BOOL WINAPI SQLInstallODBC(HWND hwndParent, LPCSTR lpszInfFile,
1189 LPCSTR lpszSrcPath, LPCSTR lpszDrivers)
1191 clear_errors();
1192 FIXME("%p %s %s %s\n", hwndParent, debugstr_a(lpszInfFile),
1193 debugstr_a(lpszSrcPath), debugstr_a(lpszDrivers));
1194 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1195 return FALSE;
1198 SQLRETURN WINAPI SQLInstallerErrorW(WORD iError, DWORD *pfErrorCode,
1199 LPWSTR lpszErrorMsg, WORD cbErrorMsgMax, WORD *pcbErrorMsg)
1201 TRACE("%d %p %p %d %p\n", iError, pfErrorCode, lpszErrorMsg,
1202 cbErrorMsgMax, pcbErrorMsg);
1204 if (iError == 0)
1206 return SQL_ERROR;
1208 else if (iError <= num_errors)
1210 BOOL truncated = FALSE;
1211 WORD len;
1212 LPCWSTR msg;
1213 iError--;
1214 if (pfErrorCode)
1215 *pfErrorCode = error_code[iError];
1216 msg = error_msg[iError];
1217 len = msg ? lstrlenW(msg) : 0;
1218 if (pcbErrorMsg)
1219 *pcbErrorMsg = len;
1220 len++;
1221 if (cbErrorMsgMax < len)
1223 len = cbErrorMsgMax;
1224 truncated = TRUE;
1226 if (lpszErrorMsg && len)
1228 if (msg)
1230 memcpy (lpszErrorMsg, msg, len * sizeof(WCHAR));
1232 else
1234 assert(len==1);
1235 *lpszErrorMsg = 0;
1238 else
1240 /* Yes. If you pass a null pointer and a large length it is not an error! */
1241 truncated = TRUE;
1244 return truncated ? SQL_SUCCESS_WITH_INFO : SQL_SUCCESS;
1247 /* 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 */
1248 if (pcbErrorMsg)
1249 *pcbErrorMsg = 0;
1251 if (lpszErrorMsg && cbErrorMsgMax > 0)
1252 *lpszErrorMsg = '\0';
1254 return SQL_NO_DATA;
1257 SQLRETURN WINAPI SQLInstallerError(WORD iError, DWORD *pfErrorCode,
1258 LPSTR lpszErrorMsg, WORD cbErrorMsgMax, WORD *pcbErrorMsg)
1260 SQLRETURN ret;
1261 LPWSTR wbuf;
1262 WORD cbwbuf;
1263 TRACE("%d %p %p %d %p\n", iError, pfErrorCode, lpszErrorMsg,
1264 cbErrorMsgMax, pcbErrorMsg);
1266 wbuf = 0;
1267 if (lpszErrorMsg && cbErrorMsgMax)
1269 wbuf = HeapAlloc(GetProcessHeap(), 0, cbErrorMsgMax*sizeof(WCHAR));
1270 if (!wbuf)
1271 return SQL_ERROR;
1273 ret = SQLInstallerErrorW(iError, pfErrorCode, wbuf, cbErrorMsgMax, &cbwbuf);
1274 if (wbuf)
1276 WORD cbBuf = 0;
1277 SQLInstall_narrow(1, lpszErrorMsg, wbuf, cbwbuf+1, cbErrorMsgMax, &cbBuf);
1278 HeapFree(GetProcessHeap(), 0, wbuf);
1279 if (pcbErrorMsg)
1280 *pcbErrorMsg = cbBuf-1;
1282 return ret;
1285 BOOL WINAPI SQLInstallTranslatorExW(LPCWSTR lpszTranslator, LPCWSTR lpszPathIn,
1286 LPWSTR lpszPathOut, WORD cbPathOutMax, WORD *pcbPathOut,
1287 WORD fRequest, LPDWORD lpdwUsageCount)
1289 UINT len;
1290 WCHAR path[MAX_PATH];
1292 clear_errors();
1293 TRACE("%s %s %p %d %p %d %p\n", debugstr_w(lpszTranslator),
1294 debugstr_w(lpszPathIn), lpszPathOut, cbPathOutMax, pcbPathOut,
1295 fRequest, lpdwUsageCount);
1297 write_registry_values(odbctranslators, lpszTranslator, lpszPathIn, path, lpdwUsageCount);
1299 len = lstrlenW(path);
1301 if (pcbPathOut)
1302 *pcbPathOut = len;
1304 if (lpszPathOut && cbPathOutMax > len)
1306 lstrcpyW(lpszPathOut, path);
1307 return TRUE;
1309 return FALSE;
1312 BOOL WINAPI SQLInstallTranslatorEx(LPCSTR lpszTranslator, LPCSTR lpszPathIn,
1313 LPSTR lpszPathOut, WORD cbPathOutMax, WORD *pcbPathOut,
1314 WORD fRequest, LPDWORD lpdwUsageCount)
1316 LPCSTR p;
1317 LPWSTR translator, pathin;
1318 WCHAR pathout[MAX_PATH];
1319 BOOL ret;
1320 WORD cbOut = 0;
1322 clear_errors();
1323 TRACE("%s %s %p %d %p %d %p\n", debugstr_a(lpszTranslator),
1324 debugstr_a(lpszPathIn), lpszPathOut, cbPathOutMax, pcbPathOut,
1325 fRequest, lpdwUsageCount);
1327 for (p = lpszTranslator; *p; p += lstrlenA(p) + 1)
1328 TRACE("%s\n", debugstr_a(p));
1330 translator = SQLInstall_strdup_multi(lpszTranslator);
1331 pathin = SQLInstall_strdup(lpszPathIn);
1333 ret = SQLInstallTranslatorExW(translator, pathin, pathout, MAX_PATH,
1334 &cbOut, fRequest, lpdwUsageCount);
1335 if (ret)
1337 int len = WideCharToMultiByte(CP_ACP, 0, pathout, -1, lpszPathOut,
1338 0, NULL, NULL);
1339 if (len)
1341 if (pcbPathOut)
1342 *pcbPathOut = len - 1;
1344 if (!lpszPathOut || cbPathOutMax < len)
1346 ret = FALSE;
1347 goto out;
1349 len = WideCharToMultiByte(CP_ACP, 0, pathout, -1, lpszPathOut,
1350 cbPathOutMax, NULL, NULL);
1354 out:
1355 HeapFree(GetProcessHeap(), 0, translator);
1356 HeapFree(GetProcessHeap(), 0, pathin);
1357 return ret;
1360 BOOL WINAPI SQLInstallTranslator(LPCSTR lpszInfFile, LPCSTR lpszTranslator,
1361 LPCSTR lpszPathIn, LPSTR lpszPathOut, WORD cbPathOutMax,
1362 WORD *pcbPathOut, WORD fRequest, LPDWORD lpdwUsageCount)
1364 clear_errors();
1365 TRACE("%s %s %s %p %d %p %d %p\n", debugstr_a(lpszInfFile),
1366 debugstr_a(lpszTranslator), debugstr_a(lpszPathIn), lpszPathOut,
1367 cbPathOutMax, pcbPathOut, fRequest, lpdwUsageCount);
1369 if (lpszInfFile)
1370 return FALSE;
1372 return SQLInstallTranslatorEx(lpszTranslator, lpszPathIn, lpszPathOut,
1373 cbPathOutMax, pcbPathOut, fRequest, lpdwUsageCount);
1376 BOOL WINAPI SQLInstallTranslatorW(LPCWSTR lpszInfFile, LPCWSTR lpszTranslator,
1377 LPCWSTR lpszPathIn, LPWSTR lpszPathOut, WORD cbPathOutMax,
1378 WORD *pcbPathOut, WORD fRequest, LPDWORD lpdwUsageCount)
1380 clear_errors();
1381 TRACE("%s %s %s %p %d %p %d %p\n", debugstr_w(lpszInfFile),
1382 debugstr_w(lpszTranslator), debugstr_w(lpszPathIn), lpszPathOut,
1383 cbPathOutMax, pcbPathOut, fRequest, lpdwUsageCount);
1385 if (lpszInfFile)
1386 return FALSE;
1388 return SQLInstallTranslatorExW(lpszTranslator, lpszPathIn, lpszPathOut,
1389 cbPathOutMax, pcbPathOut, fRequest, lpdwUsageCount);
1392 BOOL WINAPI SQLManageDataSources(HWND hwnd)
1394 clear_errors();
1395 FIXME("%p\n", hwnd);
1396 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1397 return FALSE;
1400 SQLRETURN WINAPI SQLPostInstallerErrorW(DWORD fErrorCode, LPCWSTR szErrorMsg)
1402 FIXME("%lu %s\n", fErrorCode, debugstr_w(szErrorMsg));
1403 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1404 return FALSE;
1407 SQLRETURN WINAPI SQLPostInstallerError(DWORD fErrorCode, LPCSTR szErrorMsg)
1409 FIXME("%lu %s\n", fErrorCode, debugstr_a(szErrorMsg));
1410 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1411 return FALSE;
1414 BOOL WINAPI SQLReadFileDSNW(LPCWSTR lpszFileName, LPCWSTR lpszAppName,
1415 LPCWSTR lpszKeyName, LPWSTR lpszString, WORD cbString,
1416 WORD *pcbString)
1418 clear_errors();
1419 FIXME("%s %s %s %s %d %p\n", debugstr_w(lpszFileName), debugstr_w(lpszAppName),
1420 debugstr_w(lpszKeyName), debugstr_w(lpszString), cbString, pcbString);
1421 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1422 return FALSE;
1425 BOOL WINAPI SQLReadFileDSN(LPCSTR lpszFileName, LPCSTR lpszAppName,
1426 LPCSTR lpszKeyName, LPSTR lpszString, WORD cbString,
1427 WORD *pcbString)
1429 clear_errors();
1430 FIXME("%s %s %s %s %d %p\n", debugstr_a(lpszFileName), debugstr_a(lpszAppName),
1431 debugstr_a(lpszKeyName), debugstr_a(lpszString), cbString, pcbString);
1432 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1433 return FALSE;
1436 BOOL WINAPI SQLRemoveDefaultDataSource(void)
1438 clear_errors();
1439 FIXME("\n");
1440 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1441 return FALSE;
1444 BOOL WINAPI SQLRemoveDriverW(LPCWSTR drivername, BOOL remove_dsn, LPDWORD usage_count)
1446 HKEY hkey;
1447 DWORD usagecount = 1;
1449 clear_errors();
1450 TRACE("%s %d %p\n", debugstr_w(drivername), remove_dsn, usage_count);
1452 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey) == ERROR_SUCCESS)
1454 HKEY hkeydriver;
1456 if (RegOpenKeyW(hkey, drivername, &hkeydriver) == ERROR_SUCCESS)
1458 DWORD size, type;
1459 DWORD count;
1461 size = sizeof(usagecount);
1462 RegGetValueA(hkeydriver, NULL, "UsageCount", RRF_RT_DWORD, &type, &usagecount, &size);
1463 TRACE("Usage count %ld\n", usagecount);
1464 count = usagecount - 1;
1465 if (count)
1467 if (RegSetValueExA(hkeydriver, "UsageCount", 0, REG_DWORD, (BYTE*)&count, sizeof(count)) != ERROR_SUCCESS)
1468 ERR("Failed to write registry UsageCount key\n");
1471 RegCloseKey(hkeydriver);
1474 if (usagecount)
1475 usagecount--;
1477 if (!usagecount)
1479 if (RegDeleteKeyW(hkey, drivername) != ERROR_SUCCESS)
1480 ERR("Failed to delete registry key: %s\n", debugstr_w(drivername));
1482 if (RegOpenKeyW(hkey, odbcdrivers, &hkeydriver) == ERROR_SUCCESS)
1484 if(RegDeleteValueW(hkeydriver, drivername) != ERROR_SUCCESS)
1485 ERR("Failed to delete registry value: %s\n", debugstr_w(drivername));
1486 RegCloseKey(hkeydriver);
1490 RegCloseKey(hkey);
1493 if (usage_count)
1494 *usage_count = usagecount;
1496 return TRUE;
1499 BOOL WINAPI SQLRemoveDriver(LPCSTR lpszDriver, BOOL fRemoveDSN,
1500 LPDWORD lpdwUsageCount)
1502 WCHAR *driver;
1503 BOOL ret;
1505 clear_errors();
1506 TRACE("%s %d %p\n", debugstr_a(lpszDriver), fRemoveDSN, lpdwUsageCount);
1508 driver = SQLInstall_strdup(lpszDriver);
1510 ret = SQLRemoveDriverW(driver, fRemoveDSN, lpdwUsageCount);
1512 HeapFree(GetProcessHeap(), 0, driver);
1513 return ret;
1516 BOOL WINAPI SQLRemoveDriverManager(LPDWORD pdwUsageCount)
1518 clear_errors();
1519 FIXME("%p\n", pdwUsageCount);
1520 if (pdwUsageCount) *pdwUsageCount = 1;
1521 return TRUE;
1524 BOOL WINAPI SQLRemoveDSNFromIniW(LPCWSTR lpszDSN)
1526 HKEY hkey;
1528 TRACE("%s\n", debugstr_w(lpszDSN));
1530 clear_errors();
1532 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, L"Software\\ODBC\\ODBC.INI\\ODBC Data Sources", &hkey) == ERROR_SUCCESS)
1534 RegDeleteValueW(hkey, lpszDSN);
1535 RegCloseKey(hkey);
1538 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, L"Software\\ODBC\\ODBC.INI", &hkey) == ERROR_SUCCESS)
1540 RegDeleteTreeW(hkey, lpszDSN);
1541 RegCloseKey(hkey);
1544 return TRUE;
1547 BOOL WINAPI SQLRemoveDSNFromIni(LPCSTR lpszDSN)
1549 BOOL ret = FALSE;
1550 WCHAR *dsn;
1552 TRACE("%s\n", debugstr_a(lpszDSN));
1554 clear_errors();
1556 dsn = SQLInstall_strdup(lpszDSN);
1557 if (dsn)
1558 ret = SQLRemoveDSNFromIniW(dsn);
1559 else
1560 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
1562 heap_free(dsn);
1564 return ret;
1567 BOOL WINAPI SQLRemoveTranslatorW(const WCHAR *translator, DWORD *usage_count)
1569 HKEY hkey;
1570 DWORD usagecount = 1;
1571 BOOL ret = TRUE;
1573 clear_errors();
1574 TRACE("%s %p\n", debugstr_w(translator), usage_count);
1576 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey) == ERROR_SUCCESS)
1578 HKEY hkeydriver;
1580 if (RegOpenKeyW(hkey, translator, &hkeydriver) == ERROR_SUCCESS)
1582 DWORD size, type;
1583 DWORD count;
1585 size = sizeof(usagecount);
1586 RegGetValueA(hkeydriver, NULL, "UsageCount", RRF_RT_DWORD, &type, &usagecount, &size);
1587 TRACE("Usage count %ld\n", usagecount);
1588 count = usagecount - 1;
1589 if (count)
1591 if (RegSetValueExA(hkeydriver, "UsageCount", 0, REG_DWORD, (BYTE*)&count, sizeof(count)) != ERROR_SUCCESS)
1592 ERR("Failed to write registry UsageCount key\n");
1595 RegCloseKey(hkeydriver);
1598 if (usagecount)
1599 usagecount--;
1601 if (!usagecount)
1603 if(RegDeleteKeyW(hkey, translator) != ERROR_SUCCESS)
1605 push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
1606 WARN("Failed to delete registry key: %s\n", debugstr_w(translator));
1607 ret = FALSE;
1610 if (ret && RegOpenKeyW(hkey, odbctranslators, &hkeydriver) == ERROR_SUCCESS)
1612 if(RegDeleteValueW(hkeydriver, translator) != ERROR_SUCCESS)
1614 push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
1615 WARN("Failed to delete registry key: %s\n", debugstr_w(translator));
1616 ret = FALSE;
1619 RegCloseKey(hkeydriver);
1623 RegCloseKey(hkey);
1626 if (ret && usage_count)
1627 *usage_count = usagecount;
1629 return ret;
1632 BOOL WINAPI SQLRemoveTranslator(LPCSTR lpszTranslator, LPDWORD lpdwUsageCount)
1634 WCHAR *translator;
1635 BOOL ret;
1637 clear_errors();
1638 TRACE("%s %p\n", debugstr_a(lpszTranslator), lpdwUsageCount);
1640 translator = SQLInstall_strdup(lpszTranslator);
1641 ret = SQLRemoveTranslatorW(translator, lpdwUsageCount);
1643 HeapFree(GetProcessHeap(), 0, translator);
1644 return ret;
1647 BOOL WINAPI SQLSetConfigMode(UWORD wConfigMode)
1649 clear_errors();
1650 TRACE("%u\n", wConfigMode);
1652 if (wConfigMode > ODBC_SYSTEM_DSN)
1654 push_error(ODBC_ERROR_INVALID_PARAM_SEQUENCE, odbc_error_invalid_param_sequence);
1655 return FALSE;
1657 else
1659 config_mode = wConfigMode;
1660 return TRUE;
1664 BOOL WINAPI SQLValidDSNW(LPCWSTR lpszDSN)
1666 static const WCHAR invalid[] = {'[',']','{','}','(',')',',',';','?','*','=','!','@','\\',0};
1667 clear_errors();
1668 TRACE("%s\n", debugstr_w(lpszDSN));
1670 if(lstrlenW(lpszDSN) > SQL_MAX_DSN_LENGTH || wcspbrk(lpszDSN, invalid) != NULL)
1672 return FALSE;
1675 return TRUE;
1678 BOOL WINAPI SQLValidDSN(LPCSTR lpszDSN)
1680 static const char *invalid = "[]{}(),;?*=!@\\";
1681 clear_errors();
1682 TRACE("%s\n", debugstr_a(lpszDSN));
1684 if(strlen(lpszDSN) > SQL_MAX_DSN_LENGTH || strpbrk(lpszDSN, invalid) != NULL)
1686 return FALSE;
1689 return TRUE;
1692 BOOL WINAPI SQLWriteDSNToIniW(LPCWSTR lpszDSN, LPCWSTR lpszDriver)
1694 DWORD ret;
1695 HKEY hkey, hkeydriver;
1696 WCHAR filename[MAX_PATH];
1698 TRACE("%s %s\n", debugstr_w(lpszDSN), debugstr_w(lpszDriver));
1700 clear_errors();
1702 if (!SQLValidDSNW(lpszDSN))
1704 push_error(ODBC_ERROR_INVALID_DSN, odbc_error_invalid_dsn);
1705 return FALSE;
1708 /* It doesn't matter if we cannot find the driver, windows just writes a blank value. */
1709 filename[0] = 0;
1710 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey) == ERROR_SUCCESS)
1712 HKEY hkeydriver;
1714 if (RegOpenKeyW(hkey, lpszDriver, &hkeydriver) == ERROR_SUCCESS)
1716 DWORD size = MAX_PATH * sizeof(WCHAR);
1717 RegGetValueW(hkeydriver, NULL, L"driver", RRF_RT_REG_SZ, NULL, filename, &size);
1718 RegCloseKey(hkeydriver);
1720 RegCloseKey(hkey);
1723 if ((ret = RegCreateKeyW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\ODBC\\ODBC.INI", &hkey)) == ERROR_SUCCESS)
1725 HKEY sources;
1727 if ((ret = RegCreateKeyW(hkey, L"ODBC Data Sources", &sources)) == ERROR_SUCCESS)
1729 RegSetValueExW(sources, lpszDSN, 0, REG_SZ, (BYTE*)lpszDriver, (lstrlenW(lpszDriver)+1)*sizeof(WCHAR));
1730 RegCloseKey(sources);
1732 RegDeleteTreeW(hkey, lpszDSN);
1733 if ((ret = RegCreateKeyW(hkey, lpszDSN, &hkeydriver)) == ERROR_SUCCESS)
1735 RegSetValueExW(sources, L"driver", 0, REG_SZ, (BYTE*)filename, (lstrlenW(filename)+1)*sizeof(WCHAR));
1736 RegCloseKey(hkeydriver);
1739 RegCloseKey(hkey);
1742 if (ret != ERROR_SUCCESS)
1743 push_error(ODBC_ERROR_REQUEST_FAILED, odbc_error_request_failed);
1745 return ret == ERROR_SUCCESS;
1748 BOOL WINAPI SQLWriteDSNToIni(LPCSTR lpszDSN, LPCSTR lpszDriver)
1750 BOOL ret = FALSE;
1751 WCHAR *dsn, *driver;
1753 TRACE("%s %s\n", debugstr_a(lpszDSN), debugstr_a(lpszDriver));
1755 dsn = SQLInstall_strdup(lpszDSN);
1756 driver = SQLInstall_strdup(lpszDriver);
1757 if (dsn && driver)
1758 ret = SQLWriteDSNToIniW(dsn, driver);
1759 else
1760 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
1762 heap_free(dsn);
1763 heap_free(driver);
1765 return ret;
1768 BOOL WINAPI SQLWriteFileDSNW(LPCWSTR lpszFileName, LPCWSTR lpszAppName,
1769 LPCWSTR lpszKeyName, LPCWSTR lpszString)
1771 clear_errors();
1772 FIXME("%s %s %s %s\n", debugstr_w(lpszFileName), debugstr_w(lpszAppName),
1773 debugstr_w(lpszKeyName), debugstr_w(lpszString));
1774 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1775 return FALSE;
1778 BOOL WINAPI SQLWriteFileDSN(LPCSTR lpszFileName, LPCSTR lpszAppName,
1779 LPCSTR lpszKeyName, LPCSTR lpszString)
1781 clear_errors();
1782 FIXME("%s %s %s %s\n", debugstr_a(lpszFileName), debugstr_a(lpszAppName),
1783 debugstr_a(lpszKeyName), debugstr_a(lpszString));
1784 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1785 return FALSE;
1788 BOOL WINAPI SQLWritePrivateProfileStringW(LPCWSTR lpszSection, LPCWSTR lpszEntry,
1789 LPCWSTR lpszString, LPCWSTR lpszFilename)
1791 static const WCHAR empty[] = {0};
1792 LONG ret;
1793 HKEY hkey;
1795 clear_errors();
1796 TRACE("%s %s %s %s\n", debugstr_w(lpszSection), debugstr_w(lpszEntry),
1797 debugstr_w(lpszString), debugstr_w(lpszFilename));
1799 if(!lpszFilename || !*lpszFilename)
1801 push_error(ODBC_ERROR_INVALID_STR, odbc_error_invalid_param_string);
1802 return FALSE;
1805 if ((ret = RegCreateKeyW(HKEY_CURRENT_USER, odbcW, &hkey)) == ERROR_SUCCESS)
1807 HKEY hkeyfilename;
1809 if ((ret = RegCreateKeyW(hkey, lpszFilename, &hkeyfilename)) == ERROR_SUCCESS)
1811 HKEY hkey_section;
1813 if ((ret = RegCreateKeyW(hkeyfilename, lpszSection, &hkey_section)) == ERROR_SUCCESS)
1815 if(lpszString)
1816 ret = RegSetValueExW(hkey_section, lpszEntry, 0, REG_SZ, (BYTE*)lpszString, (lstrlenW(lpszString)+1)*sizeof(WCHAR));
1817 else
1818 ret = RegSetValueExW(hkey_section, lpszEntry, 0, REG_SZ, (BYTE*)empty, sizeof(empty));
1819 RegCloseKey(hkey_section);
1822 RegCloseKey(hkeyfilename);
1825 RegCloseKey(hkey);
1828 return ret == ERROR_SUCCESS;
1831 BOOL WINAPI SQLWritePrivateProfileString(LPCSTR lpszSection, LPCSTR lpszEntry,
1832 LPCSTR lpszString, LPCSTR lpszFilename)
1834 BOOL ret;
1835 WCHAR *sect, *entry, *string, *file;
1836 clear_errors();
1837 TRACE("%s %s %s %s\n", lpszSection, lpszEntry, lpszString, lpszFilename);
1839 sect = heap_strdupAtoW(lpszSection);
1840 entry = heap_strdupAtoW(lpszEntry);
1841 string = heap_strdupAtoW(lpszString);
1842 file = heap_strdupAtoW(lpszFilename);
1844 ret = SQLWritePrivateProfileStringW(sect, entry, string, file);
1846 heap_free(sect);
1847 heap_free(entry);
1848 heap_free(string);
1849 heap_free(file);
1851 return ret;