Reputation: 568
I develop a WPF application. This application use external executable managed assemblies (dll or exe) to extend it's opportunity, like plug-ins. I use that external assemblies via reflection:
Dim asm As Assembly = Assembly.LoadFrom("myPlugin.exe")
Dim t As Type = (From tp As Type In asm.GetTypes
Where tp.Name.Contains("Module1")
Select tp).First
Dim entryMethod As MethodInfo = t.GetMethod("EntryMethod")
Dim o As Object = entryMethod.Invoke(Nothing, Nothing)
All of this assemblies have slight functionality and do their work relatively quickly, no problem. But one of this plugins have more continuous work, and I was in need of showing progress of plugin's working. So I've decided print current progress into the console:
Console.WriteLine(progress)
The type of plugin assembly is a console application. This method is invoked directly by my "plugin" executable. When I run it itself - console is visible and progress is printed. But when assembly loaded from WPF app - console is not visible and I cannot see the console output.
I've tried show console window from my WPF or even from plugin assembly using imported function "AllocConsole" from "kernel32.dll":
<DllImport("kernel32.dll")>
Public Function AllocConsole() As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
And later in code, but before first print in console:
AllocConsole()
The console window is visible now, but it's still blank.
So question is: is it possible to output to console from within a .NET class library? And how?
Upvotes: 0
Views: 94
Reputation: 568
Ok, I've finally found the solution.
1) Set WPF project type to "Console WPF application" in the properties of the project.
2) After main window loaded, close console window by calling "FreeConsole()" method of "kernel32.dll" library.
3) Before use of the class library in WPF, call "AllocConsole()" method of "kernel32.dll".
4) When console window is not needed, hide console window by "FreeConsole()".
Steps 3 and 4 are repeats when it need.
And I recommend write somehow helper class, wrapped invokes of "kernel32.dll" functions for work with console window.
Upvotes: 0
Reputation: 11
It depends on why you want to see the console output what the best choice is.
You can 'redirect stdout' in your new process. StreamWriter sw = new StreamWriter(fs); Console.SetOut(
If you can modify the source of the dll you loading, you can refactor the code so it doesn't write directly to the console. That way you can call the code in the console application configuring it to write to the console.
In WPF you can provide a different implementation that writes to a memory stream.
It really depends on why you want to see what's printed to the console.
If it's for debugging the application the original doll should be changed to use a logging framework. (Example log4net) that can already handle.
If your trying to use the console application to do work for you the code you need to call should be changed to be a 'pure function' with no interaction with the console. That way you can use call the function directly from the wpf app. If the source code can't be modified you could create a child process and provide memory streams for 'STDIN' and 'STDOUT' via the process start info struct
Upvotes: 1