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, *msg;
00070     int i, op;
00071 
00072     GetModuleFileName(NULL, tmp, sizeof (tmp));
00073     p = tmp;
00074     while (*p) {
00075         *p = tolower(*p);
00076         ++p;
00077     }
00078     p = strrchr(tmp, '\\');
00079     if (p == NULL) {
00080         p = tmp;
00081     }
00082     op = ODBC_ADD_DSN;
00083     msg = "Adding DSN";
00084     if (strstr(p, "rem") != NULL) {
00085         msg = "Removing DSN";
00086         op = ODBC_REMOVE_DSN;
00087     }
00088     if (strstr(p, "sys") != NULL) {
00089         if (op == ODBC_REMOVE_DSN) {
00090             op = ODBC_REMOVE_SYS_DSN;
00091         } else {
00092             op = ODBC_ADD_SYS_DSN;
00093         }
00094     }
00095     strncpy(tmp, lpszCmdLine, sizeof (tmp));
00096     /* get driver argument */
00097     i = strspn(tmp, "\"");
00098     drv = tmp + i;
00099     if (i > 0) {
00100         i = strcspn(drv, "\"");
00101         drv[i] = '\0';
00102         cfg = drv + i + 1;
00103     } else {
00104         i = strcspn(drv, " \t");
00105         if (i > 0) {
00106             drv[i] = '\0';
00107             cfg = drv + i + 1;
00108         } else {
00109             cfg = "\0\0";
00110         }
00111     }
00112     if (strlen(drv) == 0) {
00113         MessageBox(NULL, "No driver name given", msg,
00114                    MB_ICONERROR|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND);
00115         exit(1);
00116     }
00117     i = strspn(cfg, " \t;");
00118     cfg += i;
00119     i = strlen(cfg);
00120     cfg[i + 1] = '\0';
00121     if (i > 0) {
00122         p = cfg;
00123         do {
00124             p = strchr(p, ';');
00125             if (p != NULL) {
00126                 p[0] = '\0';
00127                 p += 1;
00128             }
00129         } while (p != NULL);
00130     }
00131     p = cfg;
00132     if (SQLConfigDataSource(NULL, (WORD) op, drv, cfg)) {
00133         exit(0);
00134     }
00135     ProcessErrorMessages(msg);
00136     exit(1);
00137 }
00138 

Generated on 23 Oct 2023 by doxygen.
Contact: chw@ch-werner.de