Article ID: 122303
Article Last Modified on 11/13/2001
/* dllsamp.cpp : Defines the implementation routines for the DLL
* Compile options needed: /GX dllsamp.cpp /link /DLL
*/
#include <iostream.h>
// ExceptionI is declared with _declspec(dllimport), and will not be
// caught correctly
class __declspec(dllexport) ExceptionI {
int code;
public:
ExceptionI();
ExceptionI(int i);
~ExceptionI() {};
int GetCode(void);
};
ExceptionI::ExceptionI (void)
{ code = 0; }
ExceptionI::ExceptionI (int i)
{ code = i; }
int ExceptionI::GetCode(void)
{ return code; }
// ExceptionII is derived from ExceptionI, and will be caught correctly
class ExceptionII : public ExceptionI {};
__declspec(dllexport) void dllfunc(void (*exefunc)(void))
{
try
{
cout << "In the DLL." <<endl;
exefunc();
}
catch (ExceptionI x)
{
cout << "DLL: Class ExceptionI caught: code = " << x.GetCode() <<
"." << endl;
cout << "Throwing ExceptionII..." << endl;
throw ExceptionII();
}
catch (...)
{
cout << "DLL: ExceptionI was not caught as correct type." << endl;
cout << "Throwing ExceptionII..." << endl;
throw ExceptionII();
}
}
/* exesamp.cpp : Defines the implementation routines for the EXE
* Compile options needed: /GX exesamp.cpp /link dllsamp.lib
*/
#include <iostream.h>
__declspec(dllimport) void dllfunc(void (*exefunc)(void));
class __declspec(dllimport) ExceptionI {
int code;
public:
ExceptionI();
ExceptionI(int i);
~ExceptionI() {};
int GetCode(void);
};
// ExceptionII needs to be defined in both modules to be caught correctly
class ExceptionII : public ExceptionI {};
void exefunc(void);
void main(void)
{
try
{
cout << "In the EXE." << endl;
dllfunc(exefunc);
}
catch (ExceptionII x)
{
cout << "EXE: Class ExceptionII caught: code = " << x.GetCode() <<
"." << endl;
}
catch (...)
{
cout << "EXE: ExceptionII was not caught as correct type." << endl;
}
}
void exefunc(void)
{
try
{
cout << "In the EXE again." << endl;
cout << "Throwing ExceptionI..." << endl;
throw ExceptionI(-1);
}
catch (ExceptionI x)
{
cout << "EXE: Class ExceptionI caught: code = " << x.GetCode() <<
"." << endl;
cout << "Throwing ExceptionI..." << endl;
throw ExceptionI(-1);
}
}
Additional query words: 2.00 9.00 gpf gpfault buglist2.00 compiler
Keywords: kbbug kbfix kbcpponly kbcompiler KB122303