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

huangapple go评论82阅读模式
英文:

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文件是一个64位的Windows应用程序时,LoadLibrary会失败并显示ERROR_BAD_EXE_FORMAT: %1 不是一个有效的Win32应用程序。是否有办法从64位的应用程序中加载资源?

英文:

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?

答案1

得分: 1

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

如果使用此值,系统将文件映射到进程的虚拟地址空间中,将其视为图像文件。但加载程序不会加载静态导入项或执行其他通常的初始化步骤。只有在您想要加载一个DLL以提取其中的消息或资源时才使用此标志。

除非应用程序依赖于文件具有图像的内存布局,否则应该将此值与LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVELOAD_LIBRARY_AS_DATAFILE一起使用。有关更多信息,请参阅备注部分。

英文:

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.

huangapple
  • 本文由 发表于 2020年1月6日 19:09:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定