I would like to know how to do this? For example, if exception occurs in C++ main application, how do send something to the GUI application? Any simple example would be appreciated.
-
1well, it depends how you want to handle this. you can have a try catch and in the catch you return an error code known also from the C# application, if you are in full control of both applications. – Davide Piras Dec 13 '11 at 17:23
-
1Does the GUI application start the C++ application? – hmjd Dec 13 '11 at 17:24
-
Yes the GUI is the front end and pass the arguments to C++ main application – taker Dec 14 '11 at 00:15
4 Answers
If the C# GUI app starts the C++ console app, then the exit code of the latter (the return value of the main()
function) can be accessed by the former, see the example here: Process.ExitCode. The general solution is to use some form of InterProcess Communication (IPC). Windows examples: here and here.
Assuming the two applications in C++ & C# are different processes, you cannot do that.
(but you could return an error code -between 0 and 255- with exit
in the child process and get it with waitpid
in the parent process).
However (at least on Linux) you could have a textual bi-directionnal channel (on Linux, with named or anonymous pipes, or AF_UNIX sockets) and define a protocol between the two processes.

- 223,805
- 18
- 296
- 547
-
-1 for "cannot do that" -- the C++ app most assuredly *could* send information about the error to the C# app. If the C# process launched the C++ one, it could easily capture its stderr, or use pipes for communication. If the processes were started independently, they could use named pipes, or sockets, or shared memory. – Joe White Dec 13 '11 at 17:42
-
I disagree, the C++ app cannot do that *directly*; it has (as you and I are suggesting) to define some application specific protocol. There is no direct function call to transmit an exception from a C++ process to a C# one (at least in the Linux world, where C# is Mono) ... – Basile Starynkevitch Dec 13 '11 at 17:44
-
OP didn't ask for something fancy like re-throwing the C++ exception as a CLR exception. The question was "how do send **something** to the GUI application" (emphasis mine). Your answer of "cannot do that" is ridiculous -- sending *something* back is as simple as writing the exception to stderr. – Joe White Dec 13 '11 at 17:54
-
It's not very clear for me if Exception happened in your c++ application your application suppose to exit or not? if your application suppose to exit when error hapend:
- in your C++ code when error happend use "exit(errorCode)" that errorCode is an integer number
- in your C# application for process that exwcute your c++ exe, register handler for the Exited event (make sure you set EnableRaisingEvents on the process to true).
- in your event handler read "ExitCode" of the process object that run your c++ exe.
but if in your c++ application you handle the error and just want to notify the GUI, One solution is :
- in c++ application when error happend write the error in one file
- in C# application we create one thread that read this file and when the content of the file is changed,send one event to main thread.
Note: for communication between two proccess ther is more than one way may be this text help you :http://en.wikipedia.org/wiki/Inter-process_communication

- 435
- 2
- 10
One Way is to use a WCF service. I recently wrote a C# GUI on one machine that needed to communicate with a C++/CLI app on a different machine and I just exchanged information (including error messages) through WCF, it would work equally well running on a single machine. Plus this approach gives you an excuse to learn WCF.

- 8,192
- 5
- 52
- 93