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.

265 lines
8.1 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-2001.
  7. //
  8. //----------------------------------------------------------------------------
  9. #include "pch.hpp"
  10. #include <ntcsrdll.h>
  11. #include "dllimp.h"
  12. #include "cmnutil.hpp"
  13. // These entries must match the ordering in the NTDLL_CALLS structure.
  14. DYNAMIC_CALL_NAME g_NtDllCallNames[] =
  15. {
  16. "CsrGetProcessId", FALSE,
  17. "DbgBreakPoint", TRUE,
  18. "DbgPrint", TRUE,
  19. "DbgPrompt", TRUE,
  20. "DbgUiConvertStateChangeStructure", FALSE,
  21. "DbgUiGetThreadDebugObject", FALSE,
  22. "DbgUiIssueRemoteBreakin", FALSE,
  23. "DbgUiSetThreadDebugObject", FALSE,
  24. "NtAllocateVirtualMemory", TRUE,
  25. "NtClose", TRUE,
  26. "NtCreateDebugObject", FALSE,
  27. "NtDebugActiveProcess", FALSE,
  28. "NtDebugContinue", FALSE,
  29. "NtFreeVirtualMemory", TRUE,
  30. "NtOpenProcess", TRUE,
  31. "NtOpenThread", TRUE,
  32. "NtQueryInformationProcess", TRUE,
  33. "NtQueryInformationThread", TRUE,
  34. "NtQueryObject", TRUE,
  35. "NtQuerySystemInformation", TRUE,
  36. "NtRemoveProcessDebug", FALSE,
  37. "NtSetInformationDebugObject", FALSE,
  38. "NtSetInformationProcess", FALSE,
  39. "NtSystemDebugControl", TRUE,
  40. "NtWaitForDebugEvent", FALSE,
  41. "RtlFreeHeap", TRUE,
  42. "RtlGetFunctionTableListHead", FALSE,
  43. "RtlTryEnterCriticalSection", TRUE,
  44. "RtlUnicodeStringToAnsiString", TRUE,
  45. };
  46. #define NTDLL_CALL_NAMES DIMA(g_NtDllCallNames)
  47. #define NTDLL_CALL_PROCS DIMAT(g_NtDllCalls, FARPROC)
  48. NTDLL_CALLS g_NtDllCalls;
  49. DYNAMIC_CALLS_DESC g_NtDllCallsDesc =
  50. {
  51. "ntdll.dll", NTDLL_CALL_NAMES,
  52. g_NtDllCallNames, (FARPROC*)&g_NtDllCalls, NULL, FALSE,
  53. };
  54. // These entries must match the ordering in the KERNEL32_CALLS structure.
  55. DYNAMIC_CALL_NAME g_Kernel32CallNames[] =
  56. {
  57. "CreateToolhelp32Snapshot", FALSE,
  58. "DebugActiveProcessStop", FALSE,
  59. "DebugBreak", TRUE,
  60. "DebugBreakProcess", FALSE,
  61. "DebugSetProcessKillOnExit", FALSE,
  62. "Module32First", FALSE,
  63. "Module32Next", FALSE,
  64. "Process32First", FALSE,
  65. "Process32Next", FALSE,
  66. "Thread32First", FALSE,
  67. "Thread32Next", FALSE,
  68. };
  69. #define KERNEL32_CALL_NAMES DIMA(g_Kernel32CallNames)
  70. #define KERNEL32_CALL_PROCS DIMAT(g_Kernel32Calls, FARPROC)
  71. KERNEL32_CALLS g_Kernel32Calls;
  72. DYNAMIC_CALLS_DESC g_Kernel32CallsDesc =
  73. {
  74. "kernel32.dll", KERNEL32_CALL_NAMES,
  75. g_Kernel32CallNames, (FARPROC*)&g_Kernel32Calls, NULL, FALSE,
  76. };
  77. // These entries must match the ordering in the USER32_CALLS structure.
  78. DYNAMIC_CALL_NAME g_User32CallNames[] =
  79. {
  80. "PrivateKDBreakPoint", FALSE,
  81. };
  82. #define USER32_CALL_NAMES DIMA(g_User32CallNames)
  83. #define USER32_CALL_PROCS DIMAT(g_User32Calls, FARPROC)
  84. USER32_CALLS g_User32Calls;
  85. DYNAMIC_CALLS_DESC g_User32CallsDesc =
  86. {
  87. "user32.dll", USER32_CALL_NAMES,
  88. g_User32CallNames, (FARPROC*)&g_User32Calls, NULL, FALSE,
  89. };
  90. // These entries must match the ordering in the OLE32_CALLS structure.
  91. DYNAMIC_CALL_NAME g_Ole32CallNames[] =
  92. {
  93. "CLSIDFromString", TRUE,
  94. "CoCreateInstance", TRUE,
  95. "CoInitializeEx", TRUE,
  96. "CoUninitialize", TRUE,
  97. };
  98. #define OLE32_CALL_NAMES DIMA(g_Ole32CallNames)
  99. #define OLE32_CALL_PROCS DIMAT(g_Ole32Calls, FARPROC)
  100. OLE32_CALLS g_Ole32Calls;
  101. DYNAMIC_CALLS_DESC g_Ole32CallsDesc =
  102. {
  103. "ole32.dll", OLE32_CALL_NAMES,
  104. g_Ole32CallNames, (FARPROC*)&g_Ole32Calls, NULL, FALSE,
  105. };
  106. // These entries must match the ordering in the OLEAUT32_CALLS structure.
  107. DYNAMIC_CALL_NAME g_OleAut32CallNames[] =
  108. {
  109. "SysFreeString", TRUE,
  110. };
  111. #define OLEAUT32_CALL_NAMES DIMA(g_OleAut32CallNames)
  112. #define OLEAUT32_CALL_PROCS DIMAT(g_OleAut32Calls, FARPROC)
  113. OLEAUT32_CALLS g_OleAut32Calls;
  114. DYNAMIC_CALLS_DESC g_OleAut32CallsDesc =
  115. {
  116. "oleaut32.dll", OLEAUT32_CALL_NAMES,
  117. g_OleAut32CallNames, (FARPROC*)&g_OleAut32Calls, NULL, FALSE,
  118. };
  119. // These entries must match the ordering in the CRYPT32_CALLS structure.
  120. DYNAMIC_CALL_NAME g_Crypt32CallNames[] =
  121. {
  122. "CertFindCertificateInStore", FALSE,
  123. "CertFindChainInStore", FALSE,
  124. "CertFreeCertificateChain", FALSE,
  125. "CertFreeCertificateContext", FALSE,
  126. "CertGetCertificateChain", FALSE,
  127. "CertOpenStore", FALSE,
  128. "CertOpenSystemStoreA", FALSE,
  129. "CertVerifyCertificateChainPolicy", FALSE,
  130. };
  131. #define CRYPT32_CALL_NAMES DIMA(g_Crypt32CallNames)
  132. #define CRYPT32_CALL_PROCS DIMAT(g_Crypt32Calls, FARPROC)
  133. CRYPT32_CALLS g_Crypt32Calls;
  134. DYNAMIC_CALLS_DESC g_Crypt32CallsDesc =
  135. {
  136. "crypt32.dll", CRYPT32_CALL_NAMES,
  137. g_Crypt32CallNames, (FARPROC*)&g_Crypt32Calls, NULL, FALSE,
  138. };
  139. // These entries must match the ordering in the ADVAPI32_CALLS structure.
  140. DYNAMIC_CALL_NAME g_Advapi32CallNames[] =
  141. {
  142. "EnumServicesStatusExA", FALSE,
  143. "OpenSCManagerA", FALSE,
  144. };
  145. #define ADVAPI32_CALL_NAMES DIMA(g_Advapi32CallNames)
  146. #define ADVAPI32_CALL_PROCS DIMAT(g_Advapi32Calls, FARPROC)
  147. ADVAPI32_CALLS g_Advapi32Calls;
  148. DYNAMIC_CALLS_DESC g_Advapi32CallsDesc =
  149. {
  150. "advapi32.dll", ADVAPI32_CALL_NAMES,
  151. g_Advapi32CallNames, (FARPROC*)&g_Advapi32Calls, NULL, FALSE,
  152. };
  153. #ifndef NT_NATIVE
  154. HRESULT
  155. InitDynamicCalls(DYNAMIC_CALLS_DESC* Desc)
  156. {
  157. if (Desc->Initialized)
  158. {
  159. return S_OK;
  160. }
  161. C_ASSERT(NTDLL_CALL_NAMES == NTDLL_CALL_PROCS);
  162. C_ASSERT(KERNEL32_CALL_NAMES == KERNEL32_CALL_PROCS);
  163. C_ASSERT(USER32_CALL_NAMES == USER32_CALL_PROCS);
  164. C_ASSERT(OLE32_CALL_NAMES == OLE32_CALL_PROCS);
  165. C_ASSERT(OLEAUT32_CALL_NAMES == OLEAUT32_CALL_PROCS);
  166. C_ASSERT(CRYPT32_CALL_NAMES == CRYPT32_CALL_PROCS);
  167. C_ASSERT(ADVAPI32_CALL_NAMES == ADVAPI32_CALL_PROCS);
  168. ZeroMemory(Desc->Procs, Desc->Count * sizeof(*Desc->Procs));
  169. Desc->Dll = LoadLibrary(Desc->DllName);
  170. if (Desc->Dll == NULL)
  171. {
  172. return HRESULT_FROM_WIN32(GetLastError());
  173. }
  174. ULONG i;
  175. DYNAMIC_CALL_NAME* Name = Desc->Names;
  176. FARPROC* Proc = Desc->Procs;
  177. for (i = 0; i < Desc->Count; i++)
  178. {
  179. *Proc = GetProcAddress(Desc->Dll, Name->Name);
  180. if (*Proc == NULL && Name->Required)
  181. {
  182. return E_NOINTERFACE;
  183. }
  184. Proc++;
  185. Name++;
  186. }
  187. Desc->Initialized = TRUE;
  188. return S_OK;
  189. }
  190. #else // #ifndef NT_NATIVE
  191. HRESULT
  192. InitDynamicCalls(DYNAMIC_CALLS_DESC* Desc)
  193. {
  194. if (Desc != &g_NtDllCallsDesc)
  195. {
  196. ZeroMemory(Desc->Procs, Desc->Count * sizeof(*Desc->Procs));
  197. return E_NOINTERFACE;
  198. }
  199. C_ASSERT(NTDLL_CALL_NAMES == NTDLL_CALL_PROCS);
  200. g_NtDllCalls.CsrGetProcessId = CsrGetProcessId;
  201. g_NtDllCalls.DbgBreakPoint = DbgBreakPoint;
  202. g_NtDllCalls.DbgPrint = DbgPrint;
  203. g_NtDllCalls.DbgPrompt = DbgPrompt;
  204. g_NtDllCalls.DbgUiConvertStateChangeStructure =
  205. DbgUiConvertStateChangeStructure;
  206. g_NtDllCalls.DbgUiGetThreadDebugObject = DbgUiGetThreadDebugObject;
  207. g_NtDllCalls.DbgUiIssueRemoteBreakin = DbgUiIssueRemoteBreakin;
  208. g_NtDllCalls.DbgUiSetThreadDebugObject = DbgUiSetThreadDebugObject;
  209. g_NtDllCalls.NtAllocateVirtualMemory = NtAllocateVirtualMemory;
  210. g_NtDllCalls.NtClose = NtClose;
  211. g_NtDllCalls.NtCreateDebugObject = NtCreateDebugObject;
  212. g_NtDllCalls.NtDebugActiveProcess = NtDebugActiveProcess;
  213. g_NtDllCalls.NtDebugContinue = NtDebugContinue;
  214. g_NtDllCalls.NtFreeVirtualMemory = NtFreeVirtualMemory;
  215. g_NtDllCalls.NtOpenProcess = NtOpenProcess;
  216. g_NtDllCalls.NtOpenThread = NtOpenThread;
  217. g_NtDllCalls.NtQueryInformationProcess = NtQueryInformationProcess;
  218. g_NtDllCalls.NtQueryInformationThread = NtQueryInformationThread;
  219. g_NtDllCalls.NtQueryObject = NtQueryObject;
  220. g_NtDllCalls.NtQuerySystemInformation = NtQuerySystemInformation;
  221. g_NtDllCalls.NtRemoveProcessDebug = NtRemoveProcessDebug;
  222. g_NtDllCalls.NtSetInformationDebugObject = NtSetInformationDebugObject;
  223. g_NtDllCalls.NtSetInformationProcess = NtSetInformationProcess;
  224. g_NtDllCalls.NtSystemDebugControl = NtSystemDebugControl;
  225. g_NtDllCalls.NtWaitForDebugEvent = NtWaitForDebugEvent;
  226. g_NtDllCalls.RtlFreeHeap = RtlFreeHeap;
  227. #ifndef _X86_
  228. g_NtDllCalls.RtlGetFunctionTableListHead = RtlGetFunctionTableListHead;
  229. #endif
  230. g_NtDllCalls.RtlTryEnterCriticalSection = RtlTryEnterCriticalSection;
  231. g_NtDllCalls.RtlUnicodeStringToAnsiString = RtlUnicodeStringToAnsiString;
  232. return S_OK;
  233. }
  234. #endif // #ifndef NT_NATIVE