我正在开发一个 C++ 控制台应用程序来动态加载 DLL。
应用程序可以成功调用 DLL 的函数之一。但是,在执行结束时,会抛出损坏的堆异常。具体值为:
Stack cookie instrumentation code detected a stack-based buffer overrun.
我想知道为什么...这里是代码。
#include <iostream>
#include <string>
#include "windows.h"
using namespace std;
typedef DOUBLE(CALLBACK* DllFunc)(DOUBLE, DOUBLE);
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE hDLL; // Handle to DLL
DllFunc dllFunc1;
DOUBLE p1 = 2.0, p2 = 4.0, r;
wstring dllName;
string functionName;
cout << "Insert the dll name: " << endl;
getline(wcin, dllName);
cout << "Insert the function name:" << endl;
getline(cin, functionName);
cout << "Insert the first value: " << endl;
cin >> p1;
cout << "Insert the second value" << endl;
cin >> p2;
hDLL = LoadLibrary(dllName.c_str());
if (hDLL != NULL)
{
cout << "DLL loaded: " << hDLL << endl;
functionName = "?" + functionName + "@MyMathFuncs@MathFuncs@@SANNN@Z";
dllFunc1 = (DllFunc)GetProcAddress(hDLL, functionName.c_str());
if (!dllFunc1)
{
// handle the error
FreeLibrary(hDLL);
cout << "Function not found!" << endl;
}
else
{
// call the function
r = dllFunc1(p1, p2);
cout << "The result is: " << r << endl;
FreeLibrary(hDLL);
}
}
else {
cout << "Dll not found" << endl;
}
cout << "Press any key to exit." << endl;
int i;
cin >> i;
return 0;
}
我不知道问题出在哪里:如果正确加载了库,我将其释放;没有指针,我没有使用任何缓冲区...
任何想法?仅当执行到达最后一个闭合的花括号时才会发生异常...