英文:
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_EXCLUSIVE
或LOAD_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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论