Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am writing C++ dll which is going to be consumed by 3rd party application. One of it's constructor initializes some data which may cause exception.

My question is, should I throw this exception to calling application for handling and display message? Will it cause any compiler dependency issues across application?

Please suggest best way to handle this scenario.

Gajesh

What I have tried:

I can take initialization part out of constructor in a separate function but this will give addition burden on end user which I want to avoid.
Posted
Updated 25-Feb-16 8:02am

My first rule of using DLLs with C++ is don't. It's generally more pain in the bottom than it's worth.

If I can't follow my first rule my second rule is don't give a DLL a C++ interface, just use a plain C interface instead. This means use opaque pointers, primitive types and POD structures for objects passed across the interface and don't propagate exceptions either.

My third rule if you can't follow the second rule is give clients the source code so they can compile their own version of the code with the compiler they build their applications with. If you do this you can do whatever you want at the interface as you know that however the compiler implements name mangling, exception propagation and the process heap it'll all work.

There's some ways you can use some C++ constructs between DLLs. As an example most compilers for Windows use compatible v-tables (COM wouldn't work if they didn't) so you might be able to pass pointers to interface classes across the interface and expect them to be called properly even if you use different compilers with different methods of name mangling. Whether these techniques are worth doing depends on how much emasculation of the language you're willing to tolerate.
 
Share this answer
 
It is not good. When the DLL may throw execptions, all modules using it must be linked against the same C++ runtime. It may be even necessary to use the same compiler version but I'm not sure about this.

When all functions inside the DLL using the specific constructor return a status, you might omit calling an additional initialisation function by setting an internal error state when the initialisation fails. This will add no additional burden except checking return values which should be done anyway.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900