Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
12 KiB

  1. /***************************************************************************/
  2. /* SETUP.C */
  3. /* Copyright (C) 1995-96 SYWARE Inc., All rights reserved */
  4. /***************************************************************************/
  5. // Commenting #define out - causing compiler error - not sure if needed, compiles
  6. // okay without it.
  7. //#define WINVER 0x0400
  8. #include "precomp.h"
  9. #include "wbemidl.h"
  10. #include <comdef.h>
  11. //smart pointer
  12. _COM_SMARTPTR_TYPEDEF(IWbemServices, IID_IWbemServices);
  13. _COM_SMARTPTR_TYPEDEF(IEnumWbemClassObject, IID_IEnumWbemClassObject);
  14. //_COM_SMARTPTR_TYPEDEF(IWbemContext, IID_IWbemContext );
  15. _COM_SMARTPTR_TYPEDEF(IWbemLocator, IID_IWbemLocator);
  16. #include "drdbdr.h"
  17. #include "odbcinst.h"
  18. /***************************************************************************/
  19. typedef struct tagSETUP {
  20. WORD fRequest;
  21. char szOriginalDSN[SQL_MAX_DSN_LENGTH+1];
  22. char szDSN[SQL_MAX_DSN_LENGTH+1];
  23. char szDatabase[MAX_DATABASE_NAME_LENGTH+1];
  24. char szUsername[MAX_USER_NAME_LENGTH+1];
  25. char szPassword[MAX_PASSWORD_LENGTH+1];
  26. char szHost[MAX_HOST_NAME_LENGTH+1];
  27. char szPort[MAX_PORT_NUMBER_LENGTH+1];
  28. } SETUP, FAR *LPSETUP;
  29. /***************************************************************************/
  30. extern "C" BOOL EXPFUNC dlgSetup (
  31. HWND hDlg,
  32. UINT message,
  33. WPARAM wParam,
  34. LPARAM lParam)
  35. {
  36. LPSETUP lpSetup;
  37. switch (message) {
  38. case WM_INITDIALOG:
  39. lpSetup = (LPSETUP) lParam;
  40. SendDlgItemMessage(hDlg, DSN_NAME, EM_LIMITTEXT,
  41. SQL_MAX_DSN_LENGTH, 0L);
  42. SendDlgItemMessage(hDlg, DATABASE_NAME, EM_LIMITTEXT,
  43. MAX_DATABASE_NAME_LENGTH, 0L);
  44. SendDlgItemMessage(hDlg, HOST_NAME, EM_LIMITTEXT,
  45. MAX_HOST_NAME_LENGTH, 0L);
  46. SendDlgItemMessage(hDlg, PORT_NUMBER, EM_LIMITTEXT,
  47. MAX_PORT_NUMBER_LENGTH, 0L);
  48. SetWindowLong(hDlg, DWL_USER, (LPARAM) lpSetup);
  49. SetDlgItemText(hDlg, DSN_NAME, (LPSTR) (lpSetup->szDSN));
  50. SetDlgItemText(hDlg, DATABASE_NAME, (LPSTR) (lpSetup->szDatabase));
  51. SetDlgItemText(hDlg, HOST_NAME, (LPSTR) (lpSetup->szHost));
  52. SetDlgItemText(hDlg, PORT_NUMBER, (LPSTR) (lpSetup->szPort));
  53. if ((lpSetup->fRequest == ODBC_ADD_DSN) &&
  54. (lstrlen((char*)lpSetup->szDSN) != 0))
  55. EnableWindow(GetDlgItem(hDlg, DSN_NAME), FALSE);
  56. return (TRUE);
  57. case WM_COMMAND:
  58. switch (GET_WM_COMMAND_ID(wParam, lParam)) {
  59. case IDOK:
  60. /* Get data source name */
  61. lpSetup = (LPSETUP) GetWindowLong(hDlg, DWL_USER);
  62. GetDlgItemText(hDlg, DSN_NAME, (char*)lpSetup->szDSN,
  63. SQL_MAX_DSN_LENGTH);
  64. /* Get database name */
  65. GetDlgItemText(hDlg, DATABASE_NAME, (char*)lpSetup->szDatabase,
  66. MAX_DATABASE_NAME_LENGTH);
  67. AnsiToOem((char*)lpSetup->szDatabase, (char*)lpSetup->szDatabase);
  68. /* Clear off leading blanks */
  69. while (*lpSetup->szDatabase == ' ')
  70. lstrcpy((char*)lpSetup->szDatabase, (char*) (lpSetup->szDatabase+1));
  71. /* Clear off trailing blanks */
  72. while (*lpSetup->szDatabase != '\0') {
  73. if (lpSetup->szDatabase[lstrlen((char*)lpSetup->szDatabase)-1] != ' ')
  74. break;
  75. lpSetup->szDatabase[lstrlen((char*)lpSetup->szDatabase)-1] = '\0';
  76. }
  77. /* Get rid of terminating backslash (if any) */
  78. if (lstrlen((char*)lpSetup->szDatabase) > 0) {
  79. if (lpSetup->szDatabase[lstrlen((char*)lpSetup->szDatabase)-1] == '\\')
  80. lpSetup->szDatabase[lstrlen((char*)lpSetup->szDatabase)-1] = '\0';
  81. }
  82. else {
  83. /* If no directory specified, use the current directory */
  84. lstrcpy((char*)lpSetup->szDatabase, ".");
  85. }
  86. /* Get host name */
  87. GetDlgItemText(hDlg, HOST_NAME, lpSetup->szHost,
  88. MAX_HOST_NAME_LENGTH);
  89. /* Clear off leading blanks */
  90. while (*lpSetup->szHost == ' ')
  91. lstrcpy(lpSetup->szHost, lpSetup->szHost+1);
  92. /* Clear off trailing blanks */
  93. while (*lpSetup->szHost != '\0') {
  94. if (lpSetup->szHost[lstrlen(lpSetup->szHost)-1] != ' ')
  95. break;
  96. lpSetup->szHost[lstrlen(lpSetup->szHost)-1] = '\0';
  97. }
  98. /* Get port number */
  99. GetDlgItemText(hDlg, PORT_NUMBER, lpSetup->szPort,
  100. MAX_PORT_NUMBER_LENGTH);
  101. /* Clear off leading blanks */
  102. while (*lpSetup->szPort == ' ')
  103. lstrcpy(lpSetup->szPort, lpSetup->szPort+1);
  104. /* Clear off trailing blanks */
  105. while (*lpSetup->szPort != '\0') {
  106. if (lpSetup->szPort[lstrlen(lpSetup->szPort)-1] != ' ')
  107. break;
  108. lpSetup->szPort[lstrlen(lpSetup->szPort)-1] = '\0';
  109. }
  110. /* Return */
  111. EndDialog(hDlg, TRUE);
  112. return (TRUE);
  113. case IDCANCEL:
  114. EndDialog(hDlg, FALSE);
  115. return (TRUE);
  116. }
  117. break;
  118. }
  119. return (FALSE);
  120. }
  121. /***************************************************************************/
  122. extern "C" BOOL INSTAPI ConfigDSN(HWND hwnd,
  123. WORD fRequest,
  124. LPCSTR lpszDriver,
  125. LPCSTR lpszAttributes)
  126. {
  127. SETUP sSetup;
  128. LPSTR ptr;
  129. LPSTR lpszKeyword;
  130. LPSTR lpszValue;
  131. DLGPROC lpProc;
  132. char szDriver[MAX_DRIVER_LENGTH+1];
  133. char szTitle[100];
  134. char szMessage[128];
  135. /* Parse the attribute string */
  136. sSetup.fRequest = fRequest;
  137. lstrcpy((char*)sSetup.szOriginalDSN, "");
  138. lstrcpy((char*)sSetup.szDSN, "");
  139. lstrcpy((char*)sSetup.szDatabase, "");
  140. lstrcpy((char*)sSetup.szUsername, "");
  141. lstrcpy((char*)sSetup.szPassword, "");
  142. lstrcpy(sSetup.szHost, "");
  143. lstrcpy(sSetup.szPort, "");
  144. ptr = (LPSTR) lpszAttributes;
  145. while ( (lpszAttributes != NULL) && *ptr) {
  146. /* Point at start of next keyword */
  147. lpszKeyword = ptr;
  148. /* Find the end of the keyword */
  149. while ((*ptr != '\0') && (*ptr != '='))
  150. ptr++;
  151. /* Accomodate bug in Microsoft ODBC Administrator */
  152. if (*ptr == '\0') {
  153. ptr++;
  154. continue;
  155. }
  156. /* Error if no value */
  157. if ((*ptr == '\0') || (ptr == lpszKeyword))
  158. return FALSE;
  159. /* Put a zero terminator on the keyword */
  160. *ptr = '\0';
  161. ptr++;
  162. /* Point at start of the keyword's value */
  163. lpszValue = ptr;
  164. /* Find the end of the value */
  165. while (*ptr != '\0')
  166. ptr++;
  167. /* Save the keyword */
  168. if (!lstrcmpi(lpszKeyword, KEY_DSN)) {
  169. lstrcpy((char*)sSetup.szOriginalDSN, lpszValue);
  170. lstrcpy((char*)sSetup.szDSN, lpszValue);
  171. }
  172. else if (!lstrcmpi(lpszKeyword, KEY_DATABASE))
  173. lstrcpy((char*)sSetup.szDatabase, lpszValue);
  174. else if (!lstrcmpi(lpszKeyword, KEY_USERNAME))
  175. lstrcpy((char*)sSetup.szUsername, lpszValue);
  176. else if (!lstrcmpi(lpszKeyword, KEY_PASSWORD))
  177. lstrcpy((char*)sSetup.szPassword, lpszValue);
  178. else if (!lstrcmpi(lpszKeyword, KEY_HOST))
  179. lstrcpy(sSetup.szHost, lpszValue);
  180. else if (!lstrcmpi(lpszKeyword, KEY_PORT))
  181. lstrcpy(sSetup.szPort, lpszValue);
  182. /* Restore the input string */
  183. lpszKeyword[lstrlen(lpszKeyword)] = '=';
  184. ptr++;
  185. }
  186. /* Which configuration operation? */
  187. switch (fRequest) {
  188. case ODBC_REMOVE_DSN:
  189. /* Remove. Error if none specified */
  190. if (lstrlen((char*)sSetup.szOriginalDSN) == 0)
  191. return FALSE;
  192. /* Remove the datasource */
  193. if (!SQLRemoveDSNFromIni((char*)sSetup.szOriginalDSN))
  194. return FALSE;
  195. break;
  196. case ODBC_CONFIG_DSN:
  197. /* Config. Get the database name from ODBC.INI file if not specified */
  198. if (lstrlen((char*)sSetup.szDatabase) == 0) {
  199. if (lstrlen((char*)sSetup.szOriginalDSN) > 0) {
  200. SQLGetPrivateProfileString((char*)sSetup.szOriginalDSN, KEY_DATABASE,
  201. ".", (char*)sSetup.szDatabase, MAX_DATABASE_NAME_LENGTH+1,
  202. ODBC_INI);
  203. }
  204. }
  205. /* Get the user name from ODBC.INI file if not specified */
  206. if (lstrlen((char*)sSetup.szUsername) == 0) {
  207. if (lstrlen((char*)sSetup.szOriginalDSN) > 0) {
  208. SQLGetPrivateProfileString((char*)sSetup.szOriginalDSN, KEY_USERNAME,
  209. "", (char*)sSetup.szUsername, MAX_USER_NAME_LENGTH+1,
  210. ODBC_INI);
  211. }
  212. }
  213. /* Get the password from ODBC.INI file if not specified */
  214. if (lstrlen((char*)sSetup.szPassword) == 0) {
  215. if (lstrlen((char*)sSetup.szOriginalDSN) > 0) {
  216. SQLGetPrivateProfileString((char*)sSetup.szOriginalDSN, KEY_PASSWORD,
  217. "", (char*)sSetup.szPassword, MAX_PASSWORD_LENGTH+1,
  218. ODBC_INI);
  219. }
  220. }
  221. /* Get the host from ODBC.INI file if not specified */
  222. if (lstrlen(sSetup.szHost) == 0) {
  223. if (lstrlen(sSetup.szOriginalDSN) > 0) {
  224. SQLGetPrivateProfileString(sSetup.szOriginalDSN, KEY_HOST,
  225. "", sSetup.szHost, MAX_HOST_NAME_LENGTH+1,
  226. ODBC_INI);
  227. }
  228. }
  229. /* Get the port number from ODBC.INI file if not specified */
  230. if (lstrlen(sSetup.szPort) == 0) {
  231. if (lstrlen(sSetup.szOriginalDSN) > 0) {
  232. SQLGetPrivateProfileString(sSetup.szOriginalDSN, KEY_PORT,
  233. "", sSetup.szPort, MAX_PORT_NUMBER_LENGTH+1,
  234. ODBC_INI);
  235. }
  236. }
  237. /* Get changes from the user */
  238. if (hwnd != NULL) {
  239. lpProc = MakeProcInstance( dlgSetup, s_hModule);
  240. if (!DialogBoxParam(s_hModule, "dlgSetup", hwnd, lpProc,
  241. (LPARAM) ((LPSETUP) &sSetup))) {
  242. FreeProcInstance(lpProc);
  243. return FALSE;
  244. }
  245. FreeProcInstance(lpProc);
  246. }
  247. /* Did datasource name change? */
  248. if (lstrcmpi((char*)sSetup.szOriginalDSN, (char*)sSetup.szDSN)) {
  249. /* Yes. Does the new datasource name already exist? */
  250. SQLGetPrivateProfileString((char*)sSetup.szDSN, KEY_DRIVER,
  251. "", (char*)szDriver, MAX_DRIVER_LENGTH+1, ODBC_INI);
  252. if (lstrlen((char*)szDriver) != 0) {
  253. /* Yes. Ask user if it is OK to overwrite. */
  254. LoadString(s_hModule, STR_SETUP, (char*)szTitle, sizeof(szTitle));
  255. LoadString(s_hModule, STR_OVERWRITE, (char*)szMessage, sizeof(szMessage));
  256. if (IDOK != MessageBox(hwnd, (char*)szMessage, (char*)szTitle,
  257. MB_OKCANCEL | MB_ICONQUESTION))
  258. return FALSE;
  259. }
  260. /* Remove the old datasource */
  261. if (!SQLRemoveDSNFromIni((char*)sSetup.szOriginalDSN))
  262. return FALSE;
  263. /* Create a new one */
  264. if (!SQLWriteDSNToIni((char*)sSetup.szDSN, lpszDriver))
  265. return FALSE;
  266. }
  267. /* Write the rest of the INI file entries */
  268. SQLWritePrivateProfileString((char*)sSetup.szDSN, KEY_DATABASE,
  269. (char*)sSetup.szDatabase, ODBC_INI);
  270. SQLWritePrivateProfileString((char*)sSetup.szDSN, KEY_USERNAME,
  271. (char*)sSetup.szUsername, ODBC_INI);
  272. SQLWritePrivateProfileString((char*)sSetup.szDSN, KEY_PASSWORD,
  273. (char*)sSetup.szPassword, ODBC_INI);
  274. SQLWritePrivateProfileString(sSetup.szDSN, KEY_HOST,
  275. sSetup.szHost, ODBC_INI);
  276. SQLWritePrivateProfileString(sSetup.szDSN, KEY_PORT,
  277. sSetup.szPort, ODBC_INI);
  278. break;
  279. case ODBC_ADD_DSN:
  280. /* Add. Get changes from the user */
  281. if (hwnd != NULL) {
  282. lpProc = MakeProcInstance( dlgSetup, s_hModule);
  283. if (!DialogBoxParam(s_hModule, "dlgSetup", hwnd, lpProc,
  284. (LPARAM) ((LPSETUP) &sSetup))) {
  285. FreeProcInstance(lpProc);
  286. return FALSE;
  287. }
  288. FreeProcInstance(lpProc);
  289. }
  290. /* Does the new datasource name already exist? */
  291. SQLGetPrivateProfileString((char*)sSetup.szDSN, KEY_DRIVER,
  292. "", (char*)szDriver, MAX_DRIVER_LENGTH+1, ODBC_INI);
  293. if (lstrlen((char*)szDriver) != 0) {
  294. /* Yes. Ask user if it is OK to overwrite. */
  295. if (hwnd != NULL) {
  296. LoadString(s_hModule, STR_SETUP, (char*)szTitle, sizeof(szTitle));
  297. LoadString(s_hModule, STR_OVERWRITE, (char*)szMessage, sizeof(szMessage));
  298. if (IDOK != MessageBox(hwnd, (char*)szMessage, (char*)szTitle,
  299. MB_OKCANCEL | MB_ICONQUESTION))
  300. return FALSE;
  301. }
  302. /* Remove the old datasource */
  303. if (!SQLRemoveDSNFromIni((char*)sSetup.szDSN))
  304. return FALSE;
  305. }
  306. /* Create a new one */
  307. if (!SQLWriteDSNToIni((char*)sSetup.szDSN, lpszDriver))
  308. return FALSE;
  309. SQLWritePrivateProfileString((char*)sSetup.szDSN, KEY_DATABASE,
  310. (char*)sSetup.szDatabase, ODBC_INI);
  311. SQLWritePrivateProfileString((char*)sSetup.szDSN, KEY_USERNAME,
  312. (char*)sSetup.szUsername, ODBC_INI);
  313. SQLWritePrivateProfileString((char*)sSetup.szDSN, KEY_PASSWORD,
  314. (char*)sSetup.szPassword, ODBC_INI);
  315. SQLWritePrivateProfileString(sSetup.szDSN, KEY_HOST,
  316. sSetup.szHost, ODBC_INI);
  317. SQLWritePrivateProfileString(sSetup.szDSN, KEY_PORT,
  318. sSetup.szPort, ODBC_INI);
  319. break;
  320. }
  321. return TRUE;
  322. }
  323. /***************************************************************************/