c++winapiresourceswin64

WinAPI C++ how to load resources from 64-bit application


I want to load out resources from an EXE. For example I want to load version info RT_VERSION, from EXE path

Normally I would do this way

HMODULE lib = LoadLibrary(path);

HRSRC resVersion = FindResource(lib, MAKEINTRESOURCE(1), RT_VERSION);
DWORD resVersionSize = SizeofResource(lib, resVersion);
HGLOBAL resVersionLoad = LoadResource(lib, resVersion);
LPVOID resVersionData = LockResource(lib);

But when the exe of path is a win-64 application, LoadLibrary fails with ERROR_BAD_EXE_FORMAT : %1 is not a valid Win32 application. Is there anyway to load resources from win-64 application?


Solution

  • Windows only allows you to load modules of the same bitness into a process. When you are calling LoadLibrary, the system assumes that you will be using that module, and does the usual initialization. To prevent that you need to call LoadLibraryEx instead, passing the LOAD_LIBRARY_AS_IMAGE_RESOURCE flag:

    If this value is used, the system maps the file into the process's virtual address space as an image file. However, the loader does not load the static imports or perform the other usual initialization steps. Use this flag when you want to load a DLL only to extract messages or resources from it.

    Unless the application depends on the file having the in-memory layout of an image, this value should be used with either LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE or LOAD_LIBRARY_AS_DATAFILE. For more information, see the Remarks section.