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.

264 lines
9.0 KiB

  1. //************************************************************************
  2. // Generic Win 3.1 fax printer driver support. User Interface helper
  3. // functions which are called in WOWFAXUI.C, helpers used during upgrade.
  4. //
  5. // History:
  6. // 14-mar-95 reedb Created. These were moved from WOWFAXUI.C.
  7. //
  8. //************************************************************************
  9. #include "windows.h"
  10. #include "wowfaxui.h"
  11. #include "winspool.h"
  12. extern LPCRITICAL_SECTION lpCriticalSection;
  13. extern HINSTANCE ghInst;
  14. //************************************************************************
  15. // AllocPathW - Helper for DrvUpgradePrinter and friends.
  16. //************************************************************************
  17. PWSTR AllocPathW(VOID)
  18. {
  19. PWSTR szTmp;
  20. szTmp = WFLOCALALLOC((MAX_PATH+1) * sizeof(TCHAR), L"AllocPathW");
  21. return(szTmp);
  22. }
  23. //************************************************************************
  24. // BuildPathW - Helper for DrvUpgradePrinter and friends.
  25. //************************************************************************
  26. PWSTR BuildPathW(PWSTR szPath, PWSTR szFileName)
  27. {
  28. PWSTR szTmp;
  29. if ((szTmp = WFLOCALALLOC((MAX_PATH+1) * sizeof(TCHAR), L"BuildPathW")) != NULL) {
  30. wcscpy(szTmp, szPath);
  31. wcscat(szTmp, L"\\");
  32. wcscat(szTmp, szFileName);
  33. return(szTmp);
  34. }
  35. else {
  36. return(NULL);
  37. }
  38. }
  39. //************************************************************************
  40. // MyGetFileTime - Helper for DrvUpgradePrinter and friends.
  41. //************************************************************************
  42. BOOL MyGetFileTime(PWSTR szDir, PWSTR szName, LPFILETIME lpFileTime)
  43. {
  44. LPWIN32_FIND_DATA lpfd;
  45. HANDLE hfd;
  46. PWSTR szTmp;
  47. BOOL bRet = FALSE;
  48. szTmp = BuildPathW(szDir, szName);
  49. lpfd = WFLOCALALLOC(sizeof(WIN32_FIND_DATA), L"MyGetFileTime");
  50. if ((szTmp) && (lpfd)) {
  51. LOGDEBUG(1, (L"WOWFAXUI!GetFileTime, szTmp: %s\n", szTmp));
  52. if ((hfd = FindFirstFile(szTmp, lpfd)) != INVALID_HANDLE_VALUE) {
  53. memcpy(lpFileTime, &(lpfd->ftLastWriteTime), sizeof(FILETIME));
  54. FindClose(hfd);
  55. bRet = TRUE;
  56. LOGDEBUG(1, (L"WOWFAXUI!GetFileTime, FileTimeHi: %X FileTimeLo: %X\n", lpFileTime->dwHighDateTime, lpFileTime->dwLowDateTime));
  57. }
  58. else {
  59. LOGDEBUG(0, (L"WOWFAXUI!GetFileTime, file not found: %s\n", szTmp));
  60. }
  61. }
  62. if (szTmp) {
  63. LocalFree(szTmp);
  64. }
  65. if (lpfd) {
  66. LocalFree(lpfd);
  67. }
  68. return(bRet);
  69. }
  70. //************************************************************************
  71. // CheckForNewerFiles - Helper for DrvUpgradePrinter. Compares the date/time
  72. // of wowfaxui.dll and wowfax.dll in the two passed directories. Returns
  73. // FALSE if files in szOldDriverDir are the same or newer than those
  74. // in szSysDir. Otherwise returns non-zero.
  75. //************************************************************************
  76. BOOL CheckForNewerFiles(PWSTR szOldDriverDir, PWSTR szSysDir)
  77. {
  78. FILETIME ftSourceDriver, ftCurrentDriver;
  79. BOOL bRet = FALSE;
  80. if ((szOldDriverDir) && (szSysDir)) {
  81. if (MyGetFileTime(szOldDriverDir, L"wowfax.dll", &ftCurrentDriver)) {
  82. if (MyGetFileTime(szSysDir, L"wowfax.dll", &ftSourceDriver)) {
  83. // Check time/date to see if we need to update the drivers.
  84. if (CompareFileTime(&ftSourceDriver, &ftCurrentDriver) > 0) {
  85. bRet = TRUE;
  86. }
  87. }
  88. }
  89. if (MyGetFileTime(szOldDriverDir, L"wowfaxui.dll", &ftCurrentDriver)) {
  90. if (MyGetFileTime(szSysDir, L"wowfaxui.dll", &ftSourceDriver)) {
  91. if (CompareFileTime(&ftSourceDriver, &ftCurrentDriver) > 0) {
  92. bRet = TRUE;
  93. }
  94. }
  95. }
  96. }
  97. else {
  98. LOGDEBUG(0, (L"WOWFAXUI!CheckForNewerFiles: NULL directory parameters\n"));
  99. }
  100. return(bRet);
  101. }
  102. //************************************************************************
  103. // DoUpgradePrinter - Called by DrvUpgradePrinter which is called in the
  104. // system context by the spooler.
  105. //************************************************************************
  106. BOOL DoUpgradePrinter(DWORD dwLevel, LPDRIVER_UPGRADE_INFO_1W lpDrvUpgradeInfo)
  107. {
  108. static BOOL bDrvUpgradePrinterLock = FALSE;
  109. HANDLE hPrinter = NULL;
  110. DRIVER_INFO_2 DriverInfo, *pDriverInfo = NULL;
  111. DWORD dwNeeded = 0;
  112. PWSTR szSysDir = NULL;
  113. PWSTR szDstDir = NULL;
  114. PWSTR szSrcPath = NULL;
  115. PWCHAR pwc;
  116. BOOL bRet = FALSE;
  117. TCHAR szName[WOWFAX_MAX_USER_MSG_LEN] = L"";
  118. // Check for correct level for upgrade.
  119. if (dwLevel != 1) {
  120. LOGDEBUG(0, (L"WOWFAXUI!DrvUpgradePrinter, Bad input Level\n"));
  121. SetLastError(ERROR_INVALID_LEVEL);
  122. goto DoUpgradePrinterExit;
  123. }
  124. szDstDir = AllocPathW();
  125. szSysDir = AllocPathW();
  126. if (!szDstDir || !szSysDir) {
  127. LOGDEBUG(0, (L"WOWFAXUI!DoUpgradePrinter, work space allocation failed\n"));
  128. goto DoUpgradePrinterExit;
  129. }
  130. if (!GetSystemDirectory(szSysDir, MAX_PATH+1)) {
  131. LOGDEBUG(0, (L"WOWFAXUI!DoUpgradePrinter, GetSystemDirectory failed\n"));
  132. goto DoUpgradePrinterExit;
  133. }
  134. if (!lpDrvUpgradeInfo->pPrinterName) {
  135. LOGDEBUG(0, (L"WOWFAXUI!DoUpgradePrinter, pPrinterName is NULL\n"));
  136. goto DoUpgradePrinterExit;
  137. }
  138. // Get the paths to the old printer drivers.
  139. if (!OpenPrinter(lpDrvUpgradeInfo->pPrinterName, &hPrinter, NULL)) {
  140. LOGDEBUG(0, (L"WOWFAXUI!DoUpgradePrinter, Unable to open: %s\n", lpDrvUpgradeInfo->pPrinterName));
  141. goto DoUpgradePrinterExit;
  142. }
  143. GetPrinterDriver(hPrinter, NULL, 2, (LPBYTE) pDriverInfo, 0, &dwNeeded);
  144. if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
  145. LOGDEBUG(0, (L"WOWFAXUI!DoUpgradePrinter, GetPrinterDriver failed\n"));
  146. goto DoUpgradePrinterExit;
  147. }
  148. if ((pDriverInfo = WFLOCALALLOC(dwNeeded, L"DoUpgradePrinter")) == NULL) {
  149. LOGDEBUG(0, (L"WOWFAXUI!DoUpgradePrinter, work space allocation failed\n"));
  150. goto DoUpgradePrinterExit;
  151. }
  152. if (!GetPrinterDriver(hPrinter, NULL, 2, (LPBYTE) pDriverInfo, dwNeeded, &dwNeeded)) {
  153. LOGDEBUG(0, (L"WOWFAXUI!DoUpgradePrinter, GetPrinterDriver failed, GetLastError: %d\n", GetLastError()));
  154. goto DoUpgradePrinterExit;
  155. }
  156. ClosePrinter(hPrinter);
  157. // Strip off the file name.
  158. if ((pwc = wcsrchr(pDriverInfo->pDriverPath, L'\\')) == NULL) {
  159. LOGDEBUG(0, (L"WOWFAXUI!DoUpgradePrinter, unable to strip file name\n"));
  160. goto DoUpgradePrinterExit;
  161. }
  162. *pwc = UNICODE_NULL;
  163. // Install new printer driver if it's more recent than the old one.
  164. if (CheckForNewerFiles(pDriverInfo->pDriverPath, szSysDir)) {
  165. LOGDEBUG(1, (L"WOWFAXUI!DoUpgradePrinter, Doing driver update\n"));
  166. memset(&DriverInfo, 0, sizeof(DRIVER_INFO_2));
  167. if (!GetPrinterDriverDirectory(NULL, NULL, 1, (LPBYTE) szDstDir, MAX_PATH, &dwNeeded)) {
  168. LOGDEBUG(0, (L"WOWFAXUI!DoUpgradePrinter, GetPrinterDriverDirectory failed\n"));
  169. goto DoUpgradePrinterExit;
  170. }
  171. // This is a dummy. We've no data file, but spooler won't take NULL.
  172. DriverInfo.pDataFile = BuildPathW(szDstDir, WOWFAX_DLL_NAME);
  173. DriverInfo.pDriverPath = BuildPathW(szDstDir, WOWFAX_DLL_NAME);
  174. LOGDEBUG(1, (L"WOWFAXUI!DoUpgradePrinter, pDriverPath = %s\n", DriverInfo.pDataFile));
  175. if (DriverInfo.pDriverPath) {
  176. szSrcPath = BuildPathW(szSysDir, WOWFAX_DLL_NAME);
  177. if (szSrcPath) {
  178. CopyFile(szSrcPath, DriverInfo.pDriverPath, FALSE);
  179. LocalFree(szSrcPath);
  180. }
  181. }
  182. DriverInfo.pConfigFile = BuildPathW(szDstDir, WOWFAXUI_DLL_NAME);
  183. szSrcPath = BuildPathW(szSysDir, WOWFAXUI_DLL_NAME);
  184. if (DriverInfo.pConfigFile) {
  185. if (szSrcPath) {
  186. CopyFile(szSrcPath, DriverInfo.pConfigFile, FALSE);
  187. LocalFree(szSrcPath);
  188. }
  189. }
  190. // Install the printer driver.
  191. DriverInfo.cVersion = 1;
  192. if (LoadString(ghInst, WOWFAX_NAME_STR, szName, WOWFAX_MAX_USER_MSG_LEN)) {
  193. DriverInfo.pName = szName;
  194. if (AddPrinterDriver(NULL, 2, (LPBYTE) &DriverInfo) == FALSE) {
  195. bRet = (GetLastError() == ERROR_PRINTER_DRIVER_ALREADY_INSTALLED);
  196. }
  197. else {
  198. bRet = TRUE;
  199. }
  200. }
  201. if (DriverInfo.pDataFile) {
  202. LocalFree(DriverInfo.pDataFile);
  203. }
  204. if (DriverInfo.pDriverPath) {
  205. LocalFree(DriverInfo.pDriverPath);
  206. }
  207. if (DriverInfo.pConfigFile) {
  208. LocalFree(DriverInfo.pConfigFile);
  209. }
  210. }
  211. else {
  212. LOGDEBUG(1, (L"WOWFAXUI!DoUpgradePrinter, No driver update\n"));
  213. bRet = TRUE;
  214. }
  215. DoUpgradePrinterExit:
  216. if (szDstDir) {
  217. LocalFree(szDstDir);
  218. }
  219. if (szSysDir) {
  220. LocalFree(szSysDir);
  221. }
  222. if (pDriverInfo) {
  223. LocalFree(pDriverInfo);
  224. }
  225. return(bRet);
  226. }