_matherr 内置到 DLL 中时不会被调用
我有一个基本的解决方案文件(.sln),我可以在其中重现我最近遇到的问题。
它包含3个项目: 1.) MathTest.lib - 包含可能导致数学错误的方法,如 acos(1.1)。 2.) MathTestDll.dll - 调用上述库中的方法。 3.) UnitTest.exe - 调用 DLL 中应导致错误的导出方法。
我想做的事情相当简单: 以下代码包含 _matherr() 例程,理想情况下应该链接良好。对值为 1.1 的 acos() 的调用无效(无效输入),并应导致错误,该错误应由已实现的 _matherr() 处理程序处理。我希望我对 _matherr() 行为的看法是正确的。请告诉我。 MathTest.lib
#include "MathTest.h"
#include <iostream>
#include <math.h>
int _matherr(_exception* _Except)
{
std::cout << _Except->name;
return -1;
}
void MathTest::ThrowMatherr(float par)
{
float result = acos(par);
std::cout << result;
}
这个“ThrowMatherr()”方法将由 DLL 调用,如下所示: MathTestDll.dll
void MatherrCaller::CauseMatherr()
{
MathTest* mathtest = new MathTest();
mathtest->ThrowMatherr(1.1);
}
然后导出为:
extern "C" __declspec(dllexport) void CallThisToCauseMatherr();
void CallThisToCauseMatherr()
{
MatherrCaller* caller = new MatherrCaller();
caller->CauseMatherr();
}
这个导出的方法将由一个简单的测试调用。 UnitTest.exe
#include <windows.h>
typedef void (*METHODTOCALL)();
int main()
{
HMODULE module = LoadLibrary((LPCSTR)"..\\Debug\\MatherrTestDll.dll");
if(module != NULL)
{
METHODTOCALL ProcAdd = (METHODTOCALL) GetProcAddress(module, (LPCSTR)"CallThisToCauseMatherr");
if (NULL != ProcAdd)
{
(ProcAdd)();
}
FreeLibrary(module);
}
return 0;
}
所有方法都可以正常调用。但是传递了无效输入的 acos() 方法永远不会调用 _matherr() 错误处理程序。请让我知道如何解决这个问题。
我必须详细地提出问题才能表达我的观点。请不要介意。
I have a basic solution file (.sln) where I was able to reproduce a problem I have been facing recently.
It contains 3 projects:
1.) MathTest.lib - containing methods that might cause a mathematical error, like acos(1.1).
2.) MathTestDll.dll - calls the methods from the above lib.
3.) UnitTest.exe - calls the exported method in the DLL that should cause the error.
What I'm trying to do is fairly simple:
The following code contains the _matherr() routine and should ideally link fine. The call to acos() with a value of 1.1 is invalid (invalid input) and should cause an error which should be handled by the implemented _matherr() handler. I hope I'm right about the behavior of _matherr(). Please let me know.
MathTest.lib
#include "MathTest.h"
#include <iostream>
#include <math.h>
int _matherr(_exception* _Except)
{
std::cout << _Except->name;
return -1;
}
void MathTest::ThrowMatherr(float par)
{
float result = acos(par);
std::cout << result;
}
This 'ThrowMatherr()' method will be called by the DLL as follows:
MathTestDll.dll
void MatherrCaller::CauseMatherr()
{
MathTest* mathtest = new MathTest();
mathtest->ThrowMatherr(1.1);
}
which is then exported as:
extern "C" __declspec(dllexport) void CallThisToCauseMatherr();
void CallThisToCauseMatherr()
{
MatherrCaller* caller = new MatherrCaller();
caller->CauseMatherr();
}
This exported method will be called by a simple test.
UnitTest.exe
#include <windows.h>
typedef void (*METHODTOCALL)();
int main()
{
HMODULE module = LoadLibrary((LPCSTR)"..\\Debug\\MatherrTestDll.dll");
if(module != NULL)
{
METHODTOCALL ProcAdd = (METHODTOCALL) GetProcAddress(module, (LPCSTR)"CallThisToCauseMatherr");
if (NULL != ProcAdd)
{
(ProcAdd)();
}
FreeLibrary(module);
}
return 0;
}
All methods get called fine. But the acos() method which has been passed invalid input never calls the _matherr() error handler. Please let me know how I can fix this.
I had to make the question detailed to get my point through. Please don't mind.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
_matherr 的文档中明确提到了这一点:
您需要将覆盖放入 EXE 模块中。更改您的单元测试以适应这一点。
It is explicitly mentioned in the documentation for _matherr:
You'll need to put the override in the EXE module. Alter your unit test to accommodate this.