【发布时间】:2011-01-23 17:22:31
【问题描述】:
我遇到了以下奇怪的代码块。假设您有以下 typedef:
typedef int (*MyFunctionPointer)(int param_1, int param_2);
然后,在一个函数中,我们尝试通过以下方式从 DLL 中运行一个函数:
LPCWSTR DllFileName; //Path to the dll stored here
LPCSTR _FunctionName; // (mangled) name of the function I want to test
MyFunctionPointer functionPointer;
HINSTANCE hInstLibrary = LoadLibrary( DllFileName );
FARPROC functionAddress = GetProcAddress( hInstLibrary, _FunctionName );
functionPointer = (MyFunctionPointer) functionAddress;
//The values are arbitrary
int a = 5;
int b = 10;
int result = 0;
result = functionPointer( a, b ); //Possible error?
问题是,我们无法知道我们通过 LoadLibrary 获得地址的函数是否接受两个整数参数。dll 名称由用户在运行时提供,然后列出导出函数的名称并且用户选择一个进行测试(同样,在运行时 :S:S )。 那么,通过最后一行的函数调用,我们不是打开了可能的堆栈损坏的大门吗?我知道这可以编译,但是如果我们将错误的参数传递给我们指向的函数,会发生什么样的运行时错误?
【问题讨论】:
-
如果您不确定 dll 导出了哪些确切的函数(确切的类型,但不是调用约定),请使用 Dependency Walker。
标签: c++ dll function-pointers loadlibrary