4

I've made a WPF application that has a GUI but I also want it to optionally run from command line, so I have added this to my App.xaml.cs:

[DllImport("kernel32.dll")]
private static extern bool AttachConsole(int pid);

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    if (e.Args.Count() > 0)
    {
        CreateReport(e.Args);
        Environment.Exit(0);
    }
}

void CreateReport(string[] arguments)
{
    AttachConsole(-1);
    // Parse arguments, generate report, etc.
}

There are two problems with this approach:

Problem 1: Running the application from command-line waits for user input before it closes:

C:\Code>GenerateReport.exe -output Example.html
Started report generation.
Report has been generated.
<-- Console waits until user input here, even though the application has finished doing its business.

Problem 2: These are command line redirection operators. AttachConsole, for some reason, stops them from working:

C:\Code>GenerateReport.exe -output Example.html > Log.txt <-- Causes no output in Log.txt (just an empty file)!

I've checked other Stack questions but none address this issue in particular...so how can you actually attach to the current console Window to output this data back to the user (for example, give them error messages, etc.) but not have these nasty problems come out of it?

Alexandru
  • 12,264
  • 17
  • 113
  • 208
  • 1
    This addresses the issue I believe http://stackoverflow.com/questions/11523595/attachconsole-shows-data-on-pipe-but-the-operator-doesnt-correctly-redirect. I tried it and it seems to work. – dharshana jagoda Sep 08 '14 at 14:57
  • @dharshanajagoda That is a great link, but does not solve Problem 1. Console still waits for user input even with the fix from that answer. The workaround, proposed by the guy, is to then use start /wait...but if you tried doing start /wait together with redirection (start /wait blah > log.txt) it does not work since you're applying redirection on start now! – Alexandru Sep 08 '14 at 15:06
  • I'm not sure what you mean by start/wait. I tried this in two different ways and it works for me. One method I used was to create a different Main entry point and only start the WPF App if the flag -output wasn't found and also tried your method. – dharshana jagoda Sep 08 '14 at 15:15
  • @dharshanajagoda No, it does not work. Do this: Add the code from that answer into your application. Call the Redirect() method from that answer. Run the application: "GenerateReport.exe -output Example.html". You will see the console waiting for user input since we have just started an application. In the answer to that question you've linked, the user who answered clearly stated using start /wait as a workaround to this...for example: "start /wait GenerateReport.exe -output Example.html", but this is no good because you can't do "start /wait GenerateReport.exe -output Example.html > log.txt" – Alexandru Sep 08 '14 at 15:19
  • @dharshanajagoda So, the answer in that question does not solve "Problem 1: Running the application from command-line waits for user input before it closes" from my question. Secondly, it proposes a workaround using start /wait which solves Problem 1. Unfortunately, this workaround fails to solve "Problem 2: AttachConsole, for some reason, stops redirection operators from working" from my question. – Alexandru Sep 08 '14 at 15:24
  • 1
    I managed to reproduce it. My bad. – dharshana jagoda Sep 08 '14 at 15:26
  • @dharshanajagoda Nasty business, eh? – Alexandru Sep 08 '14 at 15:33
  • 1
    yeah totes. That nasty solution with the SendKeys.SendWait("{ENTER}") can't be the only way out. – dharshana jagoda Sep 08 '14 at 15:45
  • @dharshanajagoda Got it working with the SendKeys + the redirect code from the link you gave me! Works quite well like this :) – Alexandru Sep 08 '14 at 15:57
  • @dharshanajagoda You should log this as the answer; if you provide code I can mark it as the accepted solution...using SendKeys works well, and combined with the link you gave me, solves both problems. – Alexandru Sep 08 '14 at 16:00

1 Answers1

2

I found a discussion about this here:

The problem of attachconsole

Seems you are out of luck or if you are not willing to resort to an ugly hack.

From the previous thread's answer: I always have gone with option 3:

"Or you can restructure your application/source a bit and provide two executables, GUI one that starts GUI directly, another that is console executable."

Community
  • 1
  • 1
Nico Heidtke
  • 525
  • 4
  • 7
  • There must be another way, for example, there must be a way to get the current console window that my application is hooked in, and when I finish running my application, there must be a way to post an enter key to the console and tell it to exit. – Alexandru Sep 08 '14 at 15:42
  • Actually. got it working with the SendKeys from that link, and also with the redirection logic from a link that dharshana posted. – Alexandru Sep 08 '14 at 16:05