ntdll: Add ACTCTX field limit checks to RtlCreateActivationContext().
[wine.git] / dlls / odbccp32 / odbccp32.c
blob90875121438dbb0003bdade79b3b09fcde500c13
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"
35 #include "odbcinst.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(odbc);
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 static const WCHAR odbcW[] = {'S','o','f','t','w','a','r','e','\\','O','D','B','C',0};
42 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};
43 static const WCHAR odbcdrivers[] = {'O','D','B','C',' ','D','r','i','v','e','r','s',0};
44 static const WCHAR odbctranslators[] = {'O','D','B','C',' ','T','r','a','n','s','l','a','t','o','r','s',0};
46 /* This config mode is known to be process-wide.
47 * MSDN documentation suggests that the value is hidden somewhere in the registry but I haven't found it yet.
48 * Although both the registry and the ODBC.ini files appear to be maintained together they are not maintained automatically through the registry's IniFileMapping.
50 static UWORD config_mode = ODBC_BOTH_DSN;
52 /* MSDN documentation suggests that the error subsystem handles errors 1 to 8
53 * only and experimentation (Windows 2000) shows that the errors are process-
54 * wide so go for the simple solution; static arrays.
56 static int num_errors;
57 static int error_code[8];
58 static const WCHAR *error_msg[8];
59 static const WCHAR odbc_error_general_err[] = {'G','e','n','e','r','a','l',' ','e','r','r','o','r',0};
60 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};
61 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};
62 static const WCHAR odbc_error_out_of_mem[] = {'O','u','t',' ','o','f',' ','m','e','m','o','r','y',0};
63 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};
64 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};
65 static const WCHAR odbc_error_invalid_dsn[] = {'I','n','v','a','l','i','d',' ','D','S','N',0};
66 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};
67 static const WCHAR odbc_error_request_failed[] = {'R','e','q','u','e','s','t',' ','F','a','i','l','e','d',0};
68 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};
70 static BOOL (WINAPI *pConfigDSN)(HWND hwnd, WORD request, const char *driver, const char *attr);
71 static BOOL (WINAPI *pConfigDSNW)(HWND hwnd, WORD request, const WCHAR *driver, const WCHAR *attr);
73 /* Push an error onto the error stack, taking care of ranges etc. */
74 static void push_error(int code, LPCWSTR msg)
76 if (num_errors < ARRAY_SIZE(error_code))
78 error_code[num_errors] = code;
79 error_msg[num_errors] = msg;
80 num_errors++;
84 /* Clear the error stack */
85 static void clear_errors(void)
87 num_errors = 0;
90 static inline WCHAR *strdupAtoW(const char *str)
92 LPWSTR ret = NULL;
94 if(str) {
95 DWORD len;
97 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
98 ret = malloc(len * sizeof(WCHAR));
99 if(ret)
100 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
103 return ret;
107 BOOL WINAPI ODBCCPlApplet( LONG i, LONG j, LONG * p1, LONG * p2)
109 clear_errors();
110 FIXME( "( %ld %ld %p %p) : stub!\n", i, j, p1, p2);
111 return FALSE;
114 static LPWSTR SQLInstall_strdup_multi(LPCSTR str)
116 LPCSTR p;
117 LPWSTR ret = NULL;
118 DWORD len;
120 if (!str)
121 return ret;
123 for (p = str; *p; p += lstrlenA(p) + 1)
126 len = MultiByteToWideChar(CP_ACP, 0, str, p - str, NULL, 0 );
127 ret = malloc((len + 1) * sizeof(WCHAR));
128 MultiByteToWideChar(CP_ACP, 0, str, p - str, ret, len );
129 ret[len] = 0;
131 return ret;
134 static LPWSTR SQLInstall_strdup(LPCSTR str)
136 DWORD len;
137 LPWSTR ret = NULL;
139 if (!str)
140 return ret;
142 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0 );
143 ret = malloc(len * sizeof(WCHAR));
144 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len );
146 return ret;
149 /* Convert the wide string or zero-length-terminated list of wide strings to a
150 * narrow string or zero-length-terminated list of narrow strings.
151 * Do not try to emulate windows undocumented excesses (e.g. adding a third \0
152 * to a list)
153 * Arguments
154 * mode Indicates the sort of string.
155 * 1 denotes that the buffers contain strings terminated by a single nul
156 * character
157 * 2 denotes that the buffers contain zero-length-terminated lists
158 * (frequently erroneously referred to as double-null-terminated)
159 * buffer The narrow-character buffer into which to place the result. This
160 * must be a non-null pointer to the first element of a buffer whose
161 * length is passed in buffer_length.
162 * str The wide-character buffer containing the string or list of strings to
163 * be converted. str_length defines how many wide characters in the
164 * buffer are to be converted, including all desired terminating nul
165 * characters.
166 * str_length Effective length of str
167 * buffer_length Length of buffer
168 * returned_length A pointer to a variable that will receive the number of
169 * narrow characters placed into the buffer. This pointer
170 * may be NULL.
172 static BOOL SQLInstall_narrow(int mode, LPSTR buffer, LPCWSTR str, WORD str_length, WORD buffer_length, WORD *returned_length)
174 LPSTR pbuf; /* allows us to allocate a temporary buffer only if needed */
175 int len; /* Length of the converted list */
176 BOOL success = FALSE;
177 assert(mode == 1 || mode == 2);
178 assert(buffer_length);
179 len = WideCharToMultiByte(CP_ACP, 0, str, str_length, 0, 0, NULL, NULL);
180 if (len > 0)
182 if (len > buffer_length)
184 pbuf = malloc(len);
186 else
188 pbuf = buffer;
190 len = WideCharToMultiByte(CP_ACP, 0, str, str_length, pbuf, len, NULL, NULL);
191 if (len > 0)
193 if (pbuf != buffer)
195 if (buffer_length > (mode - 1))
197 memcpy (buffer, pbuf, buffer_length-mode);
198 *(buffer+buffer_length-mode) = '\0';
200 *(buffer+buffer_length-1) = '\0';
202 if (returned_length)
204 *returned_length = pbuf == buffer ? len : buffer_length;
206 success = TRUE;
208 else
210 ERR("transferring wide to narrow\n");
212 if (pbuf != buffer)
214 free(pbuf);
217 else
219 ERR("measuring wide to narrow\n");
221 return success;
224 static HMODULE load_config_driver(const WCHAR *driver)
226 static WCHAR reg_driver[] = {'d','r','i','v','e','r',0};
227 long ret;
228 HMODULE hmod;
229 WCHAR *filename = NULL;
230 DWORD size = 0, type;
231 HKEY hkey;
233 if ((ret = RegOpenKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey)) == ERROR_SUCCESS)
235 HKEY hkeydriver;
237 if ((ret = RegOpenKeyW(hkey, driver, &hkeydriver)) == ERROR_SUCCESS)
239 ret = RegGetValueW(hkeydriver, NULL, reg_driver, RRF_RT_REG_SZ, &type, NULL, &size);
240 if(ret != ERROR_SUCCESS || type != REG_SZ)
242 RegCloseKey(hkeydriver);
243 RegCloseKey(hkey);
244 push_error(ODBC_ERROR_INVALID_DSN, odbc_error_invalid_dsn);
246 return NULL;
249 filename = malloc(size);
250 if(!filename)
252 RegCloseKey(hkeydriver);
253 RegCloseKey(hkey);
254 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
256 return NULL;
258 ret = RegGetValueW(hkeydriver, NULL, reg_driver, RRF_RT_REG_SZ, &type, filename, &size);
260 RegCloseKey(hkeydriver);
263 RegCloseKey(hkey);
266 if(ret != ERROR_SUCCESS)
268 free(filename);
269 push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
270 return NULL;
273 hmod = LoadLibraryExW(filename, NULL, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
274 free(filename);
276 if(!hmod)
277 push_error(ODBC_ERROR_LOAD_LIB_FAILED, odbc_error_load_lib_failed);
279 return hmod;
282 static BOOL write_config_value(const WCHAR *driver, const WCHAR *args)
284 long ret;
285 HKEY hkey, hkeydriver;
286 WCHAR *name = NULL;
288 if(!args)
289 return FALSE;
291 if((ret = RegOpenKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey)) == ERROR_SUCCESS)
293 if((ret = RegOpenKeyW(hkey, driver, &hkeydriver)) == ERROR_SUCCESS)
295 WCHAR *divider, *value;
297 name = malloc((wcslen(args) + 1) * sizeof(WCHAR));
298 if(!name)
300 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
301 goto fail;
303 lstrcpyW(name, args);
305 divider = wcschr(name,'=');
306 if(!divider)
308 push_error(ODBC_ERROR_INVALID_KEYWORD_VALUE, odbc_error_invalid_keyword);
309 goto fail;
312 value = divider + 1;
313 *divider = '\0';
315 TRACE("Write pair: %s = %s\n", debugstr_w(name), debugstr_w(value));
316 if(RegSetValueExW(hkeydriver, name, 0, REG_SZ, (BYTE*)value,
317 (lstrlenW(value)+1) * sizeof(WCHAR)) != ERROR_SUCCESS)
318 ERR("Failed to write registry installed key\n");
319 free(name);
321 RegCloseKey(hkeydriver);
324 RegCloseKey(hkey);
327 if(ret != ERROR_SUCCESS)
328 push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
330 return ret == ERROR_SUCCESS;
332 fail:
333 RegCloseKey(hkeydriver);
334 RegCloseKey(hkey);
335 free(name);
337 return FALSE;
340 static WORD map_request(WORD request)
342 switch (request)
344 case ODBC_ADD_DSN:
345 case ODBC_ADD_SYS_DSN:
346 return ODBC_ADD_DSN;
348 case ODBC_CONFIG_DSN:
349 case ODBC_CONFIG_SYS_DSN:
350 return ODBC_CONFIG_DSN;
352 case ODBC_REMOVE_DSN:
353 case ODBC_REMOVE_SYS_DSN:
354 return ODBC_REMOVE_DSN;
356 default:
357 FIXME("unhandled request %u\n", request);
358 return 0;
362 static UWORD get_config_mode(WORD request)
364 if (request == ODBC_ADD_DSN || request == ODBC_CONFIG_DSN || request == ODBC_REMOVE_DSN) return ODBC_USER_DSN;
365 return ODBC_SYSTEM_DSN;
368 BOOL WINAPI SQLConfigDataSourceW(HWND hwnd, WORD request, LPCWSTR driver, LPCWSTR attributes)
370 HMODULE mod;
371 BOOL ret = FALSE;
372 UWORD config_mode_prev = config_mode;
373 WORD mapped_request;
375 TRACE("%p, %d, %s, %s\n", hwnd, request, debugstr_w(driver), debugstr_w(attributes));
376 if (TRACE_ON(odbc))
378 const WCHAR *p;
379 for (p = attributes; *p; p += lstrlenW(p) + 1)
380 TRACE("%s\n", debugstr_w(p));
383 clear_errors();
385 mapped_request = map_request(request);
386 if (!mapped_request)
387 return FALSE;
389 mod = load_config_driver(driver);
390 if (!mod)
391 return FALSE;
393 config_mode = get_config_mode(request);
395 pConfigDSNW = (void*)GetProcAddress(mod, "ConfigDSNW");
396 if(pConfigDSNW)
397 ret = pConfigDSNW(hwnd, mapped_request, driver, attributes);
398 else
399 ERR("Failed to find ConfigDSNW\n");
401 config_mode = config_mode_prev;
403 if (!ret)
404 push_error(ODBC_ERROR_REQUEST_FAILED, odbc_error_request_failed);
406 FreeLibrary(mod);
408 return ret;
411 BOOL WINAPI SQLConfigDataSource(HWND hwnd, WORD request, LPCSTR driver, LPCSTR attributes)
413 HMODULE mod;
414 BOOL ret = FALSE;
415 WCHAR *driverW;
416 UWORD config_mode_prev = config_mode;
417 WORD mapped_request;
419 TRACE("%p, %d, %s, %s\n", hwnd, request, debugstr_a(driver), debugstr_a(attributes));
421 if (TRACE_ON(odbc))
423 const char *p;
424 for (p = attributes; *p; p += lstrlenA(p) + 1)
425 TRACE("%s\n", debugstr_a(p));
428 clear_errors();
430 mapped_request = map_request(request);
431 if (!mapped_request)
432 return FALSE;
434 driverW = strdupAtoW(driver);
435 if (!driverW)
437 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
438 return FALSE;
441 mod = load_config_driver(driverW);
442 if (!mod)
444 free(driverW);
445 return FALSE;
448 config_mode = get_config_mode(request);
450 pConfigDSN = (void*)GetProcAddress(mod, "ConfigDSN");
451 if (pConfigDSN)
453 TRACE("Calling ConfigDSN\n");
454 ret = pConfigDSN(hwnd, mapped_request, driver, attributes);
456 else
458 pConfigDSNW = (void*)GetProcAddress(mod, "ConfigDSNW");
459 if (pConfigDSNW)
461 WCHAR *attr = NULL;
462 TRACE("Calling ConfigDSNW\n");
464 attr = SQLInstall_strdup_multi(attributes);
465 if(attr)
466 ret = pConfigDSNW(hwnd, mapped_request, driverW, attr);
467 free(attr);
471 config_mode = config_mode_prev;
473 if (!ret)
474 push_error(ODBC_ERROR_REQUEST_FAILED, odbc_error_request_failed);
476 free(driverW);
477 FreeLibrary(mod);
479 return ret;
482 BOOL WINAPI SQLConfigDriverW(HWND hwnd, WORD request, LPCWSTR driver,
483 LPCWSTR args, LPWSTR msg, WORD msgmax, WORD *msgout)
485 BOOL (WINAPI *pConfigDriverW)(HWND hwnd, WORD request, const WCHAR *driver, const WCHAR *args, const WCHAR *msg, WORD msgmax, WORD *msgout);
486 HMODULE hmod;
487 BOOL funcret = FALSE;
489 clear_errors();
490 TRACE("(%p %d %s %s %p %d %p)\n", hwnd, request, debugstr_w(driver),
491 debugstr_w(args), msg, msgmax, msgout);
493 if(request == ODBC_CONFIG_DRIVER)
495 return write_config_value(driver, args);
498 hmod = load_config_driver(driver);
499 if(!hmod)
500 return FALSE;
502 pConfigDriverW = (void*)GetProcAddress(hmod, "ConfigDriverW");
503 if(pConfigDriverW)
504 funcret = pConfigDriverW(hwnd, request, driver, args, msg, msgmax, msgout);
506 if(!funcret)
507 push_error(ODBC_ERROR_REQUEST_FAILED, odbc_error_request_failed);
509 FreeLibrary(hmod);
511 return funcret;
514 BOOL WINAPI SQLConfigDriver(HWND hwnd, WORD request, LPCSTR driver,
515 LPCSTR args, LPSTR msg, WORD msgmax, WORD *msgout)
517 BOOL (WINAPI *pConfigDriverA)(HWND hwnd, WORD request, const char *driver, const char *args, const char *msg, WORD msgmax, WORD *msgout);
518 HMODULE hmod;
519 WCHAR *driverW;
520 BOOL funcret = FALSE;
522 clear_errors();
523 TRACE("(%p %d %s %s %p %d %p)\n", hwnd, request, debugstr_a(driver),
524 debugstr_a(args), msg, msgmax, msgout);
526 driverW = strdupAtoW(driver);
527 if(!driverW)
529 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
530 return FALSE;
532 if(request == ODBC_CONFIG_DRIVER)
534 BOOL ret = FALSE;
535 WCHAR *argsW = strdupAtoW(args);
536 if(argsW)
538 ret = write_config_value(driverW, argsW);
539 free(argsW);
541 else
543 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
546 free(driverW);
548 return ret;
551 hmod = load_config_driver(driverW);
552 free(driverW);
553 if(!hmod)
554 return FALSE;
556 pConfigDriverA = (void*)GetProcAddress(hmod, "ConfigDriver");
557 if(pConfigDriverA)
558 funcret = pConfigDriverA(hwnd, request, driver, args, msg, msgmax, msgout);
560 if(!funcret)
561 push_error(ODBC_ERROR_REQUEST_FAILED, odbc_error_request_failed);
563 FreeLibrary(hmod);
565 return funcret;
568 BOOL WINAPI SQLCreateDataSourceW(HWND hwnd, LPCWSTR lpszDS)
570 clear_errors();
571 FIXME("%p %s\n", hwnd, debugstr_w(lpszDS));
572 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
573 return FALSE;
576 BOOL WINAPI SQLCreateDataSource(HWND hwnd, LPCSTR lpszDS)
578 clear_errors();
579 FIXME("%p %s\n", hwnd, debugstr_a(lpszDS));
580 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
581 return FALSE;
584 BOOL WINAPI SQLGetAvailableDriversW(LPCWSTR lpszInfFile, LPWSTR lpszBuf,
585 WORD cbBufMax, WORD *pcbBufOut)
587 clear_errors();
588 FIXME("%s %p %d %p\n", debugstr_w(lpszInfFile), lpszBuf, cbBufMax, pcbBufOut);
589 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
590 return FALSE;
593 BOOL WINAPI SQLGetAvailableDrivers(LPCSTR lpszInfFile, LPSTR lpszBuf,
594 WORD cbBufMax, WORD *pcbBufOut)
596 clear_errors();
597 FIXME("%s %p %d %p\n", debugstr_a(lpszInfFile), lpszBuf, cbBufMax, pcbBufOut);
598 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
599 return FALSE;
602 BOOL WINAPI SQLGetConfigMode(UWORD *pwConfigMode)
604 clear_errors();
605 TRACE("%p\n", pwConfigMode);
606 if (pwConfigMode)
607 *pwConfigMode = config_mode;
608 return TRUE;
611 BOOL WINAPI SQLGetInstalledDriversW(WCHAR *buf, WORD size, WORD *sizeout)
613 WORD written = 0;
614 DWORD index = 0;
615 BOOL ret = TRUE;
616 DWORD valuelen;
617 WCHAR *value;
618 HKEY drivers;
619 DWORD len;
620 LONG res;
622 clear_errors();
624 TRACE("%p %d %p\n", buf, size, sizeout);
626 if (!buf || !size)
628 push_error(ODBC_ERROR_INVALID_BUFF_LEN, odbc_error_invalid_buff_len);
629 return FALSE;
632 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, drivers_key, 0, KEY_QUERY_VALUE, &drivers);
633 if (res)
635 push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
636 return FALSE;
639 valuelen = 256;
640 value = malloc(valuelen * sizeof(WCHAR));
642 size--;
644 while (1)
646 len = valuelen;
647 res = RegEnumValueW(drivers, index, value, &len, NULL, NULL, NULL, NULL);
648 while (res == ERROR_MORE_DATA)
650 value = realloc(value, ++len * sizeof(WCHAR));
651 res = RegEnumValueW(drivers, index, value, &len, NULL, NULL, NULL, NULL);
653 if (res == ERROR_SUCCESS)
655 lstrcpynW(buf + written, value, size - written);
656 written += min(len + 1, size - written);
658 else if (res == ERROR_NO_MORE_ITEMS)
659 break;
660 else
662 push_error(ODBC_ERROR_GENERAL_ERR, odbc_error_general_err);
663 ret = FALSE;
664 break;
666 index++;
669 buf[written++] = 0;
671 free(value);
672 RegCloseKey(drivers);
673 if (sizeout)
674 *sizeout = written;
675 return ret;
678 BOOL WINAPI SQLGetInstalledDrivers(char *buf, WORD size, WORD *sizeout)
680 WORD written;
681 WCHAR *wbuf;
682 BOOL ret;
684 TRACE("%p %d %p\n", buf, size, sizeout);
686 if (!buf || !size)
688 push_error(ODBC_ERROR_INVALID_BUFF_LEN, odbc_error_invalid_buff_len);
689 return FALSE;
692 wbuf = malloc(size * sizeof(WCHAR));
693 if (!wbuf)
695 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
696 return FALSE;
699 ret = SQLGetInstalledDriversW(wbuf, size, &written);
700 if (!ret)
702 free(wbuf);
703 return FALSE;
706 if (sizeout)
707 *sizeout = WideCharToMultiByte(CP_ACP, 0, wbuf, written, NULL, 0, NULL, NULL);
708 WideCharToMultiByte(CP_ACP, 0, wbuf, written, buf, size, NULL, NULL);
710 free(wbuf);
711 return TRUE;
714 static HKEY get_privateprofile_sectionkey(LPCWSTR section, LPCWSTR filename)
716 HKEY hkey, hkeyfilename, hkeysection;
717 LONG ret;
719 if (RegOpenKeyW(HKEY_CURRENT_USER, odbcW, &hkey))
720 return NULL;
722 ret = RegOpenKeyW(hkey, filename, &hkeyfilename);
723 RegCloseKey(hkey);
724 if (ret)
725 return NULL;
727 ret = RegOpenKeyW(hkeyfilename, section, &hkeysection);
728 RegCloseKey(hkeyfilename);
730 return ret ? NULL : hkeysection;
733 int WINAPI SQLGetPrivateProfileStringW(LPCWSTR section, LPCWSTR entry,
734 LPCWSTR defvalue, LPWSTR buff, int buff_len, LPCWSTR filename)
736 BOOL usedefault = TRUE;
737 HKEY sectionkey;
738 LONG ret = 0;
740 TRACE("%s %s %s %p %d %s\n", debugstr_w(section), debugstr_w(entry),
741 debugstr_w(defvalue), buff, buff_len, debugstr_w(filename));
743 clear_errors();
745 if (buff_len <= 0 || !section)
746 return 0;
748 if(buff)
749 buff[0] = 0;
751 if (!defvalue || !buff)
752 return 0;
754 sectionkey = get_privateprofile_sectionkey(section, filename);
755 if (sectionkey)
757 DWORD type, size;
759 if (entry)
761 size = buff_len * sizeof(*buff);
762 if (RegGetValueW(sectionkey, NULL, entry, RRF_RT_REG_SZ, &type, buff, &size) == ERROR_SUCCESS)
764 usedefault = FALSE;
765 ret = (size / sizeof(*buff)) - 1;
768 else
770 WCHAR name[MAX_PATH];
771 DWORD index = 0;
772 DWORD namelen;
774 usedefault = FALSE;
776 memset(buff, 0, buff_len);
778 namelen = sizeof(name);
779 while (RegEnumValueW(sectionkey, index, name, &namelen, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
781 if ((ret + namelen+1) > buff_len)
782 break;
784 lstrcpyW(buff+ret, name);
785 ret += namelen+1;
786 namelen = sizeof(name);
787 index++;
791 RegCloseKey(sectionkey);
793 else
794 usedefault = entry != NULL;
796 if (usedefault)
798 lstrcpynW(buff, defvalue, buff_len);
799 ret = lstrlenW(buff);
802 return ret;
805 int WINAPI SQLGetPrivateProfileString(LPCSTR section, LPCSTR entry,
806 LPCSTR defvalue, LPSTR buff, int buff_len, LPCSTR filename)
808 WCHAR *sectionW, *filenameW;
809 BOOL usedefault = TRUE;
810 HKEY sectionkey;
811 LONG ret = 0;
813 TRACE("%s %s %s %p %d %s\n", debugstr_a(section), debugstr_a(entry),
814 debugstr_a(defvalue), buff, buff_len, debugstr_a(filename));
816 clear_errors();
818 if (buff_len <= 0)
819 return 0;
821 if (buff)
822 buff[0] = 0;
824 if (!section || !defvalue || !buff)
825 return 0;
827 sectionW = strdupAtoW(section);
828 filenameW = strdupAtoW(filename);
830 sectionkey = get_privateprofile_sectionkey(sectionW, filenameW);
832 free(sectionW);
833 free(filenameW);
835 if (sectionkey)
837 DWORD type, size;
839 if (entry)
841 size = buff_len * sizeof(*buff);
842 if (RegGetValueA(sectionkey, NULL, entry, RRF_RT_REG_SZ, &type, buff, &size) == ERROR_SUCCESS)
844 usedefault = FALSE;
845 ret = (size / sizeof(*buff)) - 1;
848 else
850 char name[MAX_PATH] = {0};
851 DWORD index = 0;
852 DWORD namelen;
854 usedefault = FALSE;
856 memset(buff, 0, buff_len);
858 namelen = sizeof(name);
859 while (RegEnumValueA(sectionkey, index, name, &namelen, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
861 if ((ret + namelen+1) > buff_len)
862 break;
864 lstrcpyA(buff+ret, name);
866 ret += namelen+1;
867 namelen = sizeof(name);
868 index++;
872 RegCloseKey(sectionkey);
874 else
875 usedefault = entry != NULL;
877 if (usedefault)
879 lstrcpynA(buff, defvalue, buff_len);
880 ret = strlen(buff);
883 return ret;
886 BOOL WINAPI SQLGetTranslatorW(HWND hwndParent, LPWSTR lpszName, WORD cbNameMax,
887 WORD *pcbNameOut, LPWSTR lpszPath, WORD cbPathMax,
888 WORD *pcbPathOut, DWORD *pvOption)
890 clear_errors();
891 FIXME("%p %s %d %p %p %d %p %p\n", hwndParent, debugstr_w(lpszName), cbNameMax,
892 pcbNameOut, lpszPath, cbPathMax, pcbPathOut, pvOption);
893 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
894 return FALSE;
897 BOOL WINAPI SQLGetTranslator(HWND hwndParent, LPSTR lpszName, WORD cbNameMax,
898 WORD *pcbNameOut, LPSTR lpszPath, WORD cbPathMax,
899 WORD *pcbPathOut, DWORD *pvOption)
901 clear_errors();
902 FIXME("%p %s %d %p %p %d %p %p\n", hwndParent, debugstr_a(lpszName), cbNameMax,
903 pcbNameOut, lpszPath, cbPathMax, pcbPathOut, pvOption);
904 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
905 return FALSE;
908 BOOL WINAPI SQLInstallDriverW(LPCWSTR lpszInfFile, LPCWSTR lpszDriver,
909 LPWSTR lpszPath, WORD cbPathMax, WORD * pcbPathOut)
911 DWORD usage;
913 clear_errors();
914 TRACE("%s %s %p %d %p\n", debugstr_w(lpszInfFile),
915 debugstr_w(lpszDriver), lpszPath, cbPathMax, pcbPathOut);
917 if (lpszInfFile)
918 return FALSE;
920 return SQLInstallDriverExW(lpszDriver, NULL, lpszPath, cbPathMax,
921 pcbPathOut, ODBC_INSTALL_COMPLETE, &usage);
924 BOOL WINAPI SQLInstallDriver(LPCSTR lpszInfFile, LPCSTR lpszDriver,
925 LPSTR lpszPath, WORD cbPathMax, WORD * pcbPathOut)
927 DWORD usage;
929 clear_errors();
930 TRACE("%s %s %p %d %p\n", debugstr_a(lpszInfFile),
931 debugstr_a(lpszDriver), lpszPath, cbPathMax, pcbPathOut);
933 if (lpszInfFile)
934 return FALSE;
936 return SQLInstallDriverEx(lpszDriver, NULL, lpszPath, cbPathMax,
937 pcbPathOut, ODBC_INSTALL_COMPLETE, &usage);
940 static void write_registry_values(const WCHAR *regkey, const WCHAR *driver, const WCHAR *path_in, WCHAR *path,
941 DWORD *usage_count)
943 static const WCHAR installed[] = {'I','n','s','t','a','l','l','e','d',0};
944 static const WCHAR slash[] = {'\\', 0};
945 static const WCHAR driverW[] = {'D','r','i','v','e','r',0};
946 static const WCHAR setupW[] = {'S','e','t','u','p',0};
947 static const WCHAR translator[] = {'T','r','a','n','s','l','a','t','o','r',0};
948 HKEY hkey, hkeydriver;
950 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey) == ERROR_SUCCESS)
952 if (RegCreateKeyW(hkey, regkey, &hkeydriver) == ERROR_SUCCESS)
954 if(RegSetValueExW(hkeydriver, driver, 0, REG_SZ, (BYTE*)installed, sizeof(installed)) != ERROR_SUCCESS)
955 ERR("Failed to write registry installed key\n");
957 RegCloseKey(hkeydriver);
960 if (RegCreateKeyW(hkey, driver, &hkeydriver) == ERROR_SUCCESS)
962 WCHAR entry[1024];
963 const WCHAR *p;
964 DWORD usagecount = 0;
965 DWORD type, size;
967 /* Skip name entry */
968 p = driver;
969 p += lstrlenW(p) + 1;
971 if (!path_in)
972 GetSystemDirectoryW(path, MAX_PATH);
973 else
974 lstrcpyW(path, path_in);
976 /* Store Usage */
977 size = sizeof(usagecount);
978 RegGetValueA(hkeydriver, NULL, "UsageCount", RRF_RT_DWORD, &type, &usagecount, &size);
979 TRACE("Usage count %ld\n", usagecount);
981 for (; *p; p += lstrlenW(p) + 1)
983 WCHAR *divider = wcschr(p,'=');
985 if (divider)
987 WCHAR *value;
988 int len;
990 /* Write pair values to the registry. */
991 lstrcpynW(entry, p, divider - p + 1);
993 divider++;
994 TRACE("Writing pair %s,%s\n", debugstr_w(entry), debugstr_w(divider));
996 /* Driver, Setup, Translator entries use the system path unless a path is specified. */
997 if(lstrcmpiW(driverW, entry) == 0 || lstrcmpiW(setupW, entry) == 0 ||
998 lstrcmpiW(translator, entry) == 0)
1000 len = lstrlenW(path) + lstrlenW(slash) + lstrlenW(divider) + 1;
1001 value = malloc(len * sizeof(WCHAR));
1002 if(!value)
1004 ERR("Out of memory\n");
1005 return;
1008 lstrcpyW(value, path);
1009 lstrcatW(value, slash);
1010 lstrcatW(value, divider);
1012 else
1014 len = lstrlenW(divider) + 1;
1015 value = malloc(len * sizeof(WCHAR));
1016 lstrcpyW(value, divider);
1019 if (RegSetValueExW(hkeydriver, entry, 0, REG_SZ, (BYTE*)value,
1020 (lstrlenW(value)+1)*sizeof(WCHAR)) != ERROR_SUCCESS)
1021 ERR("Failed to write registry data %s %s\n", debugstr_w(entry), debugstr_w(value));
1022 free(value);
1024 else
1026 ERR("No pair found. %s\n", debugstr_w(p));
1027 break;
1031 /* Set Usage Count */
1032 usagecount++;
1033 if (RegSetValueExA(hkeydriver, "UsageCount", 0, REG_DWORD, (BYTE*)&usagecount, sizeof(usagecount)) != ERROR_SUCCESS)
1034 ERR("Failed to write registry UsageCount key\n");
1036 if (usage_count)
1037 *usage_count = usagecount;
1039 RegCloseKey(hkeydriver);
1042 RegCloseKey(hkey);
1046 BOOL WINAPI SQLInstallDriverExW(LPCWSTR lpszDriver, LPCWSTR lpszPathIn,
1047 LPWSTR lpszPathOut, WORD cbPathOutMax, WORD *pcbPathOut,
1048 WORD fRequest, LPDWORD lpdwUsageCount)
1050 UINT len;
1051 WCHAR path[MAX_PATH];
1053 clear_errors();
1054 TRACE("%s %s %p %d %p %d %p\n", debugstr_w(lpszDriver),
1055 debugstr_w(lpszPathIn), lpszPathOut, cbPathOutMax, pcbPathOut,
1056 fRequest, lpdwUsageCount);
1058 write_registry_values(odbcdrivers, lpszDriver, lpszPathIn, path, lpdwUsageCount);
1060 len = lstrlenW(path);
1062 if (pcbPathOut)
1063 *pcbPathOut = len;
1065 if (lpszPathOut && cbPathOutMax > len)
1067 lstrcpyW(lpszPathOut, path);
1068 return TRUE;
1070 return FALSE;
1073 BOOL WINAPI SQLInstallDriverEx(LPCSTR lpszDriver, LPCSTR lpszPathIn,
1074 LPSTR lpszPathOut, WORD cbPathOutMax, WORD *pcbPathOut,
1075 WORD fRequest, LPDWORD lpdwUsageCount)
1077 LPWSTR driver, pathin;
1078 WCHAR pathout[MAX_PATH];
1079 BOOL ret;
1080 WORD cbOut = 0;
1082 clear_errors();
1083 TRACE("%s %s %p %d %p %d %p\n", debugstr_a(lpszDriver),
1084 debugstr_a(lpszPathIn), lpszPathOut, cbPathOutMax, pcbPathOut,
1085 fRequest, lpdwUsageCount);
1087 driver = SQLInstall_strdup_multi(lpszDriver);
1088 pathin = SQLInstall_strdup(lpszPathIn);
1090 ret = SQLInstallDriverExW(driver, pathin, pathout, MAX_PATH, &cbOut,
1091 fRequest, lpdwUsageCount);
1092 if (ret)
1094 int len = WideCharToMultiByte(CP_ACP, 0, pathout, -1, lpszPathOut,
1095 0, NULL, NULL);
1096 if (len)
1098 if (pcbPathOut)
1099 *pcbPathOut = len - 1;
1101 if (!lpszPathOut || cbPathOutMax < len)
1103 ret = FALSE;
1104 goto out;
1106 len = WideCharToMultiByte(CP_ACP, 0, pathout, -1, lpszPathOut,
1107 cbPathOutMax, NULL, NULL);
1111 out:
1112 free(driver);
1113 free(pathin);
1114 return ret;
1117 BOOL WINAPI SQLInstallDriverManagerW(LPWSTR lpszPath, WORD cbPathMax,
1118 WORD *pcbPathOut)
1120 UINT len;
1121 WCHAR path[MAX_PATH];
1123 TRACE("(%p %d %p)\n", lpszPath, cbPathMax, pcbPathOut);
1125 if (cbPathMax < MAX_PATH)
1126 return FALSE;
1128 clear_errors();
1130 len = GetSystemDirectoryW(path, MAX_PATH);
1132 if (pcbPathOut)
1133 *pcbPathOut = len;
1135 if (lpszPath && cbPathMax > len)
1137 lstrcpyW(lpszPath, path);
1138 return TRUE;
1140 return FALSE;
1143 BOOL WINAPI SQLInstallDriverManager(LPSTR lpszPath, WORD cbPathMax,
1144 WORD *pcbPathOut)
1146 BOOL ret;
1147 WORD len, cbOut = 0;
1148 WCHAR path[MAX_PATH];
1150 TRACE("(%p %d %p)\n", lpszPath, cbPathMax, pcbPathOut);
1152 if (cbPathMax < MAX_PATH)
1153 return FALSE;
1155 clear_errors();
1157 ret = SQLInstallDriverManagerW(path, MAX_PATH, &cbOut);
1158 if (ret)
1160 len = WideCharToMultiByte(CP_ACP, 0, path, -1, lpszPath, 0,
1161 NULL, NULL);
1162 if (len)
1164 if (pcbPathOut)
1165 *pcbPathOut = len - 1;
1167 if (!lpszPath || cbPathMax < len)
1168 return FALSE;
1170 len = WideCharToMultiByte(CP_ACP, 0, path, -1, lpszPath,
1171 cbPathMax, NULL, NULL);
1174 return ret;
1177 BOOL WINAPI SQLInstallODBCW(HWND hwndParent, LPCWSTR lpszInfFile,
1178 LPCWSTR lpszSrcPath, LPCWSTR lpszDrivers)
1180 clear_errors();
1181 FIXME("%p %s %s %s\n", hwndParent, debugstr_w(lpszInfFile),
1182 debugstr_w(lpszSrcPath), debugstr_w(lpszDrivers));
1183 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1184 return FALSE;
1187 BOOL WINAPI SQLInstallODBC(HWND hwndParent, LPCSTR lpszInfFile,
1188 LPCSTR lpszSrcPath, LPCSTR lpszDrivers)
1190 clear_errors();
1191 FIXME("%p %s %s %s\n", hwndParent, debugstr_a(lpszInfFile),
1192 debugstr_a(lpszSrcPath), debugstr_a(lpszDrivers));
1193 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1194 return FALSE;
1197 SQLRETURN WINAPI SQLInstallerErrorW(WORD iError, DWORD *pfErrorCode,
1198 LPWSTR lpszErrorMsg, WORD cbErrorMsgMax, WORD *pcbErrorMsg)
1200 TRACE("%d %p %p %d %p\n", iError, pfErrorCode, lpszErrorMsg,
1201 cbErrorMsgMax, pcbErrorMsg);
1203 if (iError == 0)
1205 return SQL_ERROR;
1207 else if (iError <= num_errors)
1209 BOOL truncated = FALSE;
1210 WORD len;
1211 LPCWSTR msg;
1212 iError--;
1213 if (pfErrorCode)
1214 *pfErrorCode = error_code[iError];
1215 msg = error_msg[iError];
1216 len = msg ? lstrlenW(msg) : 0;
1217 if (pcbErrorMsg)
1218 *pcbErrorMsg = len;
1219 len++;
1220 if (cbErrorMsgMax < len)
1222 len = cbErrorMsgMax;
1223 truncated = TRUE;
1225 if (lpszErrorMsg && len)
1227 if (msg)
1229 memcpy (lpszErrorMsg, msg, len * sizeof(WCHAR));
1231 else
1233 assert(len==1);
1234 *lpszErrorMsg = 0;
1237 else
1239 /* Yes. If you pass a null pointer and a large length it is not an error! */
1240 truncated = TRUE;
1243 return truncated ? SQL_SUCCESS_WITH_INFO : SQL_SUCCESS;
1246 /* 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 */
1247 if (pcbErrorMsg)
1248 *pcbErrorMsg = 0;
1250 if (lpszErrorMsg && cbErrorMsgMax > 0)
1251 *lpszErrorMsg = '\0';
1253 return SQL_NO_DATA;
1256 SQLRETURN WINAPI SQLInstallerError(WORD iError, DWORD *pfErrorCode,
1257 LPSTR lpszErrorMsg, WORD cbErrorMsgMax, WORD *pcbErrorMsg)
1259 SQLRETURN ret;
1260 LPWSTR wbuf;
1261 WORD cbwbuf;
1262 TRACE("%d %p %p %d %p\n", iError, pfErrorCode, lpszErrorMsg,
1263 cbErrorMsgMax, pcbErrorMsg);
1265 wbuf = 0;
1266 if (lpszErrorMsg && cbErrorMsgMax)
1268 wbuf = malloc(cbErrorMsgMax * sizeof(WCHAR));
1269 if (!wbuf)
1270 return SQL_ERROR;
1272 ret = SQLInstallerErrorW(iError, pfErrorCode, wbuf, cbErrorMsgMax, &cbwbuf);
1273 if (wbuf)
1275 WORD cbBuf = 0;
1276 SQLInstall_narrow(1, lpszErrorMsg, wbuf, cbwbuf+1, cbErrorMsgMax, &cbBuf);
1277 free(wbuf);
1278 if (pcbErrorMsg)
1279 *pcbErrorMsg = cbBuf-1;
1281 return ret;
1284 BOOL WINAPI SQLInstallTranslatorExW(LPCWSTR lpszTranslator, LPCWSTR lpszPathIn,
1285 LPWSTR lpszPathOut, WORD cbPathOutMax, WORD *pcbPathOut,
1286 WORD fRequest, LPDWORD lpdwUsageCount)
1288 UINT len;
1289 WCHAR path[MAX_PATH];
1291 clear_errors();
1292 TRACE("%s %s %p %d %p %d %p\n", debugstr_w(lpszTranslator),
1293 debugstr_w(lpszPathIn), lpszPathOut, cbPathOutMax, pcbPathOut,
1294 fRequest, lpdwUsageCount);
1296 write_registry_values(odbctranslators, lpszTranslator, lpszPathIn, path, lpdwUsageCount);
1298 len = lstrlenW(path);
1300 if (pcbPathOut)
1301 *pcbPathOut = len;
1303 if (lpszPathOut && cbPathOutMax > len)
1305 lstrcpyW(lpszPathOut, path);
1306 return TRUE;
1308 return FALSE;
1311 BOOL WINAPI SQLInstallTranslatorEx(LPCSTR lpszTranslator, LPCSTR lpszPathIn,
1312 LPSTR lpszPathOut, WORD cbPathOutMax, WORD *pcbPathOut,
1313 WORD fRequest, LPDWORD lpdwUsageCount)
1315 LPCSTR p;
1316 LPWSTR translator, pathin;
1317 WCHAR pathout[MAX_PATH];
1318 BOOL ret;
1319 WORD cbOut = 0;
1321 clear_errors();
1322 TRACE("%s %s %p %d %p %d %p\n", debugstr_a(lpszTranslator),
1323 debugstr_a(lpszPathIn), lpszPathOut, cbPathOutMax, pcbPathOut,
1324 fRequest, lpdwUsageCount);
1326 for (p = lpszTranslator; *p; p += lstrlenA(p) + 1)
1327 TRACE("%s\n", debugstr_a(p));
1329 translator = SQLInstall_strdup_multi(lpszTranslator);
1330 pathin = SQLInstall_strdup(lpszPathIn);
1332 ret = SQLInstallTranslatorExW(translator, pathin, pathout, MAX_PATH,
1333 &cbOut, fRequest, lpdwUsageCount);
1334 if (ret)
1336 int len = WideCharToMultiByte(CP_ACP, 0, pathout, -1, lpszPathOut,
1337 0, NULL, NULL);
1338 if (len)
1340 if (pcbPathOut)
1341 *pcbPathOut = len - 1;
1343 if (!lpszPathOut || cbPathOutMax < len)
1345 ret = FALSE;
1346 goto out;
1348 len = WideCharToMultiByte(CP_ACP, 0, pathout, -1, lpszPathOut,
1349 cbPathOutMax, NULL, NULL);
1353 out:
1354 free(translator);
1355 free(pathin);
1356 return ret;
1359 BOOL WINAPI SQLInstallTranslator(LPCSTR lpszInfFile, LPCSTR lpszTranslator,
1360 LPCSTR lpszPathIn, LPSTR lpszPathOut, WORD cbPathOutMax,
1361 WORD *pcbPathOut, WORD fRequest, LPDWORD lpdwUsageCount)
1363 clear_errors();
1364 TRACE("%s %s %s %p %d %p %d %p\n", debugstr_a(lpszInfFile),
1365 debugstr_a(lpszTranslator), debugstr_a(lpszPathIn), lpszPathOut,
1366 cbPathOutMax, pcbPathOut, fRequest, lpdwUsageCount);
1368 if (lpszInfFile)
1369 return FALSE;
1371 return SQLInstallTranslatorEx(lpszTranslator, lpszPathIn, lpszPathOut,
1372 cbPathOutMax, pcbPathOut, fRequest, lpdwUsageCount);
1375 BOOL WINAPI SQLInstallTranslatorW(LPCWSTR lpszInfFile, LPCWSTR lpszTranslator,
1376 LPCWSTR lpszPathIn, LPWSTR lpszPathOut, WORD cbPathOutMax,
1377 WORD *pcbPathOut, WORD fRequest, LPDWORD lpdwUsageCount)
1379 clear_errors();
1380 TRACE("%s %s %s %p %d %p %d %p\n", debugstr_w(lpszInfFile),
1381 debugstr_w(lpszTranslator), debugstr_w(lpszPathIn), lpszPathOut,
1382 cbPathOutMax, pcbPathOut, fRequest, lpdwUsageCount);
1384 if (lpszInfFile)
1385 return FALSE;
1387 return SQLInstallTranslatorExW(lpszTranslator, lpszPathIn, lpszPathOut,
1388 cbPathOutMax, pcbPathOut, fRequest, lpdwUsageCount);
1391 BOOL WINAPI SQLManageDataSources(HWND hwnd)
1393 clear_errors();
1394 FIXME("%p\n", hwnd);
1395 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1396 return FALSE;
1399 SQLRETURN WINAPI SQLPostInstallerErrorW(DWORD fErrorCode, LPCWSTR szErrorMsg)
1401 FIXME("%lu %s\n", fErrorCode, debugstr_w(szErrorMsg));
1402 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1403 return FALSE;
1406 SQLRETURN WINAPI SQLPostInstallerError(DWORD fErrorCode, LPCSTR szErrorMsg)
1408 FIXME("%lu %s\n", fErrorCode, debugstr_a(szErrorMsg));
1409 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1410 return FALSE;
1413 BOOL WINAPI SQLReadFileDSNW(LPCWSTR lpszFileName, LPCWSTR lpszAppName,
1414 LPCWSTR lpszKeyName, LPWSTR lpszString, WORD cbString,
1415 WORD *pcbString)
1417 clear_errors();
1418 FIXME("%s %s %s %s %d %p\n", debugstr_w(lpszFileName), debugstr_w(lpszAppName),
1419 debugstr_w(lpszKeyName), debugstr_w(lpszString), cbString, pcbString);
1420 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1421 return FALSE;
1424 BOOL WINAPI SQLReadFileDSN(LPCSTR lpszFileName, LPCSTR lpszAppName,
1425 LPCSTR lpszKeyName, LPSTR lpszString, WORD cbString,
1426 WORD *pcbString)
1428 clear_errors();
1429 FIXME("%s %s %s %s %d %p\n", debugstr_a(lpszFileName), debugstr_a(lpszAppName),
1430 debugstr_a(lpszKeyName), debugstr_a(lpszString), cbString, pcbString);
1431 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1432 return FALSE;
1435 BOOL WINAPI SQLRemoveDefaultDataSource(void)
1437 clear_errors();
1438 FIXME("\n");
1439 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1440 return FALSE;
1443 BOOL WINAPI SQLRemoveDriverW(LPCWSTR drivername, BOOL remove_dsn, LPDWORD usage_count)
1445 HKEY hkey;
1446 DWORD usagecount = 1;
1448 clear_errors();
1449 TRACE("%s %d %p\n", debugstr_w(drivername), remove_dsn, usage_count);
1451 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey) == ERROR_SUCCESS)
1453 HKEY hkeydriver;
1455 if (RegOpenKeyW(hkey, drivername, &hkeydriver) == ERROR_SUCCESS)
1457 DWORD size, type;
1458 DWORD count;
1460 size = sizeof(usagecount);
1461 RegGetValueA(hkeydriver, NULL, "UsageCount", RRF_RT_DWORD, &type, &usagecount, &size);
1462 TRACE("Usage count %ld\n", usagecount);
1463 count = usagecount - 1;
1464 if (count)
1466 if (RegSetValueExA(hkeydriver, "UsageCount", 0, REG_DWORD, (BYTE*)&count, sizeof(count)) != ERROR_SUCCESS)
1467 ERR("Failed to write registry UsageCount key\n");
1470 RegCloseKey(hkeydriver);
1473 if (usagecount)
1474 usagecount--;
1476 if (!usagecount)
1478 if (RegDeleteKeyW(hkey, drivername) != ERROR_SUCCESS)
1479 ERR("Failed to delete registry key: %s\n", debugstr_w(drivername));
1481 if (RegOpenKeyW(hkey, odbcdrivers, &hkeydriver) == ERROR_SUCCESS)
1483 if(RegDeleteValueW(hkeydriver, drivername) != ERROR_SUCCESS)
1484 ERR("Failed to delete registry value: %s\n", debugstr_w(drivername));
1485 RegCloseKey(hkeydriver);
1489 RegCloseKey(hkey);
1492 if (usage_count)
1493 *usage_count = usagecount;
1495 return TRUE;
1498 BOOL WINAPI SQLRemoveDriver(LPCSTR lpszDriver, BOOL fRemoveDSN,
1499 LPDWORD lpdwUsageCount)
1501 WCHAR *driver;
1502 BOOL ret;
1504 clear_errors();
1505 TRACE("%s %d %p\n", debugstr_a(lpszDriver), fRemoveDSN, lpdwUsageCount);
1507 driver = SQLInstall_strdup(lpszDriver);
1509 ret = SQLRemoveDriverW(driver, fRemoveDSN, lpdwUsageCount);
1511 free(driver);
1512 return ret;
1515 BOOL WINAPI SQLRemoveDriverManager(LPDWORD pdwUsageCount)
1517 clear_errors();
1518 FIXME("%p\n", pdwUsageCount);
1519 if (pdwUsageCount) *pdwUsageCount = 1;
1520 return TRUE;
1523 BOOL WINAPI SQLRemoveDSNFromIniW(LPCWSTR lpszDSN)
1525 HKEY hkey;
1527 TRACE("%s\n", debugstr_w(lpszDSN));
1529 clear_errors();
1531 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, L"Software\\ODBC\\ODBC.INI\\ODBC Data Sources", &hkey) == ERROR_SUCCESS)
1533 RegDeleteValueW(hkey, lpszDSN);
1534 RegCloseKey(hkey);
1537 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, L"Software\\ODBC\\ODBC.INI", &hkey) == ERROR_SUCCESS)
1539 RegDeleteTreeW(hkey, lpszDSN);
1540 RegCloseKey(hkey);
1543 return TRUE;
1546 BOOL WINAPI SQLRemoveDSNFromIni(LPCSTR lpszDSN)
1548 BOOL ret = FALSE;
1549 WCHAR *dsn;
1551 TRACE("%s\n", debugstr_a(lpszDSN));
1553 clear_errors();
1555 dsn = SQLInstall_strdup(lpszDSN);
1556 if (dsn)
1557 ret = SQLRemoveDSNFromIniW(dsn);
1558 else
1559 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
1561 free(dsn);
1563 return ret;
1566 BOOL WINAPI SQLRemoveTranslatorW(const WCHAR *translator, DWORD *usage_count)
1568 HKEY hkey;
1569 DWORD usagecount = 1;
1570 BOOL ret = TRUE;
1572 clear_errors();
1573 TRACE("%s %p\n", debugstr_w(translator), usage_count);
1575 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey) == ERROR_SUCCESS)
1577 HKEY hkeydriver;
1579 if (RegOpenKeyW(hkey, translator, &hkeydriver) == ERROR_SUCCESS)
1581 DWORD size, type;
1582 DWORD count;
1584 size = sizeof(usagecount);
1585 RegGetValueA(hkeydriver, NULL, "UsageCount", RRF_RT_DWORD, &type, &usagecount, &size);
1586 TRACE("Usage count %ld\n", usagecount);
1587 count = usagecount - 1;
1588 if (count)
1590 if (RegSetValueExA(hkeydriver, "UsageCount", 0, REG_DWORD, (BYTE*)&count, sizeof(count)) != ERROR_SUCCESS)
1591 ERR("Failed to write registry UsageCount key\n");
1594 RegCloseKey(hkeydriver);
1597 if (usagecount)
1598 usagecount--;
1600 if (!usagecount)
1602 if(RegDeleteKeyW(hkey, translator) != ERROR_SUCCESS)
1604 push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
1605 WARN("Failed to delete registry key: %s\n", debugstr_w(translator));
1606 ret = FALSE;
1609 if (ret && RegOpenKeyW(hkey, odbctranslators, &hkeydriver) == ERROR_SUCCESS)
1611 if(RegDeleteValueW(hkeydriver, translator) != ERROR_SUCCESS)
1613 push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
1614 WARN("Failed to delete registry key: %s\n", debugstr_w(translator));
1615 ret = FALSE;
1618 RegCloseKey(hkeydriver);
1622 RegCloseKey(hkey);
1625 if (ret && usage_count)
1626 *usage_count = usagecount;
1628 return ret;
1631 BOOL WINAPI SQLRemoveTranslator(LPCSTR lpszTranslator, LPDWORD lpdwUsageCount)
1633 WCHAR *translator;
1634 BOOL ret;
1636 clear_errors();
1637 TRACE("%s %p\n", debugstr_a(lpszTranslator), lpdwUsageCount);
1639 translator = SQLInstall_strdup(lpszTranslator);
1640 ret = SQLRemoveTranslatorW(translator, lpdwUsageCount);
1642 free(translator);
1643 return ret;
1646 BOOL WINAPI SQLSetConfigMode(UWORD wConfigMode)
1648 clear_errors();
1649 TRACE("%u\n", wConfigMode);
1651 if (wConfigMode > ODBC_SYSTEM_DSN)
1653 push_error(ODBC_ERROR_INVALID_PARAM_SEQUENCE, odbc_error_invalid_param_sequence);
1654 return FALSE;
1656 else
1658 config_mode = wConfigMode;
1659 return TRUE;
1663 BOOL WINAPI SQLValidDSNW(LPCWSTR lpszDSN)
1665 static const WCHAR invalid[] = {'[',']','{','}','(',')',',',';','?','*','=','!','@','\\',0};
1666 clear_errors();
1667 TRACE("%s\n", debugstr_w(lpszDSN));
1669 if(lstrlenW(lpszDSN) > SQL_MAX_DSN_LENGTH || wcspbrk(lpszDSN, invalid) != NULL)
1671 return FALSE;
1674 return TRUE;
1677 BOOL WINAPI SQLValidDSN(LPCSTR lpszDSN)
1679 static const char *invalid = "[]{}(),;?*=!@\\";
1680 clear_errors();
1681 TRACE("%s\n", debugstr_a(lpszDSN));
1683 if(strlen(lpszDSN) > SQL_MAX_DSN_LENGTH || strpbrk(lpszDSN, invalid) != NULL)
1685 return FALSE;
1688 return TRUE;
1691 BOOL WINAPI SQLWriteDSNToIniW(LPCWSTR lpszDSN, LPCWSTR lpszDriver)
1693 DWORD ret;
1694 HKEY hkey, hkeydriver;
1695 WCHAR filename[MAX_PATH];
1697 TRACE("%s %s\n", debugstr_w(lpszDSN), debugstr_w(lpszDriver));
1699 clear_errors();
1701 if (!SQLValidDSNW(lpszDSN))
1703 push_error(ODBC_ERROR_INVALID_DSN, odbc_error_invalid_dsn);
1704 return FALSE;
1707 /* It doesn't matter if we cannot find the driver, windows just writes a blank value. */
1708 filename[0] = 0;
1709 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey) == ERROR_SUCCESS)
1711 HKEY hkeydriver;
1713 if (RegOpenKeyW(hkey, lpszDriver, &hkeydriver) == ERROR_SUCCESS)
1715 DWORD size = MAX_PATH * sizeof(WCHAR);
1716 RegGetValueW(hkeydriver, NULL, L"driver", RRF_RT_REG_SZ, NULL, filename, &size);
1717 RegCloseKey(hkeydriver);
1719 RegCloseKey(hkey);
1722 if ((ret = RegCreateKeyW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\ODBC\\ODBC.INI", &hkey)) == ERROR_SUCCESS)
1724 HKEY sources;
1726 if ((ret = RegCreateKeyW(hkey, L"ODBC Data Sources", &sources)) == ERROR_SUCCESS)
1728 RegSetValueExW(sources, lpszDSN, 0, REG_SZ, (BYTE*)lpszDriver, (lstrlenW(lpszDriver)+1)*sizeof(WCHAR));
1729 RegCloseKey(sources);
1731 RegDeleteTreeW(hkey, lpszDSN);
1732 if ((ret = RegCreateKeyW(hkey, lpszDSN, &hkeydriver)) == ERROR_SUCCESS)
1734 RegSetValueExW(sources, L"driver", 0, REG_SZ, (BYTE*)filename, (lstrlenW(filename)+1)*sizeof(WCHAR));
1735 RegCloseKey(hkeydriver);
1738 RegCloseKey(hkey);
1741 if (ret != ERROR_SUCCESS)
1742 push_error(ODBC_ERROR_REQUEST_FAILED, odbc_error_request_failed);
1744 return ret == ERROR_SUCCESS;
1747 BOOL WINAPI SQLWriteDSNToIni(LPCSTR lpszDSN, LPCSTR lpszDriver)
1749 BOOL ret = FALSE;
1750 WCHAR *dsn, *driver;
1752 TRACE("%s %s\n", debugstr_a(lpszDSN), debugstr_a(lpszDriver));
1754 dsn = SQLInstall_strdup(lpszDSN);
1755 driver = SQLInstall_strdup(lpszDriver);
1756 if (dsn && driver)
1757 ret = SQLWriteDSNToIniW(dsn, driver);
1758 else
1759 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
1761 free(dsn);
1762 free(driver);
1764 return ret;
1767 BOOL WINAPI SQLWriteFileDSNW(LPCWSTR lpszFileName, LPCWSTR lpszAppName,
1768 LPCWSTR lpszKeyName, LPCWSTR lpszString)
1770 clear_errors();
1771 FIXME("%s %s %s %s\n", debugstr_w(lpszFileName), debugstr_w(lpszAppName),
1772 debugstr_w(lpszKeyName), debugstr_w(lpszString));
1773 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1774 return FALSE;
1777 BOOL WINAPI SQLWriteFileDSN(LPCSTR lpszFileName, LPCSTR lpszAppName,
1778 LPCSTR lpszKeyName, LPCSTR lpszString)
1780 clear_errors();
1781 FIXME("%s %s %s %s\n", debugstr_a(lpszFileName), debugstr_a(lpszAppName),
1782 debugstr_a(lpszKeyName), debugstr_a(lpszString));
1783 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1784 return FALSE;
1787 BOOL WINAPI SQLWritePrivateProfileStringW(LPCWSTR lpszSection, LPCWSTR lpszEntry,
1788 LPCWSTR lpszString, LPCWSTR lpszFilename)
1790 static const WCHAR empty[] = {0};
1791 LONG ret;
1792 HKEY hkey;
1794 clear_errors();
1795 TRACE("%s %s %s %s\n", debugstr_w(lpszSection), debugstr_w(lpszEntry),
1796 debugstr_w(lpszString), debugstr_w(lpszFilename));
1798 if(!lpszFilename || !*lpszFilename)
1800 push_error(ODBC_ERROR_INVALID_STR, odbc_error_invalid_param_string);
1801 return FALSE;
1804 if ((ret = RegCreateKeyW(HKEY_CURRENT_USER, odbcW, &hkey)) == ERROR_SUCCESS)
1806 HKEY hkeyfilename;
1808 if ((ret = RegCreateKeyW(hkey, lpszFilename, &hkeyfilename)) == ERROR_SUCCESS)
1810 HKEY hkey_section;
1812 if ((ret = RegCreateKeyW(hkeyfilename, lpszSection, &hkey_section)) == ERROR_SUCCESS)
1814 if(lpszString)
1815 ret = RegSetValueExW(hkey_section, lpszEntry, 0, REG_SZ, (BYTE*)lpszString, (lstrlenW(lpszString)+1)*sizeof(WCHAR));
1816 else
1817 ret = RegSetValueExW(hkey_section, lpszEntry, 0, REG_SZ, (BYTE*)empty, sizeof(empty));
1818 RegCloseKey(hkey_section);
1821 RegCloseKey(hkeyfilename);
1824 RegCloseKey(hkey);
1827 return ret == ERROR_SUCCESS;
1830 BOOL WINAPI SQLWritePrivateProfileString(LPCSTR lpszSection, LPCSTR lpszEntry,
1831 LPCSTR lpszString, LPCSTR lpszFilename)
1833 BOOL ret;
1834 WCHAR *sect, *entry, *string, *file;
1835 clear_errors();
1836 TRACE("%s %s %s %s\n", lpszSection, lpszEntry, lpszString, lpszFilename);
1838 sect = strdupAtoW(lpszSection);
1839 entry = strdupAtoW(lpszEntry);
1840 string = strdupAtoW(lpszString);
1841 file = strdupAtoW(lpszFilename);
1843 ret = SQLWritePrivateProfileStringW(sect, entry, string, file);
1845 free(sect);
1846 free(entry);
1847 free(string);
1848 free(file);
1850 return ret;