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.

477 lines
13 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996-1998
  5. //
  6. // File:
  7. //
  8. // Contents:
  9. //
  10. // History:
  11. //---------------------------------------------------------------------------
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <tchar.h>
  17. #include <odbcinst.h>
  18. #define szSQLSetConfigMode "SQLSetConfigMode"
  19. //---------------------------------------------------------------------------
  20. // various installation routine we use, can't link with library as it will
  21. // cause
  22. #ifdef UNICODE
  23. #define szSQLInstallDriver "SQLInstallDriverW"
  24. #define szSQLGetInstalledDrivers "SQLGetInstalledDriversW"
  25. #define szSQLConfigDataSource "SQLConfigDataSourceW"
  26. #define szSQLGetPrivateProfileString "SQLGetPrivateProfileStringW"
  27. #define szSQLConfigDriver "SQLConfigDriverW"
  28. #define szSQLInstallerError "SQLInstallerErrorW"
  29. #else
  30. #define szSQLInstallDriver "SQLInstallDriver"
  31. #define szSQLGetInstalledDrivers "SQLGetInstalledDrivers"
  32. #define szSQLConfigDataSource "SQLConfigDataSource"
  33. #define szSQLGetPrivateProfileString "SQLGetPrivateProfileString"
  34. #define szSQLConfigDriver "SQLConfigDriver"
  35. #define szSQLInstallerError "SQLInstallerError"
  36. #endif
  37. //
  38. // ODBC install functions - To prevent system to 'force' odbccp32.dll
  39. // odbccp32 may not present on the system nor it is available at the
  40. // time setup got kick off.
  41. //
  42. typedef BOOL (* SQLCONFIGDATASOURCE)(HWND, WORD, LPCTSTR, LPCTSTR);
  43. typedef SQLRETURN (* SQLINSTALLERERROR)(WORD , DWORD*, LPTSTR, WORD, WORD*);
  44. typedef int (* SQLGETPRIVATEPROFILESTRING)( LPCTSTR lpszSection,
  45. LPCTSTR lpszEntry,
  46. LPCTSTR lpszDefault,
  47. LPTSTR lpszRetBuffer,
  48. int cbRetBuffer,
  49. LPCTSTR lpszFilename);
  50. typedef BOOL (* SQLINSTALLDRIVER)(LPCTSTR lpszInfFile,
  51. LPCTSTR lpszDriver,
  52. LPTSTR lpszPath,
  53. WORD cbPathMax,
  54. WORD* pcbPathOut);
  55. typedef BOOL (* SQLGETINSTALLEDDRIVER)(LPTSTR lpszBuf,
  56. WORD cbBufMax,
  57. WORD* pcbBufOut);
  58. typedef BOOL (* SQLSETCONFIGMODE)(UWORD wConfigMode);
  59. static SQLCONFIGDATASOURCE fpSQLConfigDataSource=NULL;
  60. static SQLINSTALLDRIVER fpSQLInstallDriver=NULL;
  61. static SQLINSTALLERERROR fpSQLInstallerError=NULL;
  62. static SQLGETPRIVATEPROFILESTRING fpSQLGetPrivateProfileString=NULL;
  63. static SQLSETCONFIGMODE fpSQLSetConfigMode=NULL;
  64. static SQLGETINSTALLEDDRIVER fpSQLGetInstalledDriver=NULL;
  65. static HINSTANCE hODBCCP32=NULL;
  66. //------------------------------------------------------------
  67. void
  68. ReportError(
  69. IN HWND hWnd,
  70. IN LPTSTR pszDefaultMsg
  71. )
  72. /*++
  73. Abstract:
  74. Popup a error message.
  75. Parameters:
  76. hWnd - Parent window handle.
  77. pszDefaultMsg - ignore.
  78. Returns:
  79. None.
  80. ++*/
  81. {
  82. #if DBG
  83. DWORD dwErrCode;
  84. TCHAR szErrMsg[2048];
  85. WORD szErrMsgSize=sizeof(szErrMsg)/sizeof(szErrMsg[0]);
  86. WORD cbErrMsg;
  87. fpSQLInstallerError(
  88. 1,
  89. &dwErrCode,
  90. szErrMsg,
  91. szErrMsgSize,
  92. &cbErrMsg
  93. );
  94. MessageBox(hWnd, szErrMsg, _TEXT("Setup Error"), MB_OK);
  95. #else
  96. return;
  97. #endif
  98. }
  99. //------------------------------------------------------------
  100. BOOL
  101. InitODBCSetup()
  102. /*++
  103. ++*/
  104. {
  105. hODBCCP32 = LoadLibrary(_TEXT("odbccp32"));
  106. if(hODBCCP32)
  107. {
  108. fpSQLConfigDataSource=(SQLCONFIGDATASOURCE)GetProcAddress(
  109. hODBCCP32,
  110. szSQLConfigDataSource
  111. );
  112. fpSQLInstallDriver=(SQLINSTALLDRIVER)GetProcAddress(
  113. hODBCCP32,
  114. szSQLInstallDriver
  115. );
  116. fpSQLInstallerError=(SQLINSTALLERERROR)GetProcAddress(
  117. hODBCCP32,
  118. szSQLInstallerError
  119. );
  120. fpSQLGetPrivateProfileString=(SQLGETPRIVATEPROFILESTRING)GetProcAddress(
  121. hODBCCP32,
  122. szSQLGetPrivateProfileString
  123. );
  124. fpSQLSetConfigMode=(SQLSETCONFIGMODE)GetProcAddress(
  125. hODBCCP32,
  126. szSQLSetConfigMode
  127. );
  128. fpSQLGetInstalledDriver=(SQLGETINSTALLEDDRIVER)GetProcAddress(
  129. hODBCCP32,
  130. szSQLGetInstalledDrivers
  131. );
  132. }
  133. if( hODBCCP32 == NULL || fpSQLConfigDataSource == NULL ||
  134. fpSQLInstallDriver == NULL || fpSQLInstallerError == NULL ||
  135. fpSQLGetPrivateProfileString == NULL || fpSQLSetConfigMode == NULL ||
  136. fpSQLGetInstalledDriver == NULL)
  137. {
  138. ReportError(NULL, _TEXT("Can't load odbccp32.dll "));
  139. return FALSE;
  140. }
  141. return TRUE;
  142. }
  143. //------------------------------------------------------------
  144. void
  145. CleanupODBCSetup()
  146. /*++
  147. ++*/
  148. {
  149. if(hODBCCP32)
  150. FreeLibrary(hODBCCP32);
  151. fpSQLConfigDataSource=NULL;
  152. fpSQLInstallDriver=NULL;
  153. fpSQLInstallerError=NULL;
  154. fpSQLGetPrivateProfileString=NULL;
  155. fpSQLSetConfigMode=NULL;
  156. fpSQLGetInstalledDriver=NULL;
  157. hODBCCP32=NULL;
  158. return;
  159. }
  160. //---------------------------------------------------------------------------
  161. //
  162. // Access Driver installation
  163. //
  164. LPTSTR szAccessDriver=_TEXT("Microsoft Access Driver (*.mdb)\0")
  165. _TEXT("Driver=odbcjt32.dll\0")
  166. _TEXT("Setup=odbcjt32.dll\0")
  167. _TEXT("Name=Microsoft Access Driver (*.mdb)\0")
  168. _TEXT("APILevel=1\0")
  169. _TEXT("ConnectFunctions=YYN\0")
  170. _TEXT("DriverODBCVer=02.50\0")
  171. //_TEXT("FileUsage=2\0")
  172. _TEXT("FileExtns=*.mdb\0")
  173. _TEXT("SQLLevel=0\0");
  174. //---------------------------------------------------------------------------
  175. BOOL
  176. IsDriverInstalled(
  177. IN LPTSTR szDriverName
  178. )
  179. /*++
  180. Abstract:
  181. Check if a ODBC driver installed on system.
  182. Parameters:
  183. szDriveName - Name of the drive.
  184. Returns:
  185. TRUE if driver installed, FALSE otherwise.
  186. ++*/
  187. {
  188. TCHAR szBuf[8096]; // this got to be enough
  189. WORD cbBufMax=sizeof(szBuf)/sizeof(szBuf[0]);
  190. WORD cbBufOut;
  191. LPTSTR pszBuf=szBuf;
  192. if(hODBCCP32 == NULL && !InitODBCSetup())
  193. return FALSE;
  194. if(fpSQLGetInstalledDriver(szBuf, cbBufMax, &cbBufOut))
  195. {
  196. ReportError(NULL, _TEXT("SQLGetInstalledDrivers"));
  197. }
  198. else
  199. {
  200. do {
  201. if(_tcsnicmp(szDriverName, pszBuf, min(lstrlen(szDriverName), lstrlen(pszBuf))) == 0)
  202. break;
  203. pszBuf += lstrlen(pszBuf) + 1;
  204. } while(pszBuf[1] != _TEXT('\0'));
  205. }
  206. return (pszBuf[1] != _TEXT('\0'));
  207. }
  208. //---------------------------------------------------------------------------
  209. BOOL
  210. IsDataSourceInstalled(
  211. IN LPTSTR pszDataSource,
  212. IN UWORD wConfigMode,
  213. IN OUT LPTSTR pszDbFile,
  214. IN DWORD cbBufSize
  215. )
  216. /*++
  217. Abstract:
  218. Check if a ODBC datasource are installed.
  219. Parameters:
  220. pszDataSource - name of data source.
  221. wConfigMode - configuration mode, refer to ODBC for detail,
  222. license server uses ODBC_SYSTEM_DSN.
  223. pszDbFile - Pointer to buffer for receving full path to database file
  224. if data source is installed.
  225. cbBufSize - size of buffer in characters.
  226. Returns:
  227. TRUE if datasource is installed, FALSE otherwise.
  228. ++*/
  229. {
  230. BOOL bSuccess = TRUE;
  231. if(hODBCCP32 == NULL && !InitODBCSetup())
  232. {
  233. bSuccess = FALSE;
  234. goto cleanup;
  235. }
  236. if(fpSQLSetConfigMode(wConfigMode) == FALSE)
  237. {
  238. ReportError(NULL, _TEXT("SQLSetConfigMode failed"));
  239. bSuccess = FALSE;
  240. goto cleanup;
  241. }
  242. if(fpSQLGetPrivateProfileString(
  243. pszDataSource,
  244. _TEXT("DBQ"),
  245. _TEXT(""),
  246. pszDbFile,
  247. cbBufSize,
  248. _TEXT("ODBC.INI")
  249. ) == 0)
  250. {
  251. bSuccess = FALSE;
  252. }
  253. cleanup:
  254. return bSuccess;
  255. }
  256. //---------------------------------------------------------------------------
  257. BOOL
  258. ConfigDataSource(
  259. HWND hWnd,
  260. BOOL bInstall, // TRUE to install FALSE to remove
  261. LPTSTR pszDriver, // driver
  262. LPTSTR pszDsn, // DSN
  263. LPTSTR pszUser, // User
  264. LPTSTR pszPwd, // Password
  265. LPTSTR pszMdbFile // MDB file
  266. )
  267. /*++
  268. Abstract:
  269. Routine to add/remove ODBC data source.
  270. Parameters:
  271. hWnd - Parent window handle.
  272. bInstall - TRUE if installing ODBC data source, FALSE otherwise.
  273. pszDrive - Name of the ODBC drive to be used on data source.
  274. pszDsn - ODBC Data Source Name.
  275. pszUser - Login use name.
  276. pszPwd - Login password.
  277. pszMdbFile - Name of the Database file.
  278. Returns:
  279. TRUE if successfule, FALSE otherwise.
  280. ++*/
  281. {
  282. TCHAR szAttributes[MAX_PATH*6+1];
  283. BOOL bConfig=TRUE;
  284. TCHAR* pAttribute;
  285. if(hODBCCP32 == NULL && !InitODBCSetup())
  286. return FALSE;
  287. //
  288. // for attribute string
  289. //
  290. pAttribute=szAttributes;
  291. memset(szAttributes, 0, sizeof(szAttributes));
  292. wsprintf(pAttribute, _TEXT("DSN=%s"), pszDsn);
  293. pAttribute += lstrlen(pAttribute) + 1;
  294. wsprintf(pAttribute, _TEXT("UID=%s"), pszUser);
  295. pAttribute += lstrlen(pAttribute) + 1;
  296. if(pszPwd)
  297. {
  298. wsprintf(pAttribute, _TEXT("PASSWORD=%s"), pszPwd);
  299. pAttribute += lstrlen(pAttribute) + 1;
  300. }
  301. _stprintf(pAttribute, _TEXT("DBQ=%s"), pszMdbFile);
  302. bConfig=fpSQLConfigDataSource(NULL,
  303. (WORD)((bInstall) ? ODBC_ADD_SYS_DSN : ODBC_REMOVE_SYS_DSN),
  304. pszDriver,
  305. szAttributes);
  306. // ignore error on uninstall
  307. if(!bConfig && bInstall)
  308. {
  309. ReportError(hWnd, _TEXT("Can't config data source"));
  310. }
  311. return bConfig;
  312. }
  313. BOOL
  314. RepairDataSource(
  315. HWND hWnd,
  316. LPTSTR pszDriver,
  317. LPTSTR pszDsn, // DSN
  318. LPTSTR pszUser, // User
  319. LPTSTR pszPwd, // Password
  320. LPTSTR pszMdbFile // MDB file
  321. )
  322. /*++
  323. Abstract:
  324. Routine to Compact/Repair a database file
  325. Parameters:
  326. hWnd - Parent window handle.
  327. pszDsn - ODBC Data Source Name.
  328. pszUser - Login use name.
  329. pszPwd - Login password.
  330. pszMdbFile - Name of the Database file.
  331. Returns:
  332. TRUE if successfule, FALSE otherwise.
  333. ++*/
  334. {
  335. TCHAR szAttributes[MAX_PATH*6+1];
  336. BOOL bConfig=TRUE;
  337. TCHAR* pAttribute;
  338. if(hODBCCP32 == NULL && !InitODBCSetup())
  339. return FALSE;
  340. //
  341. // for attribute string
  342. //
  343. pAttribute=szAttributes;
  344. memset(szAttributes, 0, sizeof(szAttributes));
  345. wsprintf(pAttribute, _TEXT("DSN=%s"), pszDsn);
  346. pAttribute += lstrlen(pAttribute) + 1;
  347. wsprintf(pAttribute, _TEXT("UID=%s"), pszUser);
  348. pAttribute += lstrlen(pAttribute) + 1;
  349. if(pszPwd)
  350. {
  351. wsprintf(pAttribute, _TEXT("PASSWORD=%s"), pszPwd);
  352. pAttribute += lstrlen(pAttribute) + 1;
  353. }
  354. _stprintf(pAttribute, _TEXT("DBQ=%s"), pszMdbFile);
  355. pAttribute += lstrlen(pAttribute) + 1;
  356. _stprintf(pAttribute, _TEXT("REPAIR_DB=%s"), pszMdbFile);
  357. bConfig=fpSQLConfigDataSource(
  358. NULL,
  359. (WORD)ODBC_CONFIG_SYS_DSN,
  360. pszDriver,
  361. szAttributes
  362. );
  363. // ignore error on uninstall
  364. if(bConfig == FALSE)
  365. {
  366. ReportError(hWnd, _TEXT("Can't repair data source"));
  367. }
  368. return bConfig;
  369. }