Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an application whose UI is in C#, WPF and this UI is using c++ DLL for performing operations.
I am calling the c++ DLL methods from my UI in this way and it is working fine.
If C++ DLL will get some exception or error during execution then how can I pass the error with some integer value and string back to my c# exe, so that c# exe will update the UI as per the error and show the readable message to the user.

What I have tried:

I am thinking of using callbacks because an error can come at any time or from any method of DLL.
step1: register callback method of c# in c++
step2: Whenever error or exception came in DLL call this registered callback method of c# from c++ DLL.
step3: Run this callback method for infinite time in c# exe and whenever error comes from c++ DLL identify the error through error code and take corresponding actions.
I am not sure this approach is correct or not, if it's correct then how to implement it.
Posted
Updated 28-Jul-20 5:46am

Running an infinite loop is bad style. You should handle the error and than move by finding a way out.

Ensure that the C++ error are handled in C++ completly and you only inform C# about the occured issue.

Read my article about Callbacks between C++ and C#.
 
Share this answer
 
Comments
Member 14686752 28-Jul-20 4:55am    
Thanks for your response. Yeah, I used delegates for avoiding infinite loop. Your article gave me a good reference for resolving my issue.
Generally any error code generated by an API call can be queried with GetLastError() function and FormatMessage() can give textual representation of that error using FORMAT_MESSAGE_FROM_SYSTEM flag. Both of which you can call from the C# app
 
Share this answer
 
v2

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