You certainly can. First, let me you assume you have source codes of all both projects, then you can freely merge them. I would advice you not to mess with console. Instead, simulate console with your .NET application using some UI control. I usually use
ListBox
(one item per text line) which is better than
TextBox
, but there are many other options; you can also add many useful features (search in output, save it, etc.)
You can turn your Win32 C++ application into C++/CLI class library, which will require only minimal modification. If you really use anything which require extensive use of native environment/libraries, make if a mixed-mode C++/CLI + unmanaged C++ project. Turn
main
function of your application into a C++/CLI (ref) class. Now, abstract out all methods writing to the console. Make them abstract virtual methods with exact same signature and make this class public and abstract.
This managed or mixed-mode DLL can be referenced by your .NET application as any other .NET assembly. Now, in this .NET application, subclass the class mentioned in the above paragraph and override those output method to write strings into your control mentioned on first paragraph.
This is the simplest and less intrusive method I can think off. You don't need to change any logic of your existing Win32 application but can easily add any features.
Now, in case you don't have the source code of this Win32 application, this is much worse as you are dealing with a separate process and you don't have a way to support the solution. This can be resolved pretty easily by running this application as a child process using
System.Diagnostics.Process.Start
,
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[
^]. You would also need to redirect the console output of the child process, read the redirected stream(s)
StandardOutput
and
StandardError
and use the content of these streams in you UI the way you want. You will find the redirection code sample here:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[
^].
Please, pay attention that I had to give you too different variants of the answer because you haven't been comprehensive in formulation of your question. Providing variants is somewhat unpleasant, because I know you would be using only one of them, so it's not very satisfying to do a double job. So please, next time try to make sure you are 100% certain when you formulate a question — thank you.
—SA