Workaround to "import" an exception from a DLL

Imperatorn johan_forsberg_86 at hotmail.com
Sun Mar 14 15:45:19 UTC 2021


On Sunday, 14 March 2021 at 09:35:40 UTC, frame wrote:
> I know I cannot throw exceptions from a DLL, it will crash. So 
> I currently use a wrapper that collects exceptions and pick it 
> up if the wrapper method returns a failure state.
>
> As I know what type of exception will be thrown, I can copy all 
> important data to a new exception object and then throwing it 
> in the current context.
>
> However, I wonder if there is way to copy the exception data 
> via Throwable interface direct because checking for types via 
> typeid() and string comparsion is just meh.
>
> It is possible to fetch the exception data:
>
> // Throwable e;
>
> // this returns null in the program (but works in a debugger 
> watch):
> MyExceptionObj imported = cast(MyExceptionObj)e;
>
> // this actually works:
> MyExceptionObj imported = cast(MyExceptionObj) cast(void*)e;
>
> But for some reason throwing this object directly would crash.
>
> Is there are way to copy the exception data in a new Throwable 
> instance that can be thrown from the current context? Or can 
> the runtime be tricked by overwriting the TypeInfo in memory? 
> (I don't know exactly what the actual problem is.)

In the past I've used a two patterns which are kinda wierd but. 
Basically the first is returning a tuple w value and exception 
and just check it, kinda like Go. The second approach is to have 
a function u can call to get the latest ex, kinda like 
GetLastError win api


More information about the Digitalmars-d-learn mailing list