Title: Create Activation Context Implementation (with code) · Issue #100 · fancycode/MemoryModule · GitHub
Open Graph Title: Create Activation Context Implementation (with code) · Issue #100 · fancycode/MemoryModule
X Title: Create Activation Context Implementation (with code) · Issue #100 · fancycode/MemoryModule
Description: Whenever you load a DLL or start an EXE which has a manifest you should create an AcrivationContext before loading any dependent libraries (before calling BuildImportTable()) This is required when any of the imported libraries exists in ...
Open Graph Description: Whenever you load a DLL or start an EXE which has a manifest you should create an AcrivationContext before loading any dependent libraries (before calling BuildImportTable()) This is required when ...
X Description: Whenever you load a DLL or start an EXE which has a manifest you should create an AcrivationContext before loading any dependent libraries (before calling BuildImportTable()) This is required when ...
Opengraph URL: https://github.com/fancycode/MemoryModule/issues/100
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Create Activation Context Implementation (with code)","articleBody":"Whenever you load a DLL or start an EXE which has a manifest you should create an AcrivationContext before loading any dependent libraries (before calling BuildImportTable())\r\n\r\nThis is required when any of the imported libraries exists in multiple versions in the SXS Side-by-Side store of Windows.\r\nThe ActivationContext is required to tell Windows for example which version of COMCTL32.dll or MSVCR80.dll has to be loaded.\r\n\r\nI made lots of changes in the original code. Therefore I don't make a pull request now.\r\nApart from that Joachim Bauch does not merge pull requests anymore. He does not even answer emails.\r\n\r\nSo here is my code:\r\n\r\n```\r\n// Required for all DLL's and EXE's which load dependent SXS DLL's like COMCTL32.dll or MSVCR80.dll\r\n// Returns API error\r\nDWORD cMemMod::CreateActivationContext(PMEMORYMODULE module, HANDLE* ph_ActCtx, ULONG_PTR* pu32_ActCtxCookie)\r\n{\r\n DWORD u32_Error = 0;\r\n\r\n LPCWSTR u16_Manifest = module-\u003eisDLL ? ISOLATIONAWARE_MANIFEST_RESOURCE_ID : CREATEPROCESS_MANIFEST_RESOURCE_ID;\r\n HRSRC h_Resource = FindResource((HMODULE)module-\u003ecodeBase, u16_Manifest, RT_MANIFEST);\r\n if (!h_Resource)\r\n return 0; // The EXE / DLL has no manifest\r\n\r\n BYTE* u8_MemPtr = (BYTE*)LoadResource ((HMODULE)module-\u003ecodeBase, h_Resource);\r\n DWORD u32_Size = SizeofResource((HMODULE)module-\u003ecodeBase, h_Resource);\r\n\r\n WCHAR u16_TempPath[MAX_PATH];\r\n GetTempPath(MAX_PATH, u16_TempPath); // terminated with backslash\r\n\r\n // Create a random temporary file name\r\n WCHAR u16_TmpFile[MAX_PATH];\r\n swprintf(u16_TmpFile, L\"%sDLL%X.manifest\", u16_TempPath, (DWORD)__rdtsc());\r\n \r\n HANDLE h_File = CreateFileW(u16_TmpFile, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, 0);\r\n if (h_File == INVALID_HANDLE_VALUE)\r\n return GetLastError();\r\n\r\n DWORD u32_Written;\r\n if (!WriteFile(h_File, u8_MemPtr, u32_Size, \u0026u32_Written, 0))\r\n u32_Error = GetLastError();\r\n\r\n CloseHandle(h_File);\r\n\r\n if (u32_Error)\r\n return u32_Error;\r\n\r\n // Getting a manifest from memory does not work.\r\n // Even with Flags = ACTCTX_FLAG_HMODULE_VALID | ACTCTX_FLAG_RESOURCE_NAME_VALID --\u003e Windows searches for the *.DLL file \r\n // and for the *.MANIFEST file specified in lpSource on disk.\r\n // If lpSource == NULL --\u003e ERROR_MOD_NOT_FOUND. On Windows XP bug --\u003e ERROR_NOT_ENOUGH_MEMORY\r\n ACTCTXW k_Ctx = {0};\r\n k_Ctx.cbSize = sizeof(k_Ctx);\r\n k_Ctx.lpSource = u16_TmpFile; \r\n\r\n *ph_ActCtx = CreateActCtxW(\u0026k_Ctx);\r\n u32_Error = GetLastError();\r\n\r\n DeleteFile(u16_TmpFile);\r\n\r\n if (*ph_ActCtx == INVALID_HANDLE_VALUE)\r\n return u32_Error;\r\n \r\n if (!ActivateActCtx(*ph_ActCtx, pu32_ActCtxCookie))\r\n return GetLastError();\r\n\r\n TRACE(L\"\u003e\u003e Activate ActCtx: %p\", *ph_ActCtx);\r\n return 0;\r\n}\r\n```\r\n\r\nYou must call this function before calling BuildImportTable().\r\n\r\n```\r\n HANDLE h_ActContext = NULL;\r\n ULONG_PTR u32_ActCtxCookie = 0;\r\n\r\n // Create Activation Context (and activate) before loading dependent modules\r\n if (u32_Error = CreateActivationContext(pi_ModData, \u0026h_ActContext, \u0026u32_ActCtxCookie))\r\n goto error;\r\n\r\n```\r\n\r\nAfter calling the entry point of the DLL you don't need the activation context anymore.\r\n\r\n```\r\n if (h_ActContext)\r\n {\r\n TRACE(L\"\u003c\u003c Deactivate ActCtx\");\r\n\r\n if (u32_ActCtxCookie)\r\n DeactivateActCtx(0, u32_ActCtxCookie); \r\n \r\n ReleaseActCtx(h_ActContext);\r\n }\r\n\r\n```\r\n","author":{"url":"https://github.com/Elmue","@type":"Person","name":"Elmue"},"datePublished":"2020-07-02T16:35:55.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/100/MemoryModule/issues/100"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:deec127c-6e7d-ca35-67c5-92a8cfbfaa4e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B7DE:273B14:7442DA:A15F23:696F3A58 |
| html-safe-nonce | ac55bf727137966713c19569609e85cdcea532d269344127b2ef075285db19d4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCN0RFOjI3M0IxNDo3NDQyREE6QTE1RjIzOjY5NkYzQTU4IiwidmlzaXRvcl9pZCI6IjI3NTQ2NzI1NTIzMTgwMjQyODAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ec56a46a47840880a4ac78cca69d86d8d2fa535862310f5abd5200b52b01d5a4 |
| hovercard-subject-tag | issue:650066671 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/fancycode/MemoryModule/100/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e7144874eb0af0faa253d34c2ad4efe2910c501ed892fa4b22cb4d092891989d/fancycode/MemoryModule/issues/100 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e7144874eb0af0faa253d34c2ad4efe2910c501ed892fa4b22cb4d092891989d/fancycode/MemoryModule/issues/100 |
| og:image:alt | Whenever you load a DLL or start an EXE which has a manifest you should create an AcrivationContext before loading any dependent libraries (before calling BuildImportTable()) This is required when ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Elmue |
| hostname | github.com |
| expected-hostname | github.com |
| None | b278ad162d35332b6de714dfb005de04386c4d92df6475522bef910f491a35ee |
| turbo-cache-control | no-preview |
| go-import | github.com/fancycode/MemoryModule git https://github.com/fancycode/MemoryModule.git |
| octolytics-dimension-user_id | 247730 |
| octolytics-dimension-user_login | fancycode |
| octolytics-dimension-repository_id | 1018522 |
| octolytics-dimension-repository_nwo | fancycode/MemoryModule |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1018522 |
| octolytics-dimension-repository_network_root_nwo | fancycode/MemoryModule |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 39aed5006635ab6f45e6b77d23e73b08a00272a3 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width