我遇到了以下奇怪的代码块。想象一下你有以下 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 )。那么,通过最后一行的函数调用,我们不是打开了可能的堆栈损坏的大门吗?我知道这可以编译,但是如果我们将错误的参数传递给我们指向的函数,会发生什么样的运行时错误?