Leaked source code of windows server 2003
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.

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