WinAPI C++ 如何从 64 位应用程序加载资源

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

我想从 EXE 中加载资源。例如,我想从 EXE path

加载版本信息 RT_VERSION

通常我会这样做

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);

但是当 path 的 exe 是 win-64 应用程序时,LoadLibrary 失败 ERROR_BAD_EXE_FORMAT : %1 is not a valid Win32 application. 无论如何从 win-64 应用程序加载资源?

Windows 只允许您将相同位数的模块加载到进程中。当您调用 LoadLibrary 时,系统假定您将使用该模块,并进行通常的初始化。为防止您需要调用 LoadLibraryEx,传递 LOAD_LIBRARY_AS_IMAGE_RESOURCE 标志:

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.