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.

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