mjgardne
asked on
How to map/marshall native exceptions into managed exceptions?
Hello,
I am creating a DLL that contains a native "C++" class "CR", and a managed wrapper class "CM" that wraps around CR via a pointer.
CR defines and uses "C++" native exceptions.
In CM, I have created managed versions of the unmanaged exceptions in CR.
The methods in CM that invoke, via CR's pointer, methods in the unmanaged code of CR have try...catch...finally. For each possible native exception that could be thrown through from the unmanaged method, I have placed a catch. Inside the catch, I simply throw the managed version of the native exception. Basically, this is how I am providing the mapping from unmanaged to managed exceptions.
If I do not catch an unmanaged exception, I expect that an SEH exception will be generated.
From what I have read, this process should work, but whenever I force my native code to throw an unmanaged exception, what I catch in my CM wrapper class is an SEH exception. Arggg... So much for me understanding things! :P
So, I am hoping that someone can tell me the best practices way to marshall exceptions between managed and unmanaged code... Must I replace my native "C++" code with SEH (hope not)? Is there an interop method that provides mapping that I must override? Should I just define managed exceptions in my unmanaged code (i would prefer to keep the worlds separate, if possible)?
I would sincerely appreciate you help with these questions...
Thanks,
Mike
I am creating a DLL that contains a native "C++" class "CR", and a managed wrapper class "CM" that wraps around CR via a pointer.
CR defines and uses "C++" native exceptions.
In CM, I have created managed versions of the unmanaged exceptions in CR.
The methods in CM that invoke, via CR's pointer, methods in the unmanaged code of CR have try...catch...finally. For each possible native exception that could be thrown through from the unmanaged method, I have placed a catch. Inside the catch, I simply throw the managed version of the native exception. Basically, this is how I am providing the mapping from unmanaged to managed exceptions.
If I do not catch an unmanaged exception, I expect that an SEH exception will be generated.
From what I have read, this process should work, but whenever I force my native code to throw an unmanaged exception, what I catch in my CM wrapper class is an SEH exception. Arggg... So much for me understanding things! :P
So, I am hoping that someone can tell me the best practices way to marshall exceptions between managed and unmanaged code... Must I replace my native "C++" code with SEH (hope not)? Is there an interop method that provides mapping that I must override? Should I just define managed exceptions in my unmanaged code (i would prefer to keep the worlds separate, if possible)?
I would sincerely appreciate you help with these questions...
Thanks,
Mike
Is the behavior different if you catch an exception of type System.Runtime.InteropServ ices.SEHEx ception http://msdn.microsoft.com/en-us/library/aa330529(VS.71).aspx in the managed code?
Here is also one article that describes using vectored exception handling to convert Win32 and C++ exceptions to managed exceptions: http://blogs.microsoft.co.il/blogs/sasha/archive/2008/08/20/converting-win32-and-c-exceptions-to-managed-exceptions.aspx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks,
Mike
Open in new window