Subject Exceptions sample again
Author Samofatov, Nickolay
All,

I posted the Delphi sample in previous letter from wrong directory.
Interested reader would notice that calling conventions in Delphi sample
did not correspond with C++ counterparts.
This apparently shouldn't affect test outcome, but here is the sample
again anyways. :-)

Delphi
---------------------
program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;

Type
ECallbackException = class (Exception)
constructor Create(ErrCode : Integer);
end;

TCallback = procedure (ErrCode : Integer); cdecl;

{ ECallbackException }

constructor ECallbackException.Create(ErrCode: Integer);
begin
inherited Create('CallbackError : ' + IntToStr(ErrCode));
end;


procedure callbackRaiseError(ErrCode : Integer); cdecl;
begin
raise ECallbackException.Create(ErrCode);
end;

function ExternalAPI : Integer; cdecl; external
'ExceptTest.dll';
procedure SetCallback(Callback : TCallback); cdecl; external
'ExceptTest.dll';

begin
try
WriteLn('SetCallback');
SetCallback(callbackRaiseError);

Write('Call API...');
WriteLn('return', ExternalAPI);

ReadLn;
except
on E:Exception do
begin
WriteLn;
WriteLn('Exception [', E.ClassName, '] : ', E.Message);

ReadLn;
end;
end
end.
---------------------

MinGW
---------------------
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}


typedef void (*fn_except_callback)(int) ;


fn_except_callback except_callback = 0;

void throwError(int code)
{
if(except_callback)
(*except_callback)(code);
}

class CSimple
{
public:
CSimple()
{
printf("CSimple\n\r");
}

~CSimple()
{
printf("~CSimple\n\r");
}
static void func() {
CSimple a;
throwError(15);
}
};

extern "C" {

void __declspec(dllexport) SetCallback(fn_except_callback callback)
{
except_callback = callback;
}

int __declspec(dllexport) ExternalAPI(void)
{
CSimple::func();

return 0;
}

}
---------------------



Nickolay Samofatov