Title: 64-bit DLL compiled by G++ failed to load. · Issue #108 · fancycode/MemoryModule · GitHub
Open Graph Title: 64-bit DLL compiled by G++ failed to load. · Issue #108 · fancycode/MemoryModule
X Title: 64-bit DLL compiled by G++ failed to load. · Issue #108 · fancycode/MemoryModule
Description: I have a 64-bit DLL, with this source code: #include
Open Graph Description: I have a 64-bit DLL, with this source code: #include
X Description: I have a 64-bit DLL, with this source code: #include <iostream> extern "C" { __declspec(dllexport) __cdecl void Greet() { std::cout << "Hello World!" << std::e...
Opengraph URL: https://github.com/fancycode/MemoryModule/issues/108
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"64-bit DLL compiled by G++ failed to load.","articleBody":"I have a **64-bit** DLL, with this source code:\r\n```C++\r\n#include \u003ciostream\u003e\r\n\r\nextern \"C\"\r\n{\r\n\t__declspec(dllexport) __cdecl void Greet()\r\n\t{\r\n\t\tstd::cout \u003c\u003c \"Hello World!\" \u003c\u003c std::endl;\r\n\t}\r\n\r\n\t__declspec(dllexport) __cdecl int addNumbers(int a, int b)\r\n\t{\r\n\t\tstd::cout \u003c\u003c \"Adding \" \u003c\u003c a \u003c\u003c \" and \" \u003c\u003c b \u003c\u003c \"...\" \u003c\u003c std::endl;\r\n\t\treturn a + b;\r\n\t}\r\n}\r\n```\r\nI compiled this using g++, downloadable from [here](https://github.com/brechtsanders/winlibs_mingw/releases/tag/11.1.0-12.0.0-9.0.0-r3).\r\n\u003cdetails\u003e\r\n \u003csummary\u003eThis is my g++ version \u003c/summary\u003e\r\ng++ (MinGW-W64 x86_64-posix-seh, built by Brecht Sanders) 11.1.0\r\nCopyright (C) 2021 Free Software Foundation, Inc.\r\nThis is free software; see the source for copying conditions. There is NO\r\nwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r\n\u003c/details\u003e\r\n \r\nThis is the command I used: \r\n```\r\ng++ SampleDll.cpp -o SampleDLL.dll --shared \r\n```\r\n\r\n# I. Attempt to load from file.\r\nI have a C source file (always compiled using `g++ Test.c MemoryModule.c -o Test.exe -D_WIN64`)\r\nand copied the whole `LoadFromFile` function from the demo to my source file:\r\n\u003cdetails\u003e\r\n \u003csummary\u003eThe source \u003c/summary\u003e\r\n\r\n\r\n```C++\r\nvoid LoadFromFile(void)\r\n{\r\n addNumberProc addNumber;\r\n HRSRC resourceInfo;\r\n DWORD resourceSize;\r\n LPVOID resourceData;\r\n TCHAR buffer[100];\r\n\r\n HINSTANCE handle = LoadLibrary(DLL_FILE);\r\n if (handle == NULL)\r\n return;\r\n\r\n addNumber = (addNumberProc)GetProcAddress(handle, \"addNumbers\");\r\n _tprintf(_T(\"From file: %d\\n\"), addNumber(1, 2));\r\n\r\n resourceInfo = FindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);\r\n _tprintf(_T(\"FindResource returned 0x%p\\n\"), resourceInfo);\r\n\r\n resourceSize = SizeofResource(handle, resourceInfo);\r\n resourceData = LoadResource(handle, resourceInfo);\r\n _tprintf(_T(\"Resource data: %ld bytes at 0x%p\\n\"), resourceSize, resourceData);\r\n\r\n LoadString(handle, 1, buffer, sizeof(buffer));\r\n _tprintf(_T(\"String1: %s\\n\"), buffer);\r\n\r\n LoadString(handle, 20, buffer, sizeof(buffer));\r\n _tprintf(_T(\"String2: %s\\n\"), buffer);\r\n\r\n FreeLibrary(handle);\r\n}\r\n```\r\n\u003c/details\u003e\r\n\r\nIt loads properly:\r\n```\r\nAdding 1 and 2...\r\nFrom file: 3\r\nFindResource returned 0x0000000000000000\r\nResource data: 0 bytes at 0x0000000000000000\r\nString1:\r\nString2: \r\n```\r\n\r\n# II. Attempt to load from Memory\r\nI also copied the `LoadFromMemory` and the `ReadLibrary` function:\r\n\u003cdetails\u003e\r\n \u003csummary\u003eThe source \u003c/summary\u003e\r\n\r\n\r\n```C++\r\nvoid* ReadLibrary(size_t* pSize) {\r\n size_t read;\r\n void* result;\r\n FILE* fp;\r\n\r\n fp = _tfopen(DLL_FILE, _T(\"rb\"));\r\n if (fp == NULL)\r\n {\r\n _tprintf(_T(\"Can't open DLL file \\\"%s\\\".\"), DLL_FILE);\r\n return NULL;\r\n }\r\n\r\n fseek(fp, 0, SEEK_END);\r\n *pSize = (size_t)(ftell(fp));\r\n if (*pSize == 0)\r\n {\r\n fclose(fp);\r\n return NULL;\r\n }\r\n\r\n result = (unsigned char *)malloc(*pSize);\r\n if (result == NULL)\r\n {\r\n return NULL;\r\n }\r\n\r\n fseek(fp, 0, SEEK_SET);\r\n read = fread(result, 1, *pSize, fp);\r\n fclose(fp);\r\n if (read != *pSize)\r\n {\r\n free(result);\r\n return NULL;\r\n }\r\n\r\n return result;\r\n}\r\n\r\nvoid LoadFromMemory(void)\r\n{\r\n void *data;\r\n size_t size;\r\n HMEMORYMODULE handle;\r\n addNumberProc addNumber;\r\n HMEMORYRSRC resourceInfo;\r\n DWORD resourceSize;\r\n LPVOID resourceData;\r\n TCHAR buffer[100];\r\n\r\n data = ReadLibrary(\u0026size);\r\n if (data == NULL)\r\n {\r\n return;\r\n }\r\n\r\n handle = MemoryLoadLibrary(data, size);\r\n\r\n if (handle == NULL)\r\n {\r\n\t\t_tprintf(_T(\"%i\"), GetLastError());\r\n _tprintf(_T(\"Can't load library from memory.\\n\"));\r\n goto exit;\r\n }\r\n\r\n addNumber = (addNumberProc)MemoryGetProcAddress(handle, \"addNumbers\");\r\n _tprintf(_T(\"From memory: %d\\n\"), addNumber(66, 99));\r\n\r\n resourceInfo = MemoryFindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);\r\n _tprintf(_T(\"MemoryFindResource returned 0x%p\\n\"), resourceInfo);\r\n\r\n resourceSize = MemorySizeofResource(handle, resourceInfo);\r\n resourceData = MemoryLoadResource(handle, resourceInfo);\r\n _tprintf(_T(\"Memory resource data: %ld bytes at 0x%p\\n\"), resourceSize, resourceData);\r\n\r\n MemoryLoadString(handle, 1, buffer, sizeof(buffer));\r\n _tprintf(_T(\"String1: %s\\n\"), buffer);\r\n\r\n MemoryLoadString(handle, 20, buffer, sizeof(buffer));\r\n _tprintf(_T(\"String2: %s\\n\"), buffer);\r\n\r\n MemoryFreeLibrary(handle);\r\n\r\nexit:\r\n free(data);\r\n}\r\n```\r\n\u003c/details\u003e\r\n\r\nThis time, the test program failed, with an access violation.\r\n\r\n# III. Attempt to use C#'s P/Invoke:\r\nIt just works, using a basic `DllImport` attribute.\r\n\r\n# IV. Attempt to use a translated MemoryModule version, from a managed executable:\r\nFails, for the same reason.\r\n\r\n# V. Some more stuff.\r\nAfter using a C# debugger on the translated version, and various `printf` debugging on the original library, I found [this line of code](https://github.com/fancycode/MemoryModule/blob/master/MemoryModule.c#L744)'s the culprit:\r\n\r\n```\r\n BOOL successfull = (*DllEntry)((HINSTANCE)code, DLL_PROCESS_ATTACH, 0);\r\n```\r\nThe access violation happened right after calling the dll entry point, and not because of a null pointer.\r\n\r\n# TL;DR\r\n- I have a C++ dll, **64-bit**, compiled using **G++ on MinGW-w64**, which is perfectly valid (`P/Invoke`-able, and `LoadLibrary`-able).\r\n- Loading it using MemoryModule gives an Access Violation error when trying to call the dll entry point.\r\n- Any other dll's (Compiled using MSVC (64-bit, or 32-bit), or using the lesser-known Orange C compiler (32-bit only)) works normally on MemoryModule.\r\n\r\nI do not have any experience with such low level programming, please help me explain why MemoryModule crashed when calling this entry point. Thanks in advance.","author":{"url":"https://github.com/trungnt2910","@type":"Person","name":"trungnt2910"},"datePublished":"2021-08-24T14:27:56.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/108/MemoryModule/issues/108"}
| 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:0f360c51-cb28-dcb6-af43-67818892d1f3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BE9A:12A9E6:345AC2:4A98D4:696F54C4 |
| html-safe-nonce | d01f66e44227aaea6920babbdfc3de266700d045a2340950fae26c3b7fc4cc40 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTlBOjEyQTlFNjozNDVBQzI6NEE5OEQ0OjY5NkY1NEM0IiwidmlzaXRvcl9pZCI6IjU3NDQxNjI5NjA3ODMxMzU5NDAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 1c2dc41758922d8967d4d8bc72c10a13cf63ec8081db817e37ca8cf6b4e81ab8 |
| hovercard-subject-tag | issue:978162295 |
| 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/108/issue_layout |
| twitter:image | https://opengraph.githubassets.com/19cf2a8dc2509a9470cfb14cb7976c5b56de0eac884baa5bec972c8cd9dda1f8/fancycode/MemoryModule/issues/108 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/19cf2a8dc2509a9470cfb14cb7976c5b56de0eac884baa5bec972c8cd9dda1f8/fancycode/MemoryModule/issues/108 |
| og:image:alt | I have a 64-bit DLL, with this source code: #include |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | trungnt2910 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 49bb2ee76ae1b4ec758faefafda636ff20b05a9708bb290d28422cdf542ae979 |
| 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 | 036f37fe552777ac9c6ee661957f947285aab936 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width