With dlopen
你可以提供NULL
作为库名称并获取一个句柄,使您可以在其中查找符号any加载的库的数量:
如果文件名是 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
多变的。
您可以使用枚举进程模块 http://msdn.microsoft.com/en-us/library/ms682631%28VS.85%29.aspx要枚举当前进程的所有已加载模块,请使用此处的示例:http://msdn.microsoft.com/en-us/library/ms682621%28v=vs.85%29.aspx http://msdn.microsoft.com/en-us/library/ms682621%28v=vs.85%29.aspx,如果你打电话PrintModules
with GetCurrentProcessId()
,它将枚举所有 HMODULE 句柄(值位于hMods[i]
) 对于当前进程。您可以将它们与 GetProcAddress 一起使用来查找您的函数。
您必须意识到,有可能在不同的 dll 中找到相同名称的函数,大多数情况下您都知道 WinAPI 函数的 dll 名称。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)