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.

326 lines
11 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Certain calls are dynamically linked so that the user-mode
  4. // DLL can be used on Win9x and NT4.
  5. //
  6. // Copyright (C) Microsoft Corporation, 2000-2002.
  7. //
  8. //----------------------------------------------------------------------------
  9. #include "pch.hpp"
  10. #ifndef _WIN32_WCE
  11. #include <ntcsrdll.h>
  12. #endif
  13. #define NTDLL_APIS
  14. #include "dllimp.h"
  15. #include "cmnutil.hpp"
  16. // These entries must match the ordering in the NTDLL_CALLS structure.
  17. DYNAMIC_CALL_NAME g_NtDllCallNames[] =
  18. {
  19. "CsrGetProcessId", FALSE,
  20. "DbgBreakPoint", TRUE,
  21. "DbgPrint", TRUE,
  22. "DbgPrompt", TRUE,
  23. "DbgUiConvertStateChangeStructure", FALSE,
  24. "DbgUiGetThreadDebugObject", FALSE,
  25. "DbgUiIssueRemoteBreakin", FALSE,
  26. "DbgUiSetThreadDebugObject", FALSE,
  27. "NtAllocateVirtualMemory", TRUE,
  28. "NtClose", TRUE,
  29. "NtCreateDebugObject", FALSE,
  30. "NtDebugActiveProcess", FALSE,
  31. "NtDebugContinue", FALSE,
  32. "NtFreeVirtualMemory", TRUE,
  33. "NtOpenProcess", TRUE,
  34. "NtOpenThread", TRUE,
  35. "NtQueryInformationProcess", TRUE,
  36. "NtQueryInformationThread", TRUE,
  37. "NtQueryObject", TRUE,
  38. "NtQuerySystemInformation", TRUE,
  39. "NtRemoveProcessDebug", FALSE,
  40. "NtResumeThread", TRUE,
  41. "NtSetInformationDebugObject", FALSE,
  42. "NtSetInformationProcess", FALSE,
  43. "NtSystemDebugControl", TRUE,
  44. "NtWaitForDebugEvent", FALSE,
  45. "RtlAnsiStringToUnicodeString", TRUE,
  46. "RtlCreateProcessParameters", FALSE,
  47. "RtlCreateUserProcess", FALSE,
  48. "RtlDestroyProcessParameters", FALSE,
  49. "RtlDosPathNameToNtPathName_U", TRUE,
  50. "RtlFindMessage", FALSE,
  51. "RtlFreeHeap", TRUE,
  52. "RtlFreeUnicodeString", TRUE,
  53. "RtlGetFunctionTableListHead", FALSE,
  54. "RtlGetUnloadEventTrace", FALSE,
  55. "RtlInitAnsiString", TRUE,
  56. "RtlInitUnicodeString", TRUE,
  57. "RtlTryEnterCriticalSection", TRUE,
  58. "RtlUnicodeStringToAnsiString", TRUE,
  59. };
  60. #define NTDLL_CALL_NAMES DIMA(g_NtDllCallNames)
  61. #define NTDLL_CALL_PROCS DIMAT(g_NtDllCalls, FARPROC)
  62. NTDLL_CALLS g_NtDllCalls;
  63. DYNAMIC_CALLS_DESC g_NtDllCallsDesc =
  64. {
  65. "ntdll.dll", NTDLL_CALL_NAMES,
  66. g_NtDllCallNames, (FARPROC*)&g_NtDllCalls, NULL, FALSE,
  67. };
  68. // These entries must match the ordering in the KERNEL32_CALLS structure.
  69. DYNAMIC_CALL_NAME g_Kernel32CallNames[] =
  70. {
  71. "CreateToolhelp32Snapshot", FALSE,
  72. "DebugActiveProcessStop", FALSE,
  73. "DebugBreak", TRUE,
  74. "DebugBreakProcess", FALSE,
  75. "DebugSetProcessKillOnExit", FALSE,
  76. "Module32First", FALSE,
  77. "Module32Next", FALSE,
  78. "OpenThread", FALSE,
  79. "Process32First", FALSE,
  80. "Process32Next", FALSE,
  81. "Thread32First", FALSE,
  82. "Thread32Next", FALSE,
  83. };
  84. #define KERNEL32_CALL_NAMES DIMA(g_Kernel32CallNames)
  85. #define KERNEL32_CALL_PROCS DIMAT(g_Kernel32Calls, FARPROC)
  86. KERNEL32_CALLS g_Kernel32Calls;
  87. DYNAMIC_CALLS_DESC g_Kernel32CallsDesc =
  88. {
  89. "kernel32.dll", KERNEL32_CALL_NAMES,
  90. g_Kernel32CallNames, (FARPROC*)&g_Kernel32Calls, NULL, FALSE,
  91. };
  92. // These entries must match the ordering in the USER32_CALLS structure.
  93. DYNAMIC_CALL_NAME g_User32CallNames[] =
  94. {
  95. "PrivateKDBreakPoint", FALSE,
  96. "GetThreadDesktop", FALSE,
  97. "SwitchDesktop", FALSE,
  98. "CloseDesktop", FALSE,
  99. };
  100. #define USER32_CALL_NAMES DIMA(g_User32CallNames)
  101. #define USER32_CALL_PROCS DIMAT(g_User32Calls, FARPROC)
  102. USER32_CALLS g_User32Calls;
  103. DYNAMIC_CALLS_DESC g_User32CallsDesc =
  104. {
  105. "user32.dll", USER32_CALL_NAMES,
  106. g_User32CallNames, (FARPROC*)&g_User32Calls, NULL, FALSE,
  107. };
  108. // These entries must match the ordering in the OLE32_CALLS structure.
  109. DYNAMIC_CALL_NAME g_Ole32CallNames[] =
  110. {
  111. "CLSIDFromString", TRUE,
  112. "CoCreateInstance", TRUE,
  113. "CoGetInterfaceAndReleaseStream", TRUE,
  114. "CoInitializeEx", TRUE,
  115. "CoMarshalInterThreadInterfaceInStream", TRUE,
  116. "CoUninitialize", TRUE,
  117. "CoUnmarshalInterface", TRUE,
  118. "CoCreateGuid", TRUE,
  119. };
  120. #define OLE32_CALL_NAMES DIMA(g_Ole32CallNames)
  121. #define OLE32_CALL_PROCS DIMAT(g_Ole32Calls, FARPROC)
  122. OLE32_CALLS g_Ole32Calls;
  123. DYNAMIC_CALLS_DESC g_Ole32CallsDesc =
  124. {
  125. "ole32.dll", OLE32_CALL_NAMES,
  126. g_Ole32CallNames, (FARPROC*)&g_Ole32Calls, NULL, FALSE,
  127. };
  128. // These entries must match the ordering in the OLEAUT32_CALLS structure.
  129. DYNAMIC_CALL_NAME g_OleAut32CallNames[] =
  130. {
  131. "SysAllocStringLen", TRUE,
  132. "SysFreeString", TRUE,
  133. "VariantClear", TRUE,
  134. "VariantCopy", TRUE,
  135. "VariantInit", TRUE,
  136. };
  137. #define OLEAUT32_CALL_NAMES DIMA(g_OleAut32CallNames)
  138. #define OLEAUT32_CALL_PROCS DIMAT(g_OleAut32Calls, FARPROC)
  139. OLEAUT32_CALLS g_OleAut32Calls;
  140. DYNAMIC_CALLS_DESC g_OleAut32CallsDesc =
  141. {
  142. "oleaut32.dll", OLEAUT32_CALL_NAMES,
  143. g_OleAut32CallNames, (FARPROC*)&g_OleAut32Calls, NULL, FALSE,
  144. };
  145. // These entries must match the ordering in the SHLWAPI_CALLS structure.
  146. DYNAMIC_CALL_NAME g_ShlWapiCallNames[] =
  147. {
  148. "PathIsDirectoryA", TRUE,
  149. };
  150. #define SHLWAPI_CALL_NAMES DIMA(g_ShlWapiCallNames)
  151. #define SHLWAPI_CALL_PROCS DIMAT(g_ShlWapiCalls, FARPROC)
  152. SHLWAPI_CALLS g_ShlWapiCalls;
  153. DYNAMIC_CALLS_DESC g_ShlWapiCallsDesc =
  154. {
  155. "shlwapi.dll", SHLWAPI_CALL_NAMES,
  156. g_ShlWapiCallNames, (FARPROC*)&g_ShlWapiCalls, NULL, FALSE,
  157. };
  158. #ifndef _WIN32_WCE
  159. // These entries must match the ordering in the CRYPT32_CALLS structure.
  160. DYNAMIC_CALL_NAME g_Crypt32CallNames[] =
  161. {
  162. "CertFindCertificateInStore", FALSE,
  163. "CertFindChainInStore", FALSE,
  164. "CertFreeCertificateChain", FALSE,
  165. "CertFreeCertificateContext", FALSE,
  166. "CertGetCertificateChain", FALSE,
  167. "CertOpenStore", FALSE,
  168. "CertOpenSystemStoreA", FALSE,
  169. "CertVerifyCertificateChainPolicy", FALSE,
  170. };
  171. #define CRYPT32_CALL_NAMES DIMA(g_Crypt32CallNames)
  172. #define CRYPT32_CALL_PROCS DIMAT(g_Crypt32Calls, FARPROC)
  173. CRYPT32_CALLS g_Crypt32Calls;
  174. DYNAMIC_CALLS_DESC g_Crypt32CallsDesc =
  175. {
  176. "crypt32.dll", CRYPT32_CALL_NAMES,
  177. g_Crypt32CallNames, (FARPROC*)&g_Crypt32Calls, NULL, FALSE,
  178. };
  179. // These entries must match the ordering in the ADVAPI32_CALLS structure.
  180. DYNAMIC_CALL_NAME g_Advapi32CallNames[] =
  181. {
  182. "EnumServicesStatusExA", FALSE,
  183. "EnumServicesStatusExW", FALSE,
  184. "OpenSCManagerA", FALSE,
  185. "OpenSCManagerW", FALSE,
  186. "GetEventLogInformation", FALSE
  187. };
  188. #define ADVAPI32_CALL_NAMES DIMA(g_Advapi32CallNames)
  189. #define ADVAPI32_CALL_PROCS DIMAT(g_Advapi32Calls, FARPROC)
  190. ADVAPI32_CALLS g_Advapi32Calls;
  191. DYNAMIC_CALLS_DESC g_Advapi32CallsDesc =
  192. {
  193. "advapi32.dll", ADVAPI32_CALL_NAMES,
  194. g_Advapi32CallNames, (FARPROC*)&g_Advapi32Calls, NULL, FALSE,
  195. };
  196. #endif // #ifndef _WIN32_WCE
  197. #ifndef NT_NATIVE
  198. HRESULT
  199. InitDynamicCalls(DYNAMIC_CALLS_DESC* Desc)
  200. {
  201. if (Desc->Initialized)
  202. {
  203. return S_OK;
  204. }
  205. C_ASSERT(NTDLL_CALL_NAMES == NTDLL_CALL_PROCS);
  206. C_ASSERT(KERNEL32_CALL_NAMES == KERNEL32_CALL_PROCS);
  207. C_ASSERT(USER32_CALL_NAMES == USER32_CALL_PROCS);
  208. C_ASSERT(OLE32_CALL_NAMES == OLE32_CALL_PROCS);
  209. C_ASSERT(OLEAUT32_CALL_NAMES == OLEAUT32_CALL_PROCS);
  210. #ifndef _WIN32_WCE
  211. C_ASSERT(CRYPT32_CALL_NAMES == CRYPT32_CALL_PROCS);
  212. C_ASSERT(ADVAPI32_CALL_NAMES == ADVAPI32_CALL_PROCS);
  213. #endif
  214. ZeroMemory(Desc->Procs, Desc->Count * sizeof(*Desc->Procs));
  215. Desc->Dll = LoadLibrary(Desc->DllName);
  216. if (Desc->Dll == NULL)
  217. {
  218. return HRESULT_FROM_WIN32(GetLastError());
  219. }
  220. ULONG i;
  221. DYNAMIC_CALL_NAME* Name = Desc->Names;
  222. FARPROC* Proc = Desc->Procs;
  223. for (i = 0; i < Desc->Count; i++)
  224. {
  225. *Proc = GetProcAddress(Desc->Dll, Name->Name);
  226. if (*Proc == NULL && Name->Required)
  227. {
  228. return E_NOINTERFACE;
  229. }
  230. Proc++;
  231. Name++;
  232. }
  233. Desc->Initialized = TRUE;
  234. return S_OK;
  235. }
  236. #else // #ifndef NT_NATIVE
  237. HRESULT
  238. InitDynamicCalls(DYNAMIC_CALLS_DESC* Desc)
  239. {
  240. if (Desc != &g_NtDllCallsDesc)
  241. {
  242. ZeroMemory(Desc->Procs, Desc->Count * sizeof(*Desc->Procs));
  243. return E_NOINTERFACE;
  244. }
  245. C_ASSERT(NTDLL_CALL_NAMES == NTDLL_CALL_PROCS);
  246. g_NtDllCalls.CsrGetProcessId = CsrGetProcessId;
  247. g_NtDllCalls.DbgBreakPoint = DbgBreakPoint;
  248. g_NtDllCalls.DbgPrint = DbgPrint;
  249. g_NtDllCalls.DbgPrompt = DbgPrompt;
  250. g_NtDllCalls.DbgUiConvertStateChangeStructure =
  251. DbgUiConvertStateChangeStructure;
  252. g_NtDllCalls.DbgUiGetThreadDebugObject = DbgUiGetThreadDebugObject;
  253. g_NtDllCalls.DbgUiIssueRemoteBreakin = DbgUiIssueRemoteBreakin;
  254. g_NtDllCalls.DbgUiSetThreadDebugObject = DbgUiSetThreadDebugObject;
  255. g_NtDllCalls.NtAllocateVirtualMemory = NtAllocateVirtualMemory;
  256. g_NtDllCalls.NtClose = NtClose;
  257. g_NtDllCalls.NtCreateDebugObject = NtCreateDebugObject;
  258. g_NtDllCalls.NtDebugActiveProcess = NtDebugActiveProcess;
  259. g_NtDllCalls.NtDebugContinue = NtDebugContinue;
  260. g_NtDllCalls.NtFreeVirtualMemory = NtFreeVirtualMemory;
  261. g_NtDllCalls.NtOpenProcess = NtOpenProcess;
  262. g_NtDllCalls.NtOpenThread = NtOpenThread;
  263. g_NtDllCalls.NtQueryInformationProcess = NtQueryInformationProcess;
  264. g_NtDllCalls.NtQueryInformationThread = NtQueryInformationThread;
  265. g_NtDllCalls.NtQueryObject = NtQueryObject;
  266. g_NtDllCalls.NtQuerySystemInformation = NtQuerySystemInformation;
  267. g_NtDllCalls.NtRemoveProcessDebug = NtRemoveProcessDebug;
  268. g_NtDllCalls.NtResumeThread = NtResumeThread;
  269. g_NtDllCalls.NtSetInformationDebugObject = NtSetInformationDebugObject;
  270. g_NtDllCalls.NtSetInformationProcess = NtSetInformationProcess;
  271. g_NtDllCalls.NtSystemDebugControl = NtSystemDebugControl;
  272. g_NtDllCalls.NtWaitForDebugEvent = NtWaitForDebugEvent;
  273. g_NtDllCalls.RtlAnsiStringToUnicodeString = RtlAnsiStringToUnicodeString;
  274. g_NtDllCalls.RtlCreateProcessParameters = RtlCreateProcessParameters;
  275. g_NtDllCalls.RtlCreateUserProcess = RtlCreateUserProcess;
  276. g_NtDllCalls.RtlDestroyProcessParameters = RtlDestroyProcessParameters;
  277. g_NtDllCalls.RtlDosPathNameToNtPathName_U = RtlDosPathNameToNtPathName_U;
  278. g_NtDllCalls.RtlFindMessage = RtlFindMessage;
  279. g_NtDllCalls.RtlFreeHeap = RtlFreeHeap;
  280. g_NtDllCalls.RtlFreeUnicodeString = RtlFreeUnicodeString;
  281. #ifndef _X86_
  282. g_NtDllCalls.RtlGetFunctionTableListHead = RtlGetFunctionTableListHead;
  283. #endif
  284. g_NtDllCalls.RtlGetUnloadEventTrace = RtlGetUnloadEventTrace;
  285. g_NtDllCalls.RtlInitAnsiString = RtlInitAnsiString;
  286. g_NtDllCalls.RtlInitUnicodeString = RtlInitUnicodeString;
  287. g_NtDllCalls.RtlTryEnterCriticalSection = RtlTryEnterCriticalSection;
  288. g_NtDllCalls.RtlUnicodeStringToAnsiString = RtlUnicodeStringToAnsiString;
  289. return S_OK;
  290. }
  291. #endif // #ifndef NT_NATIVE