Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

adddsn.c

Go to the documentation of this file.
00001 
00014 #ifndef _WIN32
00015 #error "only WIN32 supported"
00016 #endif
00017 #include <windows.h>
00018 #include <sql.h>
00019 #include <sqlext.h>
00020 #include <odbcinst.h>
00021 #include <winver.h>
00022 #include <string.h>
00023 #include <ctype.h>
00024 #include <stdio.h>
00025 
00031 static BOOL
00032 ProcessErrorMessages(char *name)
00033 {
00034     WORD err = 1;
00035     DWORD code;
00036     char errmsg[301];
00037     WORD errlen, errmax = sizeof (errmsg) - 1;
00038     int rc;
00039     BOOL ret = FALSE;
00040 
00041     do {
00042         errmsg[0] = '\0';
00043         rc = SQLInstallerError(err, &code, errmsg, errmax, &errlen);
00044         if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) {
00045             MessageBox(NULL, errmsg, name,
00046                        MB_ICONSTOP|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND);
00047             ret = TRUE;
00048         }
00049         err++;
00050     } while (rc != SQL_NO_DATA);
00051     return ret;
00052 }
00053 
00065 int APIENTRY
00066 WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
00067         LPSTR lpszCmdLine, int nCmdShow)
00068 {
00069     char tmp[1024], *p, *drv, *cfg, st[16], *msg;
00070     SQLINTEGER naterr;
00071     SQLSMALLINT len;
00072     int i, op;
00073 
00074     GetModuleFileName(NULL, tmp, sizeof (tmp));
00075     p = tmp;
00076     while (*p) {
00077         *p = tolower(*p);
00078         ++p;
00079     }
00080     p = strrchr(tmp, '\\');
00081     if (p == NULL) {
00082         p = tmp;
00083     }
00084     op = ODBC_ADD_DSN;
00085     msg = "Adding DSN";
00086     if (strstr(p, "rem") != NULL) {
00087         msg = "Removing DSN";
00088         op = ODBC_REMOVE_DSN;
00089     }
00090     if (strstr(p, "sys") != NULL) {
00091         if (op == ODBC_REMOVE_DSN) {
00092             op = ODBC_REMOVE_SYS_DSN;
00093         } else {
00094             op = ODBC_ADD_SYS_DSN;
00095         }
00096     }
00097     strncpy(tmp, lpszCmdLine, sizeof (tmp));
00098     /* get driver argument */
00099     i = strspn(tmp, "\"");
00100     drv = tmp + i;
00101     if (i > 0) {
00102         i = strcspn(drv, "\"");
00103         drv[i] = '\0';
00104         cfg = drv + i + 1;
00105     } else {
00106         i = strcspn(drv, " \t");
00107         if (i > 0) {
00108             drv[i] = '\0';
00109             cfg = drv + i + 1;
00110         } else {
00111             cfg = "\0\0";
00112         }
00113     }
00114     if (strlen(drv) == 0) {
00115         MessageBox(NULL, "No driver name given", msg,
00116                    MB_ICONERROR|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND);
00117         exit(1);
00118     }
00119     i = strspn(cfg, " \t;");
00120     cfg += i;
00121     i = strlen(cfg);
00122     cfg[i + 1] = '\0';
00123     if (i > 0) {
00124         p = cfg;
00125         do {
00126             p = strchr(p, ';');
00127             if (p != NULL) {
00128                 p[0] = '\0';
00129                 p += 1;
00130             }
00131         } while (p != NULL);
00132     }
00133     p = cfg;
00134     if (SQLConfigDataSource(NULL, (WORD) op, drv, cfg)) {
00135         exit(0);
00136     }
00137     ProcessErrorMessages(msg);
00138     exit(1);
00139 }
00140 

Generated on 25 Feb 2005 by doxygen.
Contact: chw@ch-werner.de