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.

273 lines
9.8 KiB

  1. #include "windows.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. BOOL
  5. ExtractResourceToFile(
  6. HMODULE Module,
  7. PCWSTR ResourceType,
  8. PCWSTR ResourceName,
  9. WCHAR* TempFilePath,
  10. HANDLE* TempFileHandle
  11. )
  12. {
  13. WCHAR TempDirectory[MAX_PATH];
  14. DWORD Dword = 0;
  15. int ResourceSize = 0;
  16. void* ResourceAddress = 0;
  17. HRSRC ResourceInfo = 0;
  18. BOOL Success = FALSE;
  19. TempFilePath[0] = 0;
  20. TempDirectory[0] = 0;
  21. *TempFileHandle = 0;
  22. #define X(x) do { if (!(x)) { printf("%s failed\n", #x); goto Exit; } } while(0)
  23. X(ResourceInfo = FindResourceW(Module, ResourceName, ResourceType));
  24. X(ResourceAddress = LockResource(LoadResource(Module, ResourceInfo)));
  25. X(ResourceSize = SizeofResource(Module, ResourceInfo));
  26. X(GetTempPathW(RTL_NUMBER_OF(TempDirectory), TempDirectory));
  27. X(GetTempFileNameW(TempDirectory, NULL, 0, TempFilePath));
  28. *TempFileHandle = CreateFileW(TempFilePath, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  29. X(*TempFileHandle != INVALID_HANDLE_VALUE);
  30. X(WriteFile(*TempFileHandle, ResourceAddress, ResourceSize, &Dword, NULL));
  31. #undef X
  32. Success = TRUE;
  33. Exit:
  34. if (!Success)
  35. {
  36. int Error;
  37. printf("%s() failed %d\n", __FUNCTION__, Error = GetLastError());
  38. }
  39. return Success;
  40. }
  41. int __cdecl wmain(int argc, wchar_t** argv)
  42. {
  43. PCWSTR BaseNames[] = { L"dll", L"DLL", L"Dll" };
  44. PCWSTR Extensions[] = { L"", L".dll", L".DLL", L".Dll" };
  45. const static int Flags1[] = { 0, LOAD_LIBRARY_AS_DATAFILE };
  46. const static int Flags2[] = { LOAD_LIBRARY_AS_DATAFILE, 0 };
  47. const int* Flags = Flags1;
  48. const int NumberOfFlags = 2;
  49. int i=0, j=0, k=0;
  50. const PCWSTR CommandLine = GetCommandLineW();
  51. struct ActivationContext
  52. {
  53. HANDLE Handle;
  54. ULONG Cookie;
  55. } ActivationContexts[2] = {0}; // INVALID_HANDLE_VALUE is not convenient.
  56. HANDLE Handle = NULL;
  57. INT ResourceId = 0;
  58. INT Error = 0;
  59. WCHAR Argv0[MAX_PATH];
  60. PWSTR FilePart = 0;
  61. //
  62. // This is to target this test at older sxs without the resource feature.
  63. //
  64. DWORD ResourceFlag = ACTCTX_FLAG_RESOURCE_NAME_VALID;
  65. if ((wcsstr(CommandLine, L"-swapFlagOrder") != 0) ||
  66. (wcsstr(CommandLine, L"-swapflagorder") != 0) ||
  67. (wcsstr(CommandLine, L"-swap-flag-order") != 0) ||
  68. (wcsstr(CommandLine, L"-alterFlagOrder") != 0) ||
  69. (wcsstr(CommandLine, L"-alterflagorder") != 0) ||
  70. (wcsstr(CommandLine, L"-alter-flag-order") != 0))
  71. {
  72. Flags = Flags2;
  73. }
  74. if ((wcsstr(CommandLine, L"-filesInsteadOfResources") != 0) ||
  75. (wcsstr(CommandLine, L"-filesinsteadofresources") != 0) ||
  76. (wcsstr(CommandLine, L"-files-instead-of-resources") != 0))
  77. {
  78. ResourceFlag = 0;
  79. }
  80. if ((wcsstr(CommandLine, L"-alterEmptyExtensionOrder") != 0) ||
  81. (wcsstr(CommandLine, L"-alteremptyextensionorder") != 0) ||
  82. (wcsstr(CommandLine, L"-alter-empty-extension-order") != 0))
  83. {
  84. Extensions[0] = Extensions[1];
  85. Extensions[1] = L"";
  86. }
  87. if ((wcsstr(CommandLine, L"-alterCaseOrder") != 0) ||
  88. (wcsstr(CommandLine, L"-altercaseorder") != 0) ||
  89. (wcsstr(CommandLine, L"-alter-case-order") != 0))
  90. {
  91. // flip the case of each letter, this actually creates previously nonexistant
  92. // cases: .dLL
  93. WCHAR* String = 0;
  94. const WCHAR* ConstString = 0;
  95. for (i = 0 ; i < RTL_NUMBER_OF(BaseNames) ; ++i)
  96. {
  97. ConstString = BaseNames[i];
  98. #define X \
  99. if (ConstString) \
  100. { \
  101. String = _wcsdup(ConstString); \
  102. BaseNames[i] = String; \
  103. while (*String) \
  104. if (*String != '.') *String++ ^= 0x20; \
  105. else String++; \
  106. }
  107. X
  108. }
  109. for (i = 0 ; i < RTL_NUMBER_OF(Extensions) ; ++i)
  110. {
  111. ConstString = Extensions[i];
  112. X
  113. #undef X
  114. }
  115. }
  116. //printf("argv[0]: %ls\n", argv[0]);
  117. Argv0[0] = 0;
  118. if (!GetFullPathNameW(argv[0], RTL_NUMBER_OF(Argv0), Argv0, NULL))
  119. {
  120. printf("GetFullPathNameW(%ls) failed %d\n", argv[0], Error = GetLastError());
  121. exit(Error);
  122. }
  123. //printf("Argv0: %ls\n", Argv0);
  124. __try
  125. {
  126. for (ResourceId = 0 ; ResourceId < RTL_NUMBER_OF(ActivationContexts) ; ++ResourceId)
  127. {
  128. HANDLE ManifestFileHandle = 0;
  129. WCHAR ManifestFile[MAX_PATH];
  130. ACTCTXW CreateActivationContextParameters = {sizeof(CreateActivationContextParameters)};
  131. ManifestFile[0] = 0;
  132. CreateActivationContextParameters.dwFlags = ResourceFlag;
  133. CreateActivationContextParameters.lpResourceName = (PCWSTR)(INT_PTR)(ResourceId + 2);
  134. CreateActivationContextParameters.lpSource = Argv0;
  135. if (!ResourceFlag)
  136. {
  137. ExtractResourceToFile(
  138. NULL,
  139. //GetModuleHandleW(NULL),
  140. (PCWSTR)RT_MANIFEST,
  141. CreateActivationContextParameters.lpResourceName,
  142. ManifestFile,
  143. &ManifestFileHandle);
  144. CreateActivationContextParameters.lpSource = ManifestFile;
  145. printf("dwFlags:0x%lx\n", CreateActivationContextParameters.dwFlags);
  146. printf("lpSource:%ls\n", CreateActivationContextParameters.lpSource);
  147. }
  148. // switch access to readonly
  149. // keep the file open to prevent deletion
  150. if (ManifestFileHandle)
  151. {
  152. if (!DuplicateHandle(
  153. GetCurrentProcess(),
  154. ManifestFileHandle,
  155. GetCurrentProcess(),
  156. &ManifestFileHandle,
  157. GENERIC_READ,
  158. FALSE,
  159. DUPLICATE_CLOSE_SOURCE))
  160. {
  161. ManifestFileHandle = 0;
  162. }
  163. }
  164. Handle = CreateActCtxW(&CreateActivationContextParameters);
  165. if (Handle == INVALID_HANDLE_VALUE)
  166. Error = GetLastError();
  167. if (ManifestFileHandle)
  168. {
  169. CloseHandle(ManifestFileHandle);
  170. ManifestFileHandle = 0;
  171. }
  172. if (ManifestFile[0])
  173. {
  174. //DeleteFileW(ManifestFile);
  175. ManifestFile[0] = 0;
  176. }
  177. if (Handle == INVALID_HANDLE_VALUE)
  178. {
  179. printf("CreateActCtxW(ResourceId:%d) failed %d\n", (ResourceId + 2), Error);
  180. }
  181. else
  182. {
  183. printf("CreateActCtxW(ResourceId:%d):%p\n", (ResourceId + 2), Handle);
  184. if (!ActivateActCtx(Handle, &ActivationContexts[ResourceId].Cookie))
  185. {
  186. printf("ActivateActCtx(ResourceId:%d, Handle:%p) failed %d\n", (ResourceId + 2), Handle, Error = GetLastError());
  187. ReleaseActCtx(Handle);
  188. }
  189. else
  190. {
  191. ActivationContexts[ResourceId].Handle = Handle;
  192. }
  193. }
  194. for (i = 0 ; i < RTL_NUMBER_OF(BaseNames) ; ++i)
  195. {
  196. for (j = 0 ; j < RTL_NUMBER_OF(Extensions) ; ++j)
  197. {
  198. for (k = 0 ; k < NumberOfFlags ; ++k)
  199. {
  200. HRSRC ResourceInfo = NULL;
  201. WCHAR StringResourceBuffer[2] = {0};
  202. HANDLE Dll = 0;
  203. WCHAR Name[8+1+3+1];
  204. WCHAR ModuleNameBuffer[MAX_PATH];
  205. Name[0] = 0;
  206. ModuleNameBuffer[0] = 0;
  207. wcscat(Name, BaseNames[i]);
  208. wcscat(Name, Extensions[j]);
  209. Dll = LoadLibraryExW(Name, NULL, Flags[k]);
  210. Error = GetLastError();
  211. if (Dll)
  212. {
  213. LoadStringW(Dll, 1, StringResourceBuffer, RTL_NUMBER_OF(StringResourceBuffer));
  214. GetModuleFileNameW(Dll, ModuleNameBuffer, RTL_NUMBER_OF(ModuleNameBuffer));
  215. ResourceInfo = FindResourceW(Dll, (PCWSTR)1, (PCWSTR)RT_STRING);
  216. FreeLibrary(Dll);
  217. }
  218. printf(
  219. "LoadLibraryExW(%ls, 0x%x):%p, Error:%d, ResourceInfo:%p, String:%ls, ModuleName:%ls.\n",
  220. Name,
  221. Flags[k],
  222. Dll,
  223. Error,
  224. ResourceInfo,
  225. StringResourceBuffer,
  226. ModuleNameBuffer);
  227. }
  228. }
  229. }
  230. }
  231. for (ResourceId = RTL_NUMBER_OF(ActivationContexts) - 1 ; ResourceId >= 0 ; --ResourceId)
  232. {
  233. Handle = ActivationContexts[ResourceId].Handle;
  234. if (Handle)
  235. {
  236. ULONG Cookie = ActivationContexts[ResourceId].Cookie;
  237. if (!DeactivateActCtx(0, Cookie))
  238. {
  239. printf(
  240. "DeactivateActCtx(ResourceId:%d, Handle:%p, Cookie:0x%lx) failed %d\n",
  241. ResourceId + 2,
  242. Handle,
  243. Cookie,
  244. Error = GetLastError());
  245. }
  246. ReleaseActCtx(Handle);
  247. }
  248. }
  249. }
  250. __except(EXCEPTION_EXECUTE_HANDLER)
  251. {
  252. printf("0x%x\n", Error = GetExceptionCode());
  253. }
  254. return 0;
  255. }