我正在开发一个动态加载动态链接库的C++控制台应用程序。
应用程序可以成功地调用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;
}我不知道问题出在哪里:如果库被正确加载,我就释放它;没有指针,也没有使用任何缓冲区……
有什么想法吗?只有当执行到达最后一个关闭的大括号时,才会发生异常...
发布于 2014-10-15 20:24:54
这看起来更像是堆栈问题(可能会影响错误报告)。您的调用约定正确吗?当装饰似乎指示__cdecl时,回调可能是__stdcall
https://stackoverflow.com/questions/26379942
复制相似问题