1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
/**
* @file inst.c
* SQLite ODBC Driver installer/uninstaller for WIN32
*
* $Id: inst.c,v 1.13 2011/03/09 05:41:01 chw Exp chw $
*
* Copyright (c) 2001-2011 Christian Werner <chw@ch-werner.de>
*
* See the file "license.terms" for information on usage
* and redistribution of this file and for a
* DISCLAIMER OF ALL WARRANTIES.
*/
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <odbcinst.h>
#include <winver.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#ifdef _WIN64
#define NUMDRVS 1
static char *DriverName[NUMDRVS] = {
"SQLite3 ODBC Driver"
};
static char *DSName[NUMDRVS] = {
"SQLite3 Datasource"
};
static char *DriverDLL[NUMDRVS] = {
"sqlite3odbc.dll"
};
#ifdef WITH_SQLITE_DLLS
static char *EngineDLL[NUMDRVS] = {
"sqlite3.dll"
};
#endif
#else
#define NUMDRVS 3
static char *DriverName[NUMDRVS] = {
"SQLite ODBC Driver",
"SQLite ODBC (UTF-8) Driver",
"SQLite3 ODBC Driver"
};
static char *DSName[NUMDRVS] = {
"SQLite Datasource",
"SQLite UTF-8 Datasource",
"SQLite3 Datasource"
};
static char *DriverDLL[NUMDRVS] = {
"sqliteodbc.dll",
"sqliteodbcu.dll",
"sqlite3odbc.dll"
};
#ifdef WITH_SQLITE_DLLS
static char *EngineDLL[NUMDRVS] = {
"sqlite.dll",
"sqliteu.dll",
"sqlite3.dll"
};
#endif
#endif
static int quiet = 0;
static int nosys = 0;
/**
* Handler for ODBC installation error messages.
* @param name name of API function for which to show error messages
*/
static BOOL
ProcessErrorMessages(char *name)
{
WORD err = 1;
DWORD code;
char errmsg[301];
WORD errlen, errmax = sizeof (errmsg) - 1;
int rc;
BOOL ret = FALSE;
do {
errmsg[0] = '\0';
rc = SQLInstallerError(err, &code, errmsg, errmax, &errlen);
if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) {
MessageBox(NULL, errmsg, name,
MB_ICONSTOP|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND);
ret = TRUE;
}
err++;
} while (rc != SQL_NO_DATA);
return ret;
}
/**
* Copy or delete SQLite3 module DLLs
* @param dllname file name of driver DLL
* @param path install directory for modules
* @param del flag, when true, delete DLLs in install directory
*/
static BOOL
CopyOrDelModules(char *dllname, char *path, BOOL del)
{
char firstpat[MAX_PATH];
WIN32_FIND_DATA fdata;
HANDLE h;
DWORD err;
if (strncmp(dllname, "sqlite3", 7)) {
return TRUE;
}
firstpat[0] = '\0';
if (del) {
strcpy(firstpat, path);
strcat(firstpat, "\\");
}
strcat(firstpat, "sqlite3_mod*.dll");
h = FindFirstFile(firstpat, &fdata);
if (h == INVALID_HANDLE_VALUE) {
return TRUE;
}
do {
if (del) {
DeleteFile(fdata.cFileName);
} else {
char buf[1024];
sprintf(buf, "%s\\%s", path, fdata.cFileName);
if (!CopyFile(fdata.cFileName, buf, 0)) {
sprintf(buf, "Copy %s to %s failed", fdata.cFileName, path);
MessageBox(NULL, buf, "CopyFile",
MB_ICONSTOP|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND);
FindClose(h);
return FALSE;
}
}
} while (FindNextFile(h, &fdata));
err = GetLastError();
FindClose(h);
return err == ERROR_NO_MORE_FILES;
}
/**
* Driver installer/uninstaller.
* @param remove true for uninstall
* @param drivername print name of driver
* @param dllname file name of driver DLL
* @param dll2name file name of additional DLL
* @param dsname name for data source
*/
static BOOL
InUn(int remove, char *drivername, char *dllname, char *dll2name, char *dsname)
{
char path[301], driver[300], attr[300], inst[400], inst2[400];
WORD pathmax = sizeof (path) - 1, pathlen;
DWORD usecnt, mincnt;
if (SQLInstallDriverManager(path, pathmax, &pathlen)) {
char *p;
sprintf(driver, "%s;Driver=%s;Setup=%s;",
drivername, dllname, dllname);
p = driver;
while (*p) {
if (*p == ';') {
*p = '\0';
}
++p;
}
usecnt = 0;
SQLInstallDriverEx(driver, NULL, path, pathmax, &pathlen,
ODBC_INSTALL_INQUIRY, &usecnt);
sprintf(driver, "%s;Driver=%s\\%s;Setup=%s\\%s;",
drivername, path, dllname, path, dllname);
p = driver;
while (*p) {
if (*p == ';') {
*p = '\0';
}
++p;
}
sprintf(inst, "%s\\%s", path, dllname);
if (dll2name) {
sprintf(inst2, "%s\\%s", path, dll2name);
}
if (!remove && usecnt > 0) {
/* first install try: copy over driver dll, keeping DSNs */
if (GetFileAttributes(dllname) != 0xFFFFFFFF &&
CopyFile(dllname, inst, 0) &&
CopyOrDelModules(dllname, path, 0)) {
if (dll2name != NULL) {
CopyFile(dll2name, inst2, 0);
}
return TRUE;
}
}
mincnt = remove ? 1 : 0;
while (usecnt != mincnt) {
if (!SQLRemoveDriver(driver, TRUE, &usecnt)) {
break;
}
}
if (remove) {
if (!SQLRemoveDriver(driver, TRUE, &usecnt)) {
ProcessErrorMessages("SQLRemoveDriver");
return FALSE;
}
if (!usecnt) {
char buf[512];
DeleteFile(inst);
/* but keep inst2 */
CopyOrDelModules(dllname, path, 1);
if (!quiet) {
sprintf(buf, "%s uninstalled.", drivername);
MessageBox(NULL, buf, "Info",
MB_ICONINFORMATION|MB_OK|MB_TASKMODAL|
MB_SETFOREGROUND);
}
}
if (nosys) {
goto done;
}
sprintf(attr, "DSN=%s;Database=sqlite.db;", dsname);
p = attr;
while (*p) {
if (*p == ';') {
*p = '\0';
}
++p;
}
SQLConfigDataSource(NULL, ODBC_REMOVE_SYS_DSN, drivername, attr);
goto done;
}
if (GetFileAttributes(dllname) == 0xFFFFFFFF) {
return FALSE;
}
if (!CopyFile(dllname, inst, 0)) {
char buf[512];
sprintf(buf, "Copy %s to %s failed", dllname, inst);
MessageBox(NULL, buf, "CopyFile",
MB_ICONSTOP|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND);
return FALSE;
}
if (dll2name != NULL && !CopyFile(dll2name, inst2, 0)) {
char buf[512];
sprintf(buf, "Copy %s to %s failed", dll2name, inst2);
MessageBox(NULL, buf, "CopyFile",
MB_ICONSTOP|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND);
/* but go on hoping that an SQLite engine is in place */
}
if (!CopyOrDelModules(dllname, path, 0)) {
return FALSE;
}
if (!SQLInstallDriverEx(driver, path, path, pathmax, &pathlen,
ODBC_INSTALL_COMPLETE, &usecnt)) {
ProcessErrorMessages("SQLInstallDriverEx");
return FALSE;
}
if (nosys) {
goto done;
}
sprintf(attr, "DSN=%s;Database=sqlite.db;", dsname);
p = attr;
while (*p) {
if (*p == ';') {
*p = '\0';
}
++p;
}
SQLConfigDataSource(NULL, ODBC_REMOVE_SYS_DSN, drivername, attr);
if (!SQLConfigDataSource(NULL, ODBC_ADD_SYS_DSN, drivername, attr)) {
ProcessErrorMessages("SQLConfigDataSource");
return FALSE;
}
} else {
ProcessErrorMessages("SQLInstallDriverManager");
return FALSE;
}
done:
return TRUE;
}
/**
* Main function of installer/uninstaller.
* This is the Win32 GUI main entry point.
* It (un)registers the ODBC driver(s) and deletes or
* copies the driver DLL(s) to the system folder.
*/
int APIENTRY
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
char path[300], *p;
int i, remove;
BOOL ret[3];
GetModuleFileName(NULL, path, sizeof (path));
p = path;
while (*p) {
*p = tolower(*p);
++p;
}
p = strrchr(path, '\\');
if (p == NULL) {
p = path;
} else {
*p = '\0';
++p;
SetCurrentDirectory(path);
}
remove = strstr(p, "uninst") != NULL;
quiet = strstr(p, "instq") != NULL;
nosys = strstr(p, "nosys") != NULL;
for (i = 0; i < NUMDRVS; i++) {
#ifdef WITH_SQLITE_DLLS
p = EngineDLL[i];
#else
p = NULL;
#endif
ret[i] = InUn(remove, DriverName[i], DriverDLL[i], p, DSName[i]);
}
for (i = 1; i < NUMDRVS; i++) {
ret[0] = ret[0] || ret[i];
}
if (!remove && ret[0]) {
if (!quiet) {
MessageBox(NULL, "SQLite ODBC Driver(s) installed.", "Info",
MB_ICONINFORMATION|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND);
}
}
exit(0);
}
|