GetProcAddress和所有加载的库
GetProcAddress with all loaded libraries
本文关键字:加载 GetProcAddress 更新时间:2023-10-16
使用dlopen
,您可以提供NULL
作为库名,并获得一个句柄,该句柄允许您在任何加载的库中查找符号:
如果filename是NULL指针,那么返回的句柄是主程序。当给定给dlsym()时,这个句柄会导致搜索符号,然后是加载在程序启动,然后由dlopen()加载的所有共享库RTLD_GLOBAL.
你能做同样的GetProcAddress
?我想搜索Windows API的存在,但在Windows 8中加载了不同的库。
通过查看COFF头,我知道加载了哪些库,我想我可以在那里循环处理句柄…
这是我当前使用的代码:
.hpp
#include <string>
#include <stdexcept>
/**
* @~english
* Looks up a Windows API function. Make sure you set @c _WIN32_WINNT so that the definition is available at compile
* time.
* @par Example
* @code
* # undef _WIN32_WINNT
* # define _WIN32_WINNT 0x600
* # include <system/inc/nt/windows.h>
* static const auto initialize_srw_lock_ptr = FunctionPtrLookup(InitializeSRWLock, "kernel32");
* @endcode
* @param function the function definition to lookup
* @retval nullptr the function did not exist on this version of Windows
* @returns a function pointer to invoke
*/
#define FunctionPtrLookup(function, library)
FunctionLookup<decltype(function)>(#function, library)
/**
* @~english
* The return type of FunctionLookup
*/
typedef void(*FunctionLookupPtr)();
/**
* @~english
* Looks up a Windows API function.
* @param name the name of the function to find in the library
* @retval nullptr the function did not exist on this version of Windows
* @returns a function pointer to invoke
* @see FunctionPtrLookup
*/
FunctionLookupPtr FunctionLookup(const std::string& name, const std::string& library);
/// @copydoc FunctionLookup
template<typename Signature>
const Signature * FunctionLookup(const std::string& name, const std::string& library) {
return reinterpret_cast<const Signature*>(FunctionLookup(name, library));
}
.cpp
FunctionLookupPtr FunctionLookup(const std::string& name, const std::string& library) {
const auto wide_library = Utf8ToWide(library);
const auto lib = LoadLibraryW(wide_library.c_str());
if (!lib) {
return nullptr;
}
return reinterpret_cast<FunctionLookupPtr>(GetProcAddress(lib, name.c_str()));
}
理想情况下,我想要删除library
变量
你可以使用EnumProcessModules来枚举当前进程的所有加载模块,使用这里的例子:http://msdn.microsoft.com/en-us/library/ms682621%28v=vs.85%29.aspx,如果你用GetCurrentProcessId()
调用PrintModules
,它将枚举当前进程的所有HMODULE句柄(值在hMods[i]
中)。你可以将它们与GetProcAddress一起使用来查找你的函数。
你必须意识到有可能在不同的dll中找到相同的命名函数,大多数你知道WinAPI函数的dll名称。
相关文章:
- std::原子加载和存储都需要吗
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 为什么加载SDF会导致Mobilizer创建闭环错误
- C++atioglxx.pdb未加载错误glBufferData OpenGL
- 如何使用tinyxml2从XML加载父实体和子实体
- 如何在C++中使用pybind11加载一个pickle python列表
- 系统.将数组移交给c#中动态加载的c++DLL时发生AccessViolationException
- 当我尝试加载内核模块时,如何修复C++中的这个 malloc() 错误?
- 树莓上的 Libtorch 无法加载 pt 文件,但在 ubuntu 上工作
- Python ctypes:不会按预期加载 dll
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 保存/加载大量短数组到二进制文件
- 使用 SDL2 和SDL_image C++加载图像
- 无法使用加载库卸载注入的 dll (C++)
- 是否有原子加载非原子值的函数?
- 将 bmp 文件加载到 TSpeedButton 中
- 如何使用Qt 3D库加载和显示搅拌机.obj源文件场景
- 加载与引用 .NET DLL 位于同一文件夹中的引用的 .NET DLL 时"Not found"异常