我想这将是很酷的几类环绕LoadLibrary和GetProcAddress,Library和Function分别.在我考虑这个问题时,我不确定它是否可行.这是我在想的:
Library 类:
class Library
{
HANDLE m_handle;
public:
// Handles initializing the DLL:
Library(std::string name);
// Deinitializes the DLL
~Library();
HANDLE getHandle();
bool isInitialized();
}
Run Code Online (Sandbox Code Playgroud)
和Function班级:
class Function
{
public:
Function(Library& library, std::string name);
void* call(/* varg arguments? */) throw(exception);
bool isValid();
}
Run Code Online (Sandbox Code Playgroud)
出现问题是因为我必须为参数提供动态数据类型,并将多个长度传递给实际函数指针.我可以通过在构造函数中指定它来获取参数的多个长度,并且具有特定的方法但是数据类型呢?
编辑:我根据任何人在这里使用的答案创建了类:https://github.com/ic3man5/ice--
您可以实现对函数指针的隐式转换.
template <typename Signature>
struct Function
{
Function(Library& library, std::string name)
{
m_func = reinterpret_cast<Signature *>(
::GetProcAddress(library.m_hModule, name.c_str()));
}
operator Signature *() { return m_func; }
private:
Signature * m_func;
};
Run Code Online (Sandbox Code Playgroud)
使用如下类:
Function<int (int, double)> foo(library, "foo");
int i = foo(42, 2.0);
Run Code Online (Sandbox Code Playgroud)