msi: Avoid duplicate product codes in FindRelatedProducts.
[wine.git] / dlls / odbccp32 / odbccp32.c
blob694a90c3c2e4426319fa10b6072e63ba71b61fba
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/unicode.h"
34 #include "wine/debug.h"
35 #include "wine/heap.h"
37 #include "odbcinst.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(odbc);
41 /* Registry key names */
42 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};
43 static const WCHAR odbcW[] = {'S','o','f','t','w','a','r','e','\\','O','D','B','C',0};
44 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};
45 static const WCHAR odbcdrivers[] = {'O','D','B','C',' ','D','r','i','v','e','r','s',0};
46 static const WCHAR odbctranslators[] = {'O','D','B','C',' ','T','r','a','n','s','l','a','t','o','r','s',0};
48 /* This config mode is known to be process-wide.
49 * MSDN documentation suggests that the value is hidden somewhere in the registry but I haven't found it yet.
50 * Although both the registry and the ODBC.ini files appear to be maintained together they are not maintained automatically through the registry's IniFileMapping.
52 static UWORD config_mode = ODBC_BOTH_DSN;
54 /* MSDN documentation suggests that the error subsystem handles errors 1 to 8
55 * only and experimentation (Windows 2000) shows that the errors are process-
56 * wide so go for the simple solution; static arrays.
58 static int num_errors;
59 static int error_code[8];
60 static const WCHAR *error_msg[8];
61 static const WCHAR odbc_error_general_err[] = {'G','e','n','e','r','a','l',' ','e','r','r','o','r',0};
62 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};
63 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};
64 static const WCHAR odbc_error_out_of_mem[] = {'O','u','t',' ','o','f',' ','m','e','m','o','r','y',0};
65 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};
66 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};
67 static const WCHAR odbc_error_invalid_dsn[] = {'I','n','v','a','l','i','d',' ','D','S','N',0};
68 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};
69 static const WCHAR odbc_error_request_failed[] = {'R','e','q','u','e','s','t',' ','F','a','i','l','e','d',0};
70 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};
72 /* Push an error onto the error stack, taking care of ranges etc. */
73 static void push_error(int code, LPCWSTR msg)
75 if (num_errors < ARRAY_SIZE(error_code))
77 error_code[num_errors] = code;
78 error_msg[num_errors] = msg;
79 num_errors++;
83 /* Clear the error stack */
84 static void clear_errors(void)
86 num_errors = 0;
89 static inline WCHAR *heap_strdupAtoW(const char *str)
91 LPWSTR ret = NULL;
93 if(str) {
94 DWORD len;
96 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
97 ret = heap_alloc(len*sizeof(WCHAR));
98 if(ret)
99 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
102 return ret;
106 BOOL WINAPI ODBCCPlApplet( LONG i, LONG j, LONG * p1, LONG * p2)
108 clear_errors();
109 FIXME( "( %d %d %p %p) : stub!\n", i, j, p1, p2);
110 return FALSE;
113 static LPWSTR SQLInstall_strdup_multi(LPCSTR str)
115 LPCSTR p;
116 LPWSTR ret = NULL;
117 DWORD len;
119 if (!str)
120 return ret;
122 for (p = str; *p; p += lstrlenA(p) + 1)
125 len = MultiByteToWideChar(CP_ACP, 0, str, p - str, NULL, 0 );
126 ret = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
127 MultiByteToWideChar(CP_ACP, 0, str, p - str, ret, len );
128 ret[len] = 0;
130 return ret;
133 static LPWSTR SQLInstall_strdup(LPCSTR str)
135 DWORD len;
136 LPWSTR ret = NULL;
138 if (!str)
139 return ret;
141 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0 );
142 ret = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
143 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len );
145 return ret;
148 /* Convert the wide string or zero-length-terminated list of wide strings to a
149 * narrow string or zero-length-terminated list of narrow strings.
150 * Do not try to emulate windows undocumented excesses (e.g. adding a third \0
151 * to a list)
152 * Arguments
153 * mode Indicates the sort of string.
154 * 1 denotes that the buffers contain strings terminated by a single nul
155 * character
156 * 2 denotes that the buffers contain zero-length-terminated lists
157 * (frequently erroneously referred to as double-null-terminated)
158 * buffer The narrow-character buffer into which to place the result. This
159 * must be a non-null pointer to the first element of a buffer whose
160 * length is passed in buffer_length.
161 * str The wide-character buffer containing the string or list of strings to
162 * be converted. str_length defines how many wide characters in the
163 * buffer are to be converted, including all desired terminating nul
164 * characters.
165 * str_length Effective length of str
166 * buffer_length Length of buffer
167 * returned_length A pointer to a variable that will receive the number of
168 * narrow characters placed into the buffer. This pointer
169 * may be NULL.
171 static BOOL SQLInstall_narrow(int mode, LPSTR buffer, LPCWSTR str, WORD str_length, WORD buffer_length, WORD *returned_length)
173 LPSTR pbuf; /* allows us to allocate a temporary buffer only if needed */
174 int len; /* Length of the converted list */
175 BOOL success = FALSE;
176 assert(mode == 1 || mode == 2);
177 assert(buffer_length);
178 len = WideCharToMultiByte(CP_ACP, 0, str, str_length, 0, 0, NULL, NULL);
179 if (len > 0)
181 if (len > buffer_length)
183 pbuf = HeapAlloc(GetProcessHeap(), 0, len);
185 else
187 pbuf = buffer;
189 len = WideCharToMultiByte(CP_ACP, 0, str, str_length, pbuf, len, NULL, NULL);
190 if (len > 0)
192 if (pbuf != buffer)
194 if (buffer_length > (mode - 1))
196 memcpy (buffer, pbuf, buffer_length-mode);
197 *(buffer+buffer_length-mode) = '\0';
199 *(buffer+buffer_length-1) = '\0';
201 if (returned_length)
203 *returned_length = pbuf == buffer ? len : buffer_length;
205 success = TRUE;
207 else
209 ERR("transferring wide to narrow\n");
211 if (pbuf != buffer)
213 HeapFree(GetProcessHeap(), 0, pbuf);
216 else
218 ERR("measuring wide to narrow\n");
220 return success;
223 BOOL WINAPI SQLConfigDataSourceW(HWND hwndParent, WORD fRequest,
224 LPCWSTR lpszDriver, LPCWSTR lpszAttributes)
226 LPCWSTR p;
228 clear_errors();
229 FIXME("%p %d %s %s\n", hwndParent, fRequest, debugstr_w(lpszDriver),
230 debugstr_w(lpszAttributes));
232 for (p = lpszAttributes; *p; p += lstrlenW(p) + 1)
233 FIXME("%s\n", debugstr_w(p));
235 return TRUE;
238 BOOL WINAPI SQLConfigDataSource(HWND hwndParent, WORD fRequest,
239 LPCSTR lpszDriver, LPCSTR lpszAttributes)
241 FIXME("%p %d %s %s\n", hwndParent, fRequest, debugstr_a(lpszDriver),
242 debugstr_a(lpszAttributes));
243 clear_errors();
244 return TRUE;
247 static HMODULE load_config_driver(const WCHAR *driver)
249 static WCHAR reg_driver[] = {'d','r','i','v','e','r',0};
250 long ret;
251 HMODULE hmod;
252 WCHAR *filename = NULL;
253 DWORD size = 0, type;
254 HKEY hkey;
256 if ((ret = RegOpenKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey)) == ERROR_SUCCESS)
258 HKEY hkeydriver;
260 if ((ret = RegOpenKeyW(hkey, driver, &hkeydriver)) == ERROR_SUCCESS)
262 ret = RegGetValueW(hkeydriver, NULL, reg_driver, RRF_RT_REG_SZ, &type, NULL, &size);
263 if(ret != ERROR_SUCCESS || type != REG_SZ)
265 RegCloseKey(hkeydriver);
266 RegCloseKey(hkey);
267 push_error(ODBC_ERROR_INVALID_DSN, odbc_error_invalid_dsn);
269 return NULL;
272 filename = HeapAlloc(GetProcessHeap(), 0, size);
273 if(!filename)
275 RegCloseKey(hkeydriver);
276 RegCloseKey(hkey);
277 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
279 return NULL;
281 ret = RegGetValueW(hkeydriver, NULL, reg_driver, RRF_RT_REG_SZ, &type, filename, &size);
283 RegCloseKey(hkeydriver);
286 RegCloseKey(hkey);
289 if(ret != ERROR_SUCCESS)
291 HeapFree(GetProcessHeap(), 0, filename);
292 push_error(ODBC_ERROR_INVALID_DSN, odbc_error_invalid_dsn);
293 return NULL;
296 hmod = LoadLibraryW(filename);
297 HeapFree(GetProcessHeap(), 0, filename);
299 if(!hmod)
300 push_error(ODBC_ERROR_LOAD_LIB_FAILED, odbc_error_load_lib_failed);
302 return hmod;
305 static BOOL write_config_value(const WCHAR *driver, const WCHAR *args)
307 long ret;
308 HKEY hkey, hkeydriver;
309 WCHAR *name = NULL;
311 if(!args)
312 return FALSE;
314 if((ret = RegOpenKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey)) == ERROR_SUCCESS)
316 if((ret = RegOpenKeyW(hkey, driver, &hkeydriver)) == ERROR_SUCCESS)
318 WCHAR *divider, *value;
320 name = heap_alloc( (strlenW(args) + 1) * sizeof(WCHAR));
321 if(!name)
323 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
324 goto fail;
326 lstrcpyW(name, args);
328 divider = strchrW(name,'=');
329 if(!divider)
331 push_error(ODBC_ERROR_INVALID_KEYWORD_VALUE, odbc_error_invalid_keyword);
332 goto fail;
335 value = divider + 1;
336 *divider = '\0';
338 TRACE("Write pair: %s = %s\n", debugstr_w(name), debugstr_w(value));
339 if(RegSetValueExW(hkeydriver, name, 0, REG_SZ, (BYTE*)value,
340 (strlenW(value)+1) * sizeof(WCHAR)) != ERROR_SUCCESS)
341 ERR("Failed to write registry installed key\n");
342 heap_free(name);
344 RegCloseKey(hkeydriver);
347 RegCloseKey(hkey);
350 if(ret != ERROR_SUCCESS)
351 push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
353 return ret == ERROR_SUCCESS;
355 fail:
356 RegCloseKey(hkeydriver);
357 RegCloseKey(hkey);
358 heap_free(name);
360 return FALSE;
363 BOOL WINAPI SQLConfigDriverW(HWND hwnd, WORD request, LPCWSTR driver,
364 LPCWSTR args, LPWSTR msg, WORD msgmax, WORD *msgout)
366 BOOL (WINAPI *pConfigDriverW)(HWND hwnd, WORD request, const WCHAR *driver, const WCHAR *args, const WCHAR *msg, WORD msgmax, WORD *msgout);
367 HMODULE hmod;
368 BOOL funcret = FALSE;
370 clear_errors();
371 TRACE("(%p %d %s %s %p %d %p)\n", hwnd, request, debugstr_w(driver),
372 debugstr_w(args), msg, msgmax, msgout);
374 if(request == ODBC_CONFIG_DRIVER)
376 return write_config_value(driver, args);
379 hmod = load_config_driver(driver);
380 if(!hmod)
381 return FALSE;
383 pConfigDriverW = (void*)GetProcAddress(hmod, "ConfigDriverW");
384 if(pConfigDriverW)
385 funcret = pConfigDriverW(hwnd, request, driver, args, msg, msgmax, msgout);
387 if(!funcret)
388 push_error(ODBC_ERROR_REQUEST_FAILED, odbc_error_request_failed);
390 FreeLibrary(hmod);
392 return funcret;
395 BOOL WINAPI SQLConfigDriver(HWND hwnd, WORD request, LPCSTR driver,
396 LPCSTR args, LPSTR msg, WORD msgmax, WORD *msgout)
398 BOOL (WINAPI *pConfigDriverA)(HWND hwnd, WORD request, const char *driver, const char *args, const char *msg, WORD msgmax, WORD *msgout);
399 HMODULE hmod;
400 WCHAR *driverW;
401 BOOL funcret = FALSE;
403 clear_errors();
404 TRACE("(%p %d %s %s %p %d %p)\n", hwnd, request, debugstr_a(driver),
405 debugstr_a(args), msg, msgmax, msgout);
407 driverW = heap_strdupAtoW(driver);
408 if(!driverW)
410 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
411 return FALSE;
413 if(request == ODBC_CONFIG_DRIVER)
415 BOOL ret = FALSE;
416 WCHAR *argsW = heap_strdupAtoW(args);
417 if(argsW)
419 ret = write_config_value(driverW, argsW);
420 HeapFree(GetProcessHeap(), 0, argsW);
422 else
424 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
427 HeapFree(GetProcessHeap(), 0, driverW);
429 return ret;
432 hmod = load_config_driver(driverW);
433 HeapFree(GetProcessHeap(), 0, driverW);
434 if(!hmod)
435 return FALSE;
437 pConfigDriverA = (void*)GetProcAddress(hmod, "ConfigDriver");
438 if(pConfigDriverA)
439 funcret = pConfigDriverA(hwnd, request, driver, args, msg, msgmax, msgout);
441 if(!funcret)
442 push_error(ODBC_ERROR_REQUEST_FAILED, odbc_error_request_failed);
444 FreeLibrary(hmod);
446 return funcret;
449 BOOL WINAPI SQLCreateDataSourceW(HWND hwnd, LPCWSTR lpszDS)
451 clear_errors();
452 FIXME("%p %s\n", hwnd, debugstr_w(lpszDS));
453 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
454 return FALSE;
457 BOOL WINAPI SQLCreateDataSource(HWND hwnd, LPCSTR lpszDS)
459 clear_errors();
460 FIXME("%p %s\n", hwnd, debugstr_a(lpszDS));
461 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
462 return FALSE;
465 BOOL WINAPI SQLGetAvailableDriversW(LPCWSTR lpszInfFile, LPWSTR lpszBuf,
466 WORD cbBufMax, WORD *pcbBufOut)
468 clear_errors();
469 FIXME("%s %p %d %p\n", debugstr_w(lpszInfFile), lpszBuf, cbBufMax, pcbBufOut);
470 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
471 return FALSE;
474 BOOL WINAPI SQLGetAvailableDrivers(LPCSTR lpszInfFile, LPSTR lpszBuf,
475 WORD cbBufMax, WORD *pcbBufOut)
477 clear_errors();
478 FIXME("%s %p %d %p\n", debugstr_a(lpszInfFile), lpszBuf, cbBufMax, pcbBufOut);
479 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
480 return FALSE;
483 BOOL WINAPI SQLGetConfigMode(UWORD *pwConfigMode)
485 clear_errors();
486 TRACE("%p\n", pwConfigMode);
487 if (pwConfigMode)
488 *pwConfigMode = config_mode;
489 return TRUE;
492 BOOL WINAPI SQLGetInstalledDriversW(WCHAR *buf, WORD size, WORD *sizeout)
494 WORD written = 0;
495 DWORD index = 0;
496 BOOL ret = TRUE;
497 DWORD valuelen;
498 WCHAR *value;
499 HKEY drivers;
500 DWORD len;
501 LONG res;
503 clear_errors();
505 TRACE("%p %d %p\n", buf, size, sizeout);
507 if (!buf || !size)
509 push_error(ODBC_ERROR_INVALID_BUFF_LEN, odbc_error_invalid_buff_len);
510 return FALSE;
513 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, drivers_key, 0, KEY_QUERY_VALUE, &drivers);
514 if (res)
516 push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
517 return FALSE;
520 valuelen = 256;
521 value = heap_alloc(valuelen * sizeof(WCHAR));
523 size--;
525 while (1)
527 len = valuelen;
528 res = RegEnumValueW(drivers, index, value, &len, NULL, NULL, NULL, NULL);
529 while (res == ERROR_MORE_DATA)
531 value = heap_realloc(value, ++len * sizeof(WCHAR));
532 res = RegEnumValueW(drivers, index, value, &len, NULL, NULL, NULL, NULL);
534 if (res == ERROR_SUCCESS)
536 lstrcpynW(buf + written, value, size - written);
537 written += min(len + 1, size - written);
539 else if (res == ERROR_NO_MORE_ITEMS)
540 break;
541 else
543 push_error(ODBC_ERROR_GENERAL_ERR, odbc_error_general_err);
544 ret = FALSE;
545 break;
547 index++;
550 buf[written++] = 0;
552 heap_free(value);
553 RegCloseKey(drivers);
554 if (sizeout)
555 *sizeout = written;
556 return ret;
559 BOOL WINAPI SQLGetInstalledDrivers(char *buf, WORD size, WORD *sizeout)
561 WORD written;
562 WCHAR *wbuf;
563 BOOL ret;
565 TRACE("%p %d %p\n", buf, size, sizeout);
567 if (!buf || !size)
569 push_error(ODBC_ERROR_INVALID_BUFF_LEN, odbc_error_invalid_buff_len);
570 return FALSE;
573 wbuf = heap_alloc(size * sizeof(WCHAR));
574 if (!wbuf)
576 push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
577 return FALSE;
580 ret = SQLGetInstalledDriversW(wbuf, size, &written);
581 if (!ret)
583 heap_free(wbuf);
584 return FALSE;
587 *sizeout = WideCharToMultiByte(CP_ACP, 0, wbuf, written, NULL, 0, NULL, NULL);
588 WideCharToMultiByte(CP_ACP, 0, wbuf, written, buf, size, NULL, NULL);
590 heap_free(wbuf);
591 return TRUE;
594 static HKEY get_privateprofile_sectionkey(LPCWSTR section, LPCWSTR filename)
596 HKEY hkey, hkeyfilename, hkeysection;
597 LONG ret;
599 if (RegOpenKeyW(HKEY_CURRENT_USER, odbcW, &hkey))
600 return NULL;
602 ret = RegOpenKeyW(hkey, filename, &hkeyfilename);
603 RegCloseKey(hkey);
604 if (ret)
605 return NULL;
607 ret = RegOpenKeyW(hkeyfilename, section, &hkeysection);
608 RegCloseKey(hkeyfilename);
610 return ret ? NULL : hkeysection;
613 int WINAPI SQLGetPrivateProfileStringW(LPCWSTR section, LPCWSTR entry,
614 LPCWSTR defvalue, LPWSTR buff, int buff_len, LPCWSTR filename)
616 BOOL usedefault = TRUE;
617 HKEY sectionkey;
618 LONG ret = 0;
620 TRACE("%s %s %s %p %d %s\n", debugstr_w(section), debugstr_w(entry),
621 debugstr_w(defvalue), buff, buff_len, debugstr_w(filename));
623 clear_errors();
625 if (buff_len <= 0 || !section)
626 return 0;
628 if(buff)
629 buff[0] = 0;
631 if (!defvalue || !buff)
632 return 0;
634 sectionkey = get_privateprofile_sectionkey(section, filename);
635 if (sectionkey)
637 DWORD type, size;
639 if (entry)
641 size = buff_len * sizeof(*buff);
642 if (RegGetValueW(sectionkey, NULL, entry, RRF_RT_REG_SZ, &type, buff, &size) == ERROR_SUCCESS)
644 usedefault = FALSE;
645 ret = (size / sizeof(*buff)) - 1;
648 else
650 WCHAR name[MAX_PATH];
651 DWORD index = 0;
652 DWORD namelen;
654 usedefault = FALSE;
656 memset(buff, 0, buff_len);
658 namelen = sizeof(name);
659 while (RegEnumValueW(sectionkey, index, name, &namelen, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
661 if ((ret + namelen+1) > buff_len)
662 break;
664 lstrcpyW(buff+ret, name);
665 ret += namelen+1;
666 namelen = sizeof(name);
667 index++;
671 RegCloseKey(sectionkey);
673 else
674 usedefault = entry != NULL;
676 if (usedefault)
678 lstrcpynW(buff, defvalue, buff_len);
679 ret = lstrlenW(buff);
682 return ret;
685 int WINAPI SQLGetPrivateProfileString(LPCSTR section, LPCSTR entry,
686 LPCSTR defvalue, LPSTR buff, int buff_len, LPCSTR filename)
688 WCHAR *sectionW, *filenameW;
689 BOOL usedefault = TRUE;
690 HKEY sectionkey;
691 LONG ret = 0;
693 TRACE("%s %s %s %p %d %s\n", debugstr_a(section), debugstr_a(entry),
694 debugstr_a(defvalue), buff, buff_len, debugstr_a(filename));
696 clear_errors();
698 if (buff_len <= 0)
699 return 0;
701 if (buff)
702 buff[0] = 0;
704 if (!section || !defvalue || !buff)
705 return 0;
707 sectionW = heap_strdupAtoW(section);
708 filenameW = heap_strdupAtoW(filename);
710 sectionkey = get_privateprofile_sectionkey(sectionW, filenameW);
712 heap_free(sectionW);
713 heap_free(filenameW);
715 if (sectionkey)
717 DWORD type, size;
719 if (entry)
721 size = buff_len * sizeof(*buff);
722 if (RegGetValueA(sectionkey, NULL, entry, RRF_RT_REG_SZ, &type, buff, &size) == ERROR_SUCCESS)
724 usedefault = FALSE;
725 ret = (size / sizeof(*buff)) - 1;
728 else
730 char name[MAX_PATH] = {0};
731 DWORD index = 0;
732 DWORD namelen;
734 usedefault = FALSE;
736 memset(buff, 0, buff_len);
738 namelen = sizeof(name);
739 while (RegEnumValueA(sectionkey, index, name, &namelen, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
741 if ((ret + namelen+1) > buff_len)
742 break;
744 lstrcpyA(buff+ret, name);
746 ret += namelen+1;
747 namelen = sizeof(name);
748 index++;
752 RegCloseKey(sectionkey);
754 else
755 usedefault = entry != NULL;
757 if (usedefault)
759 lstrcpynA(buff, defvalue, buff_len);
760 ret = strlen(buff);
763 return ret;
766 BOOL WINAPI SQLGetTranslatorW(HWND hwndParent, LPWSTR lpszName, WORD cbNameMax,
767 WORD *pcbNameOut, LPWSTR lpszPath, WORD cbPathMax,
768 WORD *pcbPathOut, DWORD *pvOption)
770 clear_errors();
771 FIXME("%p %s %d %p %p %d %p %p\n", hwndParent, debugstr_w(lpszName), cbNameMax,
772 pcbNameOut, lpszPath, cbPathMax, pcbPathOut, pvOption);
773 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
774 return FALSE;
777 BOOL WINAPI SQLGetTranslator(HWND hwndParent, LPSTR lpszName, WORD cbNameMax,
778 WORD *pcbNameOut, LPSTR lpszPath, WORD cbPathMax,
779 WORD *pcbPathOut, DWORD *pvOption)
781 clear_errors();
782 FIXME("%p %s %d %p %p %d %p %p\n", hwndParent, debugstr_a(lpszName), cbNameMax,
783 pcbNameOut, lpszPath, cbPathMax, pcbPathOut, pvOption);
784 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
785 return FALSE;
788 BOOL WINAPI SQLInstallDriverW(LPCWSTR lpszInfFile, LPCWSTR lpszDriver,
789 LPWSTR lpszPath, WORD cbPathMax, WORD * pcbPathOut)
791 DWORD usage;
793 clear_errors();
794 TRACE("%s %s %p %d %p\n", debugstr_w(lpszInfFile),
795 debugstr_w(lpszDriver), lpszPath, cbPathMax, pcbPathOut);
797 if (lpszInfFile)
798 return FALSE;
800 return SQLInstallDriverExW(lpszDriver, NULL, lpszPath, cbPathMax,
801 pcbPathOut, ODBC_INSTALL_COMPLETE, &usage);
804 BOOL WINAPI SQLInstallDriver(LPCSTR lpszInfFile, LPCSTR lpszDriver,
805 LPSTR lpszPath, WORD cbPathMax, WORD * pcbPathOut)
807 DWORD usage;
809 clear_errors();
810 TRACE("%s %s %p %d %p\n", debugstr_a(lpszInfFile),
811 debugstr_a(lpszDriver), lpszPath, cbPathMax, pcbPathOut);
813 if (lpszInfFile)
814 return FALSE;
816 return SQLInstallDriverEx(lpszDriver, NULL, lpszPath, cbPathMax,
817 pcbPathOut, ODBC_INSTALL_COMPLETE, &usage);
820 static void write_registry_values(const WCHAR *regkey, const WCHAR *driver, const WCHAR *path_in, WCHAR *path,
821 DWORD *usage_count)
823 static const WCHAR installed[] = {'I','n','s','t','a','l','l','e','d',0};
824 static const WCHAR slash[] = {'\\', 0};
825 static const WCHAR driverW[] = {'D','r','i','v','e','r',0};
826 static const WCHAR setupW[] = {'S','e','t','u','p',0};
827 static const WCHAR translator[] = {'T','r','a','n','s','l','a','t','o','r',0};
828 HKEY hkey, hkeydriver;
830 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey) == ERROR_SUCCESS)
832 if (RegCreateKeyW(hkey, regkey, &hkeydriver) == ERROR_SUCCESS)
834 if(RegSetValueExW(hkeydriver, driver, 0, REG_SZ, (BYTE*)installed, sizeof(installed)) != ERROR_SUCCESS)
835 ERR("Failed to write registry installed key\n");
837 RegCloseKey(hkeydriver);
840 if (RegCreateKeyW(hkey, driver, &hkeydriver) == ERROR_SUCCESS)
842 WCHAR entry[1024];
843 const WCHAR *p;
844 DWORD usagecount = 0;
845 DWORD type, size;
847 /* Skip name entry */
848 p = driver;
849 p += lstrlenW(p) + 1;
851 if (!path_in)
852 GetSystemDirectoryW(path, MAX_PATH);
853 else
854 lstrcpyW(path, path_in);
856 /* Store Usage */
857 size = sizeof(usagecount);
858 RegGetValueA(hkeydriver, NULL, "UsageCount", RRF_RT_DWORD, &type, &usagecount, &size);
859 TRACE("Usage count %d\n", usagecount);
861 for (; *p; p += lstrlenW(p) + 1)
863 WCHAR *divider = strchrW(p,'=');
865 if (divider)
867 WCHAR *value;
868 int len;
870 /* Write pair values to the registry. */
871 lstrcpynW(entry, p, divider - p + 1);
873 divider++;
874 TRACE("Writing pair %s,%s\n", debugstr_w(entry), debugstr_w(divider));
876 /* Driver, Setup, Translator entries use the system path unless a path is specified. */
877 if(lstrcmpiW(driverW, entry) == 0 || lstrcmpiW(setupW, entry) == 0 ||
878 lstrcmpiW(translator, entry) == 0)
880 len = lstrlenW(path) + lstrlenW(slash) + lstrlenW(divider) + 1;
881 value = heap_alloc(len * sizeof(WCHAR));
882 if(!value)
884 ERR("Out of memory\n");
885 return;
888 lstrcpyW(value, path);
889 lstrcatW(value, slash);
890 lstrcatW(value, divider);
892 else
894 len = lstrlenW(divider) + 1;
895 value = heap_alloc(len * sizeof(WCHAR));
896 lstrcpyW(value, divider);
899 if (RegSetValueExW(hkeydriver, entry, 0, REG_SZ, (BYTE*)value,
900 (lstrlenW(value)+1)*sizeof(WCHAR)) != ERROR_SUCCESS)
901 ERR("Failed to write registry data %s %s\n", debugstr_w(entry), debugstr_w(value));
902 heap_free(value);
904 else
906 ERR("No pair found. %s\n", debugstr_w(p));
907 break;
911 /* Set Usage Count */
912 usagecount++;
913 if (RegSetValueExA(hkeydriver, "UsageCount", 0, REG_DWORD, (BYTE*)&usagecount, sizeof(usagecount)) != ERROR_SUCCESS)
914 ERR("Failed to write registry UsageCount key\n");
916 if (usage_count)
917 *usage_count = usagecount;
919 RegCloseKey(hkeydriver);
922 RegCloseKey(hkey);
926 BOOL WINAPI SQLInstallDriverExW(LPCWSTR lpszDriver, LPCWSTR lpszPathIn,
927 LPWSTR lpszPathOut, WORD cbPathOutMax, WORD *pcbPathOut,
928 WORD fRequest, LPDWORD lpdwUsageCount)
930 UINT len;
931 WCHAR path[MAX_PATH];
933 clear_errors();
934 TRACE("%s %s %p %d %p %d %p\n", debugstr_w(lpszDriver),
935 debugstr_w(lpszPathIn), lpszPathOut, cbPathOutMax, pcbPathOut,
936 fRequest, lpdwUsageCount);
938 write_registry_values(odbcdrivers, lpszDriver, lpszPathIn, path, lpdwUsageCount);
940 len = lstrlenW(path);
942 if (pcbPathOut)
943 *pcbPathOut = len;
945 if (lpszPathOut && cbPathOutMax > len)
947 lstrcpyW(lpszPathOut, path);
948 return TRUE;
950 return FALSE;
953 BOOL WINAPI SQLInstallDriverEx(LPCSTR lpszDriver, LPCSTR lpszPathIn,
954 LPSTR lpszPathOut, WORD cbPathOutMax, WORD *pcbPathOut,
955 WORD fRequest, LPDWORD lpdwUsageCount)
957 LPWSTR driver, pathin;
958 WCHAR pathout[MAX_PATH];
959 BOOL ret;
960 WORD cbOut = 0;
962 clear_errors();
963 TRACE("%s %s %p %d %p %d %p\n", debugstr_a(lpszDriver),
964 debugstr_a(lpszPathIn), lpszPathOut, cbPathOutMax, pcbPathOut,
965 fRequest, lpdwUsageCount);
967 driver = SQLInstall_strdup_multi(lpszDriver);
968 pathin = SQLInstall_strdup(lpszPathIn);
970 ret = SQLInstallDriverExW(driver, pathin, pathout, MAX_PATH, &cbOut,
971 fRequest, lpdwUsageCount);
972 if (ret)
974 int len = WideCharToMultiByte(CP_ACP, 0, pathout, -1, lpszPathOut,
975 0, NULL, NULL);
976 if (len)
978 if (pcbPathOut)
979 *pcbPathOut = len - 1;
981 if (!lpszPathOut || cbPathOutMax < len)
983 ret = FALSE;
984 goto out;
986 len = WideCharToMultiByte(CP_ACP, 0, pathout, -1, lpszPathOut,
987 cbPathOutMax, NULL, NULL);
991 out:
992 HeapFree(GetProcessHeap(), 0, driver);
993 HeapFree(GetProcessHeap(), 0, pathin);
994 return ret;
997 BOOL WINAPI SQLInstallDriverManagerW(LPWSTR lpszPath, WORD cbPathMax,
998 WORD *pcbPathOut)
1000 UINT len;
1001 WCHAR path[MAX_PATH];
1003 TRACE("(%p %d %p)\n", lpszPath, cbPathMax, pcbPathOut);
1005 if (cbPathMax < MAX_PATH)
1006 return FALSE;
1008 clear_errors();
1010 len = GetSystemDirectoryW(path, MAX_PATH);
1012 if (pcbPathOut)
1013 *pcbPathOut = len;
1015 if (lpszPath && cbPathMax > len)
1017 lstrcpyW(lpszPath, path);
1018 return TRUE;
1020 return FALSE;
1023 BOOL WINAPI SQLInstallDriverManager(LPSTR lpszPath, WORD cbPathMax,
1024 WORD *pcbPathOut)
1026 BOOL ret;
1027 WORD len, cbOut = 0;
1028 WCHAR path[MAX_PATH];
1030 TRACE("(%p %d %p)\n", lpszPath, cbPathMax, pcbPathOut);
1032 if (cbPathMax < MAX_PATH)
1033 return FALSE;
1035 clear_errors();
1037 ret = SQLInstallDriverManagerW(path, MAX_PATH, &cbOut);
1038 if (ret)
1040 len = WideCharToMultiByte(CP_ACP, 0, path, -1, lpszPath, 0,
1041 NULL, NULL);
1042 if (len)
1044 if (pcbPathOut)
1045 *pcbPathOut = len - 1;
1047 if (!lpszPath || cbPathMax < len)
1048 return FALSE;
1050 len = WideCharToMultiByte(CP_ACP, 0, path, -1, lpszPath,
1051 cbPathMax, NULL, NULL);
1054 return ret;
1057 BOOL WINAPI SQLInstallODBCW(HWND hwndParent, LPCWSTR lpszInfFile,
1058 LPCWSTR lpszSrcPath, LPCWSTR lpszDrivers)
1060 clear_errors();
1061 FIXME("%p %s %s %s\n", hwndParent, debugstr_w(lpszInfFile),
1062 debugstr_w(lpszSrcPath), debugstr_w(lpszDrivers));
1063 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1064 return FALSE;
1067 BOOL WINAPI SQLInstallODBC(HWND hwndParent, LPCSTR lpszInfFile,
1068 LPCSTR lpszSrcPath, LPCSTR lpszDrivers)
1070 clear_errors();
1071 FIXME("%p %s %s %s\n", hwndParent, debugstr_a(lpszInfFile),
1072 debugstr_a(lpszSrcPath), debugstr_a(lpszDrivers));
1073 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1074 return FALSE;
1077 SQLRETURN WINAPI SQLInstallerErrorW(WORD iError, DWORD *pfErrorCode,
1078 LPWSTR lpszErrorMsg, WORD cbErrorMsgMax, WORD *pcbErrorMsg)
1080 TRACE("%d %p %p %d %p\n", iError, pfErrorCode, lpszErrorMsg,
1081 cbErrorMsgMax, pcbErrorMsg);
1083 if (iError == 0)
1085 return SQL_ERROR;
1087 else if (iError <= num_errors)
1089 BOOL truncated = FALSE;
1090 WORD len;
1091 LPCWSTR msg;
1092 iError--;
1093 if (pfErrorCode)
1094 *pfErrorCode = error_code[iError];
1095 msg = error_msg[iError];
1096 len = msg ? lstrlenW(msg) : 0;
1097 if (pcbErrorMsg)
1098 *pcbErrorMsg = len;
1099 len++;
1100 if (cbErrorMsgMax < len)
1102 len = cbErrorMsgMax;
1103 truncated = TRUE;
1105 if (lpszErrorMsg && len)
1107 if (msg)
1109 memcpy (lpszErrorMsg, msg, len * sizeof(WCHAR));
1111 else
1113 assert(len==1);
1114 *lpszErrorMsg = 0;
1117 else
1119 /* Yes. If you pass a null pointer and a large length it is not an error! */
1120 truncated = TRUE;
1123 return truncated ? SQL_SUCCESS_WITH_INFO : SQL_SUCCESS;
1126 /* 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 */
1127 if (pcbErrorMsg)
1128 *pcbErrorMsg = 0;
1130 if (lpszErrorMsg && cbErrorMsgMax > 0)
1131 *lpszErrorMsg = '\0';
1133 return SQL_NO_DATA;
1136 SQLRETURN WINAPI SQLInstallerError(WORD iError, DWORD *pfErrorCode,
1137 LPSTR lpszErrorMsg, WORD cbErrorMsgMax, WORD *pcbErrorMsg)
1139 SQLRETURN ret;
1140 LPWSTR wbuf;
1141 WORD cbwbuf;
1142 TRACE("%d %p %p %d %p\n", iError, pfErrorCode, lpszErrorMsg,
1143 cbErrorMsgMax, pcbErrorMsg);
1145 wbuf = 0;
1146 if (lpszErrorMsg && cbErrorMsgMax)
1148 wbuf = HeapAlloc(GetProcessHeap(), 0, cbErrorMsgMax*sizeof(WCHAR));
1149 if (!wbuf)
1150 return SQL_ERROR;
1152 ret = SQLInstallerErrorW(iError, pfErrorCode, wbuf, cbErrorMsgMax, &cbwbuf);
1153 if (wbuf)
1155 WORD cbBuf = 0;
1156 SQLInstall_narrow(1, lpszErrorMsg, wbuf, cbwbuf+1, cbErrorMsgMax, &cbBuf);
1157 HeapFree(GetProcessHeap(), 0, wbuf);
1158 if (pcbErrorMsg)
1159 *pcbErrorMsg = cbBuf-1;
1161 return ret;
1164 BOOL WINAPI SQLInstallTranslatorExW(LPCWSTR lpszTranslator, LPCWSTR lpszPathIn,
1165 LPWSTR lpszPathOut, WORD cbPathOutMax, WORD *pcbPathOut,
1166 WORD fRequest, LPDWORD lpdwUsageCount)
1168 UINT len;
1169 WCHAR path[MAX_PATH];
1171 clear_errors();
1172 TRACE("%s %s %p %d %p %d %p\n", debugstr_w(lpszTranslator),
1173 debugstr_w(lpszPathIn), lpszPathOut, cbPathOutMax, pcbPathOut,
1174 fRequest, lpdwUsageCount);
1176 write_registry_values(odbctranslators, lpszTranslator, lpszPathIn, path, lpdwUsageCount);
1178 len = lstrlenW(path);
1180 if (pcbPathOut)
1181 *pcbPathOut = len;
1183 if (lpszPathOut && cbPathOutMax > len)
1185 lstrcpyW(lpszPathOut, path);
1186 return TRUE;
1188 return FALSE;
1191 BOOL WINAPI SQLInstallTranslatorEx(LPCSTR lpszTranslator, LPCSTR lpszPathIn,
1192 LPSTR lpszPathOut, WORD cbPathOutMax, WORD *pcbPathOut,
1193 WORD fRequest, LPDWORD lpdwUsageCount)
1195 LPCSTR p;
1196 LPWSTR translator, pathin;
1197 WCHAR pathout[MAX_PATH];
1198 BOOL ret;
1199 WORD cbOut = 0;
1201 clear_errors();
1202 TRACE("%s %s %p %d %p %d %p\n", debugstr_a(lpszTranslator),
1203 debugstr_a(lpszPathIn), lpszPathOut, cbPathOutMax, pcbPathOut,
1204 fRequest, lpdwUsageCount);
1206 for (p = lpszTranslator; *p; p += lstrlenA(p) + 1)
1207 TRACE("%s\n", debugstr_a(p));
1209 translator = SQLInstall_strdup_multi(lpszTranslator);
1210 pathin = SQLInstall_strdup(lpszPathIn);
1212 ret = SQLInstallTranslatorExW(translator, pathin, pathout, MAX_PATH,
1213 &cbOut, fRequest, lpdwUsageCount);
1214 if (ret)
1216 int len = WideCharToMultiByte(CP_ACP, 0, pathout, -1, lpszPathOut,
1217 0, NULL, NULL);
1218 if (len)
1220 if (pcbPathOut)
1221 *pcbPathOut = len - 1;
1223 if (!lpszPathOut || cbPathOutMax < len)
1225 ret = FALSE;
1226 goto out;
1228 len = WideCharToMultiByte(CP_ACP, 0, pathout, -1, lpszPathOut,
1229 cbPathOutMax, NULL, NULL);
1233 out:
1234 HeapFree(GetProcessHeap(), 0, translator);
1235 HeapFree(GetProcessHeap(), 0, pathin);
1236 return ret;
1239 BOOL WINAPI SQLInstallTranslator(LPCSTR lpszInfFile, LPCSTR lpszTranslator,
1240 LPCSTR lpszPathIn, LPSTR lpszPathOut, WORD cbPathOutMax,
1241 WORD *pcbPathOut, WORD fRequest, LPDWORD lpdwUsageCount)
1243 clear_errors();
1244 TRACE("%s %s %s %p %d %p %d %p\n", debugstr_a(lpszInfFile),
1245 debugstr_a(lpszTranslator), debugstr_a(lpszPathIn), lpszPathOut,
1246 cbPathOutMax, pcbPathOut, fRequest, lpdwUsageCount);
1248 if (lpszInfFile)
1249 return FALSE;
1251 return SQLInstallTranslatorEx(lpszTranslator, lpszPathIn, lpszPathOut,
1252 cbPathOutMax, pcbPathOut, fRequest, lpdwUsageCount);
1255 BOOL WINAPI SQLInstallTranslatorW(LPCWSTR lpszInfFile, LPCWSTR lpszTranslator,
1256 LPCWSTR lpszPathIn, LPWSTR lpszPathOut, WORD cbPathOutMax,
1257 WORD *pcbPathOut, WORD fRequest, LPDWORD lpdwUsageCount)
1259 clear_errors();
1260 TRACE("%s %s %s %p %d %p %d %p\n", debugstr_w(lpszInfFile),
1261 debugstr_w(lpszTranslator), debugstr_w(lpszPathIn), lpszPathOut,
1262 cbPathOutMax, pcbPathOut, fRequest, lpdwUsageCount);
1264 if (lpszInfFile)
1265 return FALSE;
1267 return SQLInstallTranslatorExW(lpszTranslator, lpszPathIn, lpszPathOut,
1268 cbPathOutMax, pcbPathOut, fRequest, lpdwUsageCount);
1271 BOOL WINAPI SQLManageDataSources(HWND hwnd)
1273 clear_errors();
1274 FIXME("%p\n", hwnd);
1275 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1276 return FALSE;
1279 SQLRETURN WINAPI SQLPostInstallerErrorW(DWORD fErrorCode, LPCWSTR szErrorMsg)
1281 FIXME("%u %s\n", fErrorCode, debugstr_w(szErrorMsg));
1282 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1283 return FALSE;
1286 SQLRETURN WINAPI SQLPostInstallerError(DWORD fErrorCode, LPCSTR szErrorMsg)
1288 FIXME("%u %s\n", fErrorCode, debugstr_a(szErrorMsg));
1289 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1290 return FALSE;
1293 BOOL WINAPI SQLReadFileDSNW(LPCWSTR lpszFileName, LPCWSTR lpszAppName,
1294 LPCWSTR lpszKeyName, LPWSTR lpszString, WORD cbString,
1295 WORD *pcbString)
1297 clear_errors();
1298 FIXME("%s %s %s %s %d %p\n", debugstr_w(lpszFileName), debugstr_w(lpszAppName),
1299 debugstr_w(lpszKeyName), debugstr_w(lpszString), cbString, pcbString);
1300 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1301 return FALSE;
1304 BOOL WINAPI SQLReadFileDSN(LPCSTR lpszFileName, LPCSTR lpszAppName,
1305 LPCSTR lpszKeyName, LPSTR lpszString, WORD cbString,
1306 WORD *pcbString)
1308 clear_errors();
1309 FIXME("%s %s %s %s %d %p\n", debugstr_a(lpszFileName), debugstr_a(lpszAppName),
1310 debugstr_a(lpszKeyName), debugstr_a(lpszString), cbString, pcbString);
1311 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1312 return FALSE;
1315 BOOL WINAPI SQLRemoveDefaultDataSource(void)
1317 clear_errors();
1318 FIXME("\n");
1319 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1320 return FALSE;
1323 BOOL WINAPI SQLRemoveDriverW(LPCWSTR drivername, BOOL remove_dsn, LPDWORD usage_count)
1325 HKEY hkey;
1326 DWORD usagecount = 1;
1328 clear_errors();
1329 TRACE("%s %d %p\n", debugstr_w(drivername), remove_dsn, usage_count);
1331 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey) == ERROR_SUCCESS)
1333 HKEY hkeydriver;
1335 if (RegOpenKeyW(hkey, drivername, &hkeydriver) == ERROR_SUCCESS)
1337 DWORD size, type;
1338 DWORD count;
1340 size = sizeof(usagecount);
1341 RegGetValueA(hkeydriver, NULL, "UsageCount", RRF_RT_DWORD, &type, &usagecount, &size);
1342 TRACE("Usage count %d\n", usagecount);
1343 count = usagecount - 1;
1344 if (count)
1346 if (RegSetValueExA(hkeydriver, "UsageCount", 0, REG_DWORD, (BYTE*)&count, sizeof(count)) != ERROR_SUCCESS)
1347 ERR("Failed to write registry UsageCount key\n");
1350 RegCloseKey(hkeydriver);
1353 if (usagecount)
1354 usagecount--;
1356 if (!usagecount)
1358 if (RegDeleteKeyW(hkey, drivername) != ERROR_SUCCESS)
1359 ERR("Failed to delete registry key: %s\n", debugstr_w(drivername));
1361 if (RegOpenKeyW(hkey, odbcdrivers, &hkeydriver) == ERROR_SUCCESS)
1363 if(RegDeleteValueW(hkeydriver, drivername) != ERROR_SUCCESS)
1364 ERR("Failed to delete registry value: %s\n", debugstr_w(drivername));
1365 RegCloseKey(hkeydriver);
1369 RegCloseKey(hkey);
1372 if (usage_count)
1373 *usage_count = usagecount;
1375 return TRUE;
1378 BOOL WINAPI SQLRemoveDriver(LPCSTR lpszDriver, BOOL fRemoveDSN,
1379 LPDWORD lpdwUsageCount)
1381 WCHAR *driver;
1382 BOOL ret;
1384 clear_errors();
1385 TRACE("%s %d %p\n", debugstr_a(lpszDriver), fRemoveDSN, lpdwUsageCount);
1387 driver = SQLInstall_strdup(lpszDriver);
1389 ret = SQLRemoveDriverW(driver, fRemoveDSN, lpdwUsageCount);
1391 HeapFree(GetProcessHeap(), 0, driver);
1392 return ret;
1395 BOOL WINAPI SQLRemoveDriverManager(LPDWORD pdwUsageCount)
1397 clear_errors();
1398 FIXME("%p\n", pdwUsageCount);
1399 if (pdwUsageCount) *pdwUsageCount = 1;
1400 return TRUE;
1403 BOOL WINAPI SQLRemoveDSNFromIniW(LPCWSTR lpszDSN)
1405 clear_errors();
1406 FIXME("%s\n", debugstr_w(lpszDSN));
1407 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1408 return FALSE;
1411 BOOL WINAPI SQLRemoveDSNFromIni(LPCSTR lpszDSN)
1413 clear_errors();
1414 FIXME("%s\n", debugstr_a(lpszDSN));
1415 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1416 return FALSE;
1419 BOOL WINAPI SQLRemoveTranslatorW(const WCHAR *translator, DWORD *usage_count)
1421 HKEY hkey;
1422 DWORD usagecount = 1;
1423 BOOL ret = TRUE;
1425 clear_errors();
1426 TRACE("%s %p\n", debugstr_w(translator), usage_count);
1428 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, odbcini, &hkey) == ERROR_SUCCESS)
1430 HKEY hkeydriver;
1432 if (RegOpenKeyW(hkey, translator, &hkeydriver) == ERROR_SUCCESS)
1434 DWORD size, type;
1435 DWORD count;
1437 size = sizeof(usagecount);
1438 RegGetValueA(hkeydriver, NULL, "UsageCount", RRF_RT_DWORD, &type, &usagecount, &size);
1439 TRACE("Usage count %d\n", usagecount);
1440 count = usagecount - 1;
1441 if (count)
1443 if (RegSetValueExA(hkeydriver, "UsageCount", 0, REG_DWORD, (BYTE*)&count, sizeof(count)) != ERROR_SUCCESS)
1444 ERR("Failed to write registry UsageCount key\n");
1447 RegCloseKey(hkeydriver);
1450 if (usagecount)
1451 usagecount--;
1453 if (!usagecount)
1455 if(RegDeleteKeyW(hkey, translator) != ERROR_SUCCESS)
1457 push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
1458 WARN("Failed to delete registry key: %s\n", debugstr_w(translator));
1459 ret = FALSE;
1462 if (ret && RegOpenKeyW(hkey, odbctranslators, &hkeydriver) == ERROR_SUCCESS)
1464 if(RegDeleteValueW(hkeydriver, translator) != ERROR_SUCCESS)
1466 push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
1467 WARN("Failed to delete registry key: %s\n", debugstr_w(translator));
1468 ret = FALSE;
1471 RegCloseKey(hkeydriver);
1475 RegCloseKey(hkey);
1478 if (ret && usage_count)
1479 *usage_count = usagecount;
1481 return ret;
1484 BOOL WINAPI SQLRemoveTranslator(LPCSTR lpszTranslator, LPDWORD lpdwUsageCount)
1486 WCHAR *translator;
1487 BOOL ret;
1489 clear_errors();
1490 TRACE("%s %p\n", debugstr_a(lpszTranslator), lpdwUsageCount);
1492 translator = SQLInstall_strdup(lpszTranslator);
1493 ret = SQLRemoveTranslatorW(translator, lpdwUsageCount);
1495 HeapFree(GetProcessHeap(), 0, translator);
1496 return ret;
1499 BOOL WINAPI SQLSetConfigMode(UWORD wConfigMode)
1501 clear_errors();
1502 TRACE("%u\n", wConfigMode);
1504 if (wConfigMode > ODBC_SYSTEM_DSN)
1506 push_error(ODBC_ERROR_INVALID_PARAM_SEQUENCE, odbc_error_invalid_param_sequence);
1507 return FALSE;
1509 else
1511 config_mode = wConfigMode;
1512 return TRUE;
1516 BOOL WINAPI SQLValidDSNW(LPCWSTR lpszDSN)
1518 static const WCHAR invalid[] = {'[',']','{','}','(',')',',',';','?','*','=','!','@','\\',0};
1519 clear_errors();
1520 TRACE("%s\n", debugstr_w(lpszDSN));
1522 if(strlenW(lpszDSN) > SQL_MAX_DSN_LENGTH || strpbrkW(lpszDSN, invalid) != NULL)
1524 return FALSE;
1527 return TRUE;
1530 BOOL WINAPI SQLValidDSN(LPCSTR lpszDSN)
1532 static const char *invalid = "[]{}(),;?*=!@\\";
1533 clear_errors();
1534 TRACE("%s\n", debugstr_a(lpszDSN));
1536 if(strlen(lpszDSN) > SQL_MAX_DSN_LENGTH || strpbrk(lpszDSN, invalid) != NULL)
1538 return FALSE;
1541 return TRUE;
1544 BOOL WINAPI SQLWriteDSNToIniW(LPCWSTR lpszDSN, LPCWSTR lpszDriver)
1546 clear_errors();
1547 FIXME("%s %s\n", debugstr_w(lpszDSN), debugstr_w(lpszDriver));
1548 return TRUE;
1551 BOOL WINAPI SQLWriteDSNToIni(LPCSTR lpszDSN, LPCSTR lpszDriver)
1553 clear_errors();
1554 FIXME("%s %s\n", debugstr_a(lpszDSN), debugstr_a(lpszDriver));
1555 return TRUE;
1558 BOOL WINAPI SQLWriteFileDSNW(LPCWSTR lpszFileName, LPCWSTR lpszAppName,
1559 LPCWSTR lpszKeyName, LPCWSTR lpszString)
1561 clear_errors();
1562 FIXME("%s %s %s %s\n", debugstr_w(lpszFileName), debugstr_w(lpszAppName),
1563 debugstr_w(lpszKeyName), debugstr_w(lpszString));
1564 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1565 return FALSE;
1568 BOOL WINAPI SQLWriteFileDSN(LPCSTR lpszFileName, LPCSTR lpszAppName,
1569 LPCSTR lpszKeyName, LPCSTR lpszString)
1571 clear_errors();
1572 FIXME("%s %s %s %s\n", debugstr_a(lpszFileName), debugstr_a(lpszAppName),
1573 debugstr_a(lpszKeyName), debugstr_a(lpszString));
1574 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1575 return FALSE;
1578 BOOL WINAPI SQLWritePrivateProfileStringW(LPCWSTR lpszSection, LPCWSTR lpszEntry,
1579 LPCWSTR lpszString, LPCWSTR lpszFilename)
1581 static const WCHAR empty[] = {0};
1582 LONG ret;
1583 HKEY hkey;
1585 clear_errors();
1586 TRACE("%s %s %s %s\n", debugstr_w(lpszSection), debugstr_w(lpszEntry),
1587 debugstr_w(lpszString), debugstr_w(lpszFilename));
1589 if(!lpszFilename || !*lpszFilename)
1591 push_error(ODBC_ERROR_INVALID_STR, odbc_error_invalid_param_string);
1592 return FALSE;
1595 if ((ret = RegCreateKeyW(HKEY_CURRENT_USER, odbcW, &hkey)) == ERROR_SUCCESS)
1597 HKEY hkeyfilename;
1599 if ((ret = RegCreateKeyW(hkey, lpszFilename, &hkeyfilename)) == ERROR_SUCCESS)
1601 HKEY hkey_section;
1603 if ((ret = RegCreateKeyW(hkeyfilename, lpszSection, &hkey_section)) == ERROR_SUCCESS)
1605 if(lpszString)
1606 ret = RegSetValueExW(hkey_section, lpszEntry, 0, REG_SZ, (BYTE*)lpszString, (lstrlenW(lpszString)+1)*sizeof(WCHAR));
1607 else
1608 ret = RegSetValueExW(hkey_section, lpszEntry, 0, REG_SZ, (BYTE*)empty, sizeof(empty));
1609 RegCloseKey(hkey_section);
1612 RegCloseKey(hkeyfilename);
1615 RegCloseKey(hkey);
1618 return ret == ERROR_SUCCESS;
1621 BOOL WINAPI SQLWritePrivateProfileString(LPCSTR lpszSection, LPCSTR lpszEntry,
1622 LPCSTR lpszString, LPCSTR lpszFilename)
1624 BOOL ret;
1625 WCHAR *sect, *entry, *string, *file;
1626 clear_errors();
1627 TRACE("%s %s %s %s\n", lpszSection, lpszEntry, lpszString, lpszFilename);
1629 sect = heap_strdupAtoW(lpszSection);
1630 entry = heap_strdupAtoW(lpszEntry);
1631 string = heap_strdupAtoW(lpszString);
1632 file = heap_strdupAtoW(lpszFilename);
1634 ret = SQLWritePrivateProfileStringW(sect, entry, string, file);
1636 heap_free(sect);
1637 heap_free(entry);
1638 heap_free(string);
1639 heap_free(file);
1641 return ret;