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.

262 lines
7.6 KiB

  1. /*++
  2. Copyright (c) 1990-1994 Microsoft Corporation
  3. All rights reserved
  4. Module Name:
  5. Init.c
  6. Abstract:
  7. Holds initialization code for winspool.drv
  8. Author:
  9. Environment:
  10. User Mode -Win32
  11. Revision History:
  12. --*/
  13. #include "precomp.h"
  14. #pragma hdrstop
  15. #include "client.h"
  16. CRITICAL_SECTION ClientSection;
  17. // bLoadedBySpooler indicates if this instance of winspool.drv is loaded in the spooler
  18. // process. This flag is used to avoid unnecessary RPC.
  19. BOOL bLoadedBySpooler;
  20. // The following function pointers hold the spooler's server side function pointers.
  21. // This list includes most of the calls made inside the spooler except OpenPrinter and
  22. // ClosePrinter. We cant extend RPC elimination to cover (Open/Close)Printer unless
  23. // "ALL" spooler APIs use RPC elimination inside the spooler.
  24. DWORD (*fpYReadPrinter)(HANDLE, LPBYTE, DWORD, LPDWORD, BOOL);
  25. DWORD (*fpYSplReadPrinter)(HANDLE, LPBYTE *, DWORD, BOOL);
  26. DWORD (*fpYWritePrinter)(HANDLE, LPBYTE, DWORD, LPDWORD, BOOL);
  27. DWORD (*fpYSeekPrinter)(HANDLE, LARGE_INTEGER, PLARGE_INTEGER, DWORD, BOOL, BOOL);
  28. DWORD (*fpYGetPrinterDriver2)(HANDLE, LPWSTR, DWORD, LPBYTE, DWORD, LPDWORD,
  29. DWORD, DWORD, PDWORD, PDWORD, BOOL);
  30. DWORD (*fpYGetPrinterDriverDirectory)(LPWSTR, LPWSTR, DWORD, LPBYTE, DWORD, LPDWORD, BOOL);
  31. VOID (*fpYDriverUnloadComplete)(LPWSTR);
  32. DWORD (*fpYFlushPrinter)(HANDLE,LPVOID,DWORD,LPDWORD,DWORD,BOOL);
  33. DWORD (*fpYEndDocPrinter)(HANDLE,BOOL);
  34. DWORD (*fpYSetPort)(LPWSTR, LPWSTR, LPPORT_CONTAINER, BOOL);
  35. DWORD (*fpYSetJob)(HANDLE, DWORD, LPJOB_CONTAINER, DWORD, BOOL);
  36. VOID InitializeGlobalVariables()
  37. /*++
  38. Function Description -- Initializes bLoadedBySpooler and function pointers.
  39. Parameters - NONE
  40. Return Values - NONE
  41. --*/
  42. {
  43. TCHAR szSysDir[MAX_PATH];
  44. LPTSTR pszSpoolerName = NULL, pszModuleName = NULL, pszSysDir = (LPTSTR) szSysDir;
  45. BOOL bAllocSysDir = FALSE;
  46. DWORD dwNeeded, dwSize;
  47. HANDLE hLib;
  48. // Preliminary initializations
  49. bLoadedBySpooler = FALSE;
  50. fpYReadPrinter = fpYWritePrinter = NULL;
  51. fpYSplReadPrinter = NULL;
  52. fpYSeekPrinter = NULL;
  53. fpYGetPrinterDriver2 = NULL;
  54. fpYGetPrinterDriverDirectory = NULL;
  55. fpYDriverUnloadComplete = NULL;
  56. fpYFlushPrinter = NULL;
  57. fpYEndDocPrinter = NULL;
  58. hSurrogateProcess = NULL;
  59. dwSize = MAX_PATH * sizeof(TCHAR);
  60. // Get system directory
  61. if (!(dwNeeded = GetSystemDirectory(pszSysDir, MAX_PATH))) {
  62. goto CleanUp;
  63. }
  64. if (dwNeeded > dwSize) {
  65. if (pszSysDir = (LPTSTR) AllocSplMem(dwNeeded)) {
  66. bAllocSysDir = TRUE;
  67. if (!GetSystemDirectory(pszSysDir, dwNeeded / sizeof(TCHAR))) {
  68. goto CleanUp;
  69. }
  70. } else {
  71. goto CleanUp;
  72. }
  73. }
  74. dwSize = (_tcslen(pszSysDir) + 1 + _tcslen(TEXT("\\spoolsv.exe"))) * sizeof(TCHAR);
  75. if ((!(pszSpoolerName = (LPTSTR) AllocSplMem(dwSize))) ||
  76. (!(pszModuleName = (LPTSTR) AllocSplMem(dwSize)))) {
  77. goto CleanUp;
  78. }
  79. // Get spooler name
  80. _tcscpy(pszSpoolerName, pszSysDir);
  81. _tcscat(pszSpoolerName, TEXT("\\spoolsv.exe"));
  82. // Get module name. GetModuleFileName truncates the string if it is bigger than
  83. // the allocated buffer. There shouldn't be an executable spoolsv.exe* in the
  84. // system directory, which could be mistaken for the spooler.
  85. if (!GetModuleFileName(NULL, pszModuleName, dwSize / sizeof(TCHAR))) {
  86. goto CleanUp;
  87. }
  88. if (!_tcsicmp(pszSpoolerName, pszModuleName)) {
  89. // winspool.drv has been loaded by the spooler
  90. bLoadedBySpooler = TRUE;
  91. if (hLib = LoadLibrary(pszSpoolerName)) {
  92. fpYReadPrinter = (DWORD (*)(HANDLE, LPBYTE, DWORD, LPDWORD, BOOL))
  93. GetProcAddress(hLib, "YReadPrinter");
  94. fpYSplReadPrinter = (DWORD (*)(HANDLE, LPBYTE *, DWORD, BOOL))
  95. GetProcAddress(hLib, "YSplReadPrinter");
  96. fpYWritePrinter = (DWORD (*)(HANDLE, LPBYTE, DWORD, LPDWORD, BOOL))
  97. GetProcAddress(hLib, "YWritePrinter");
  98. fpYSeekPrinter = (DWORD (*)(HANDLE, LARGE_INTEGER, PLARGE_INTEGER,
  99. DWORD, BOOL, BOOL))
  100. GetProcAddress(hLib, "YSeekPrinter");
  101. fpYGetPrinterDriver2 = (DWORD (*)(HANDLE, LPWSTR, DWORD, LPBYTE, DWORD,
  102. LPDWORD, DWORD, DWORD, PDWORD, PDWORD, BOOL))
  103. GetProcAddress(hLib, "YGetPrinterDriver2");
  104. fpYGetPrinterDriverDirectory = (DWORD (*)(LPWSTR, LPWSTR, DWORD, LPBYTE, DWORD,
  105. LPDWORD, BOOL))
  106. GetProcAddress(hLib, "YGetPrinterDriverDirectory");
  107. fpYDriverUnloadComplete = (VOID (*)(LPWSTR))
  108. GetProcAddress(hLib, "YDriverUnloadComplete");
  109. fpYFlushPrinter = (DWORD (*)(HANDLE,LPVOID,DWORD,LPDWORD,DWORD,BOOL))
  110. GetProcAddress(hLib,"YFlushPrinter");
  111. fpYEndDocPrinter = (DWORD (*)(HANDLE,BOOL))
  112. GetProcAddress(hLib,"YEndDocPrinter");
  113. fpYSetPort = (DWORD (*)(LPWSTR,LPWSTR,LPPORT_CONTAINER,BOOL))
  114. GetProcAddress(hLib,"YSetPort");
  115. fpYSetJob = (DWORD (*)(HANDLE, DWORD, LPJOB_CONTAINER, DWORD, BOOL))
  116. GetProcAddress(hLib,"YSetJob");
  117. // We can leave spoolsv.exe loaded since it is in the spooler process
  118. }
  119. }
  120. CleanUp:
  121. if (pszSpoolerName) {
  122. FreeSplMem(pszSpoolerName);
  123. }
  124. if (pszModuleName) {
  125. FreeSplMem(pszModuleName);
  126. }
  127. if (bAllocSysDir) {
  128. FreeSplMem(pszSysDir);
  129. }
  130. return;
  131. }
  132. //
  133. // This entry point is called on DLL initialisation.
  134. // We need to know the module handle so we can load resources.
  135. //
  136. BOOL DllMain(
  137. IN PVOID hmod,
  138. IN DWORD Reason,
  139. IN PCONTEXT pctx OPTIONAL)
  140. {
  141. DBG_UNREFERENCED_PARAMETER(pctx);
  142. switch (Reason) {
  143. case DLL_PROCESS_ATTACH:
  144. DisableThreadLibraryCalls((HMODULE)hmod);
  145. hInst = hmod;
  146. __try {
  147. if( !bSplLibInit(NULL) ) {
  148. return FALSE;
  149. }
  150. } __except(EXCEPTION_EXECUTE_HANDLER) {
  151. SetLastError(GetExceptionCode());
  152. return FALSE;
  153. }
  154. InitializeGlobalVariables();
  155. __try {
  156. InitializeCriticalSection(&ClientSection);
  157. } __except(EXCEPTION_EXECUTE_HANDLER) {
  158. SetLastError(GetExceptionCode());
  159. return FALSE;
  160. }
  161. __try {
  162. InitializeCriticalSection(&ListAccessSem);
  163. } __except(EXCEPTION_EXECUTE_HANDLER) {
  164. DeleteCriticalSection(&ClientSection);
  165. SetLastError(GetExceptionCode());
  166. return FALSE;
  167. }
  168. __try {
  169. InitializeCriticalSection(&ProcessHndlCS);
  170. } __except(EXCEPTION_EXECUTE_HANDLER) {
  171. DeleteCriticalSection(&ClientSection);
  172. DeleteCriticalSection(&ListAccessSem);
  173. SetLastError(GetExceptionCode());
  174. return FALSE;
  175. }
  176. break;
  177. case DLL_PROCESS_DETACH:
  178. vSplLibFree();
  179. DeleteCriticalSection( &ClientSection );
  180. DeleteCriticalSection( &ListAccessSem );
  181. DeleteCriticalSection( &ProcessHndlCS);
  182. break;
  183. }
  184. return TRUE;
  185. }