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.

242 lines
7.2 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. printupg.c
  5. Abstract:
  6. Module to upgrade printer drivers and related stuff.
  7. Top-level routines: UpgradePrinters
  8. Author:
  9. Ted Miller (tedm) 4-Aug-1995
  10. Revision History:
  11. --*/
  12. #include "setupp.h"
  13. #pragma hdrstop
  14. //
  15. // Maximum time to wait for the spooler service to start
  16. //
  17. #define MAXIMUM_WAIT_TIME 30000
  18. DWORD
  19. UpgradePrinters(
  20. VOID
  21. )
  22. /*++
  23. Routine Description:
  24. Top level routine to upgrade printer drivers.
  25. Call out to ntprint.dll to to the upgrade.
  26. Arguments:
  27. None.
  28. Return Value:
  29. Win32 error code indicating outcome of operation.
  30. NO_ERROR if successful.
  31. --*/
  32. {
  33. DWORD ReturnCode;
  34. BOOL b;
  35. SERVICE_STATUS ServiceStatus;
  36. DWORD InitialTickCount;
  37. SC_HANDLE hSC,hService;
  38. HINSTANCE NtPrintLibrary;
  39. UPGRADEPRINTERSPROC UpgradeRoutine;
  40. //
  41. // Make sure the spooler is running.
  42. //
  43. hSC = OpenSCManager(NULL,NULL,SC_MANAGER_CONNECT);
  44. SetupDebugPrint( L"UpgradePrinters: Just opened SCManager");
  45. if(hSC == NULL) {
  46. ReturnCode = GetLastError();
  47. SetuplogError(
  48. LogSevWarning,
  49. SETUPLOG_USE_MESSAGEID,
  50. MSG_LOG_PRINTUPG_FAILED, NULL,
  51. SETUPLOG_USE_MESSAGEID,
  52. MSG_LOG_X_PARAM_RETURNED_WINERR,
  53. szOpenSCManager,
  54. ReturnCode,
  55. L"SC_MANAGER_CONNECT",
  56. NULL,NULL);
  57. return(ReturnCode);
  58. }
  59. hService = OpenService(hSC,L"Spooler",SERVICE_START | SERVICE_QUERY_STATUS);
  60. SetupDebugPrint1( L"UpgradePrinters: Just opened service spooler, ret = %d", hService);
  61. CloseServiceHandle(hSC);
  62. if(hService == NULL) {
  63. ReturnCode = GetLastError();
  64. SetuplogError(
  65. LogSevWarning,
  66. SETUPLOG_USE_MESSAGEID,
  67. MSG_LOG_PRINTUPG_FAILED, NULL,
  68. SETUPLOG_USE_MESSAGEID,
  69. MSG_LOG_X_PARAM_RETURNED_WINERR,
  70. szOpenService,
  71. ReturnCode,
  72. L"Spooler",
  73. NULL,NULL);
  74. return(ReturnCode);
  75. }
  76. if( !StartSpooler()) {
  77. ReturnCode = GetLastError();
  78. SetuplogError(
  79. LogSevWarning,
  80. SETUPLOG_USE_MESSAGEID,
  81. MSG_LOG_PRINTUPG_FAILED, NULL,
  82. SETUPLOG_USE_MESSAGEID,
  83. MSG_LOG_X_PARAM_RETURNED_WINERR,
  84. szStartService,
  85. ReturnCode,
  86. L"Spooler",
  87. NULL,NULL);
  88. KdPrint(("SETUP: Unable to start spooler for printer upgrade (%u)\n",ReturnCode));
  89. CloseServiceHandle(hService);
  90. return(ReturnCode);
  91. }
  92. //
  93. // Wait for the service to start.
  94. //
  95. InitialTickCount = GetTickCount();
  96. while(TRUE) {
  97. if(QueryServiceStatus(hService,&ServiceStatus)) {
  98. if( ServiceStatus.dwCurrentState == SERVICE_RUNNING ) {
  99. KdPrint(("SETUP: spooler started after %u seconds. \n",(GetTickCount() - InitialTickCount) /1000));
  100. break;
  101. } else if( ServiceStatus.dwCurrentState == SERVICE_START_PENDING ) {
  102. if( ( GetTickCount() - InitialTickCount ) < MAXIMUM_WAIT_TIME ) {
  103. // KdPrint(("SETUP: spooler has been starting for the past %u seconds. \n",(GetTickCount() - InitialTickCount) /1000));
  104. // Sleep( ServiceStatus.dwWaitHint );
  105. Sleep( 1000 );
  106. } else {
  107. //
  108. // Assume that the service is hung
  109. //
  110. KdPrint(("SETUP: the spooler appears to be hung. It has been starting for more than %u seconds. \n", MAXIMUM_WAIT_TIME/1000));
  111. SetuplogError(
  112. LogSevWarning,
  113. SETUPLOG_USE_MESSAGEID,
  114. MSG_LOG_PRINTUPG_FAILED, NULL,
  115. SETUPLOG_USE_MESSAGEID,
  116. MSG_LOG_SPOOLER_TIMEOUT, NULL,
  117. NULL);
  118. //
  119. // Return the same error code that EnumPrinterDrivers()
  120. // would return if called, but the spooler wasn't started
  121. //
  122. CloseServiceHandle(hService);
  123. return(RPC_S_SERVER_UNAVAILABLE);
  124. }
  125. } else {
  126. //
  127. // The service is not running and is not starting
  128. //
  129. KdPrint(("SETUP: Spooler is not running and is is not starting. ServiecState = (%u)\n", ServiceStatus.dwCurrentState));
  130. SetuplogError(
  131. LogSevWarning,
  132. SETUPLOG_USE_MESSAGEID,
  133. MSG_LOG_PRINTUPG_FAILED, NULL,
  134. SETUPLOG_USE_MESSAGEID,
  135. MSG_LOG_SPOOLER_NOT_RUNNING, NULL,
  136. NULL);
  137. //
  138. // Return the same error code that EnumPrinterDrivers()
  139. // would return if called, but the spooler wasn't started
  140. //
  141. CloseServiceHandle(hService);
  142. return(RPC_S_SERVER_UNAVAILABLE);
  143. }
  144. } else {
  145. //
  146. // If unable to query the spooler status, then ignore the
  147. // error, wait for some time, and assume that the service is up
  148. // and running. If it is not started, then the EnumeratePrinterDrivers
  149. // will fail, an we will catch the error there.
  150. //
  151. ReturnCode = GetLastError();
  152. KdPrint(("SETUP: Unable to query spooler status. Error = (%u)\n",ReturnCode));
  153. Sleep( 10000 );
  154. break;
  155. }
  156. }
  157. CloseServiceHandle(hService);
  158. NtPrintLibrary = LoadLibrary(L"NTPRINT");
  159. if(!NtPrintLibrary) {
  160. ReturnCode = GetLastError();
  161. SetuplogError(
  162. LogSevWarning,
  163. SETUPLOG_USE_MESSAGEID,
  164. MSG_LOG_PRINTUPG_FAILED, NULL,
  165. SETUPLOG_USE_MESSAGEID,
  166. MSG_LOG_X_PARAM_RETURNED_WINERR,
  167. L"LoadLibrary",
  168. ReturnCode,
  169. L"NTPRINT.DLL",
  170. NULL,NULL);
  171. return(ReturnCode);
  172. }
  173. UpgradeRoutine = (UPGRADEPRINTERSPROC)GetProcAddress(
  174. NtPrintLibrary,
  175. UPGRADEPRINTERSPROCNAME
  176. );
  177. if(!UpgradeRoutine) {
  178. ReturnCode = GetLastError();
  179. SetuplogError(
  180. LogSevWarning,
  181. SETUPLOG_USE_MESSAGEID,
  182. MSG_LOG_PRINTUPG_FAILED, NULL,
  183. SETUPLOG_USE_MESSAGEID,
  184. MSG_LOG_X_PARAM_RETURNED_WINERR,
  185. L"GetProcAddress",
  186. ReturnCode,
  187. L"NTPRINT.DLL",
  188. NULL,NULL);
  189. FreeLibrary(NtPrintLibrary);
  190. return(ReturnCode);
  191. }
  192. ReturnCode = UpgradeRoutine(MainWindowHandle,&InternalSetupData);
  193. if(ReturnCode != NO_ERROR) {
  194. SetuplogError(
  195. LogSevWarning,
  196. SETUPLOG_USE_MESSAGEID,
  197. MSG_LOG_PRINTUPG_FAILED, NULL,
  198. SETUPLOG_USE_MESSAGEID,
  199. MSG_LOG_X_RETURNED_WINERR,
  200. L"NTPRINT.DLL",
  201. ReturnCode,
  202. NULL,NULL);
  203. }
  204. FreeLibrary(NtPrintLibrary);
  205. SetupDebugPrint1( L"UpgradePrinters: leaving ret = %d", ReturnCode);
  206. return(ReturnCode);
  207. }