1

I have an application that behaves as a windows forms and as a console application depending if I pass parameters or not. If I pass parameters it behaves as a console application.

In this last case, as a console application, I attach to the console using:

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

So at the beginning of the app, I call it as below:

AttachConsole(-1);

Later what I do is simply read the string parameter or parameters passed as a parameter in command line, and I return encrypted these strings. After It I wait for user key press using:

Console.ReadKey(true);

After user press any key I want to close the console window.

My problem is that when user press a key, the application does not continue, it still keeps waiting for a key because this key pressed is catched by the another attached console. So in order the application continues user needs to press key twice. On first key press, command prompt is printed again (this comes from the attached console).

How can I solve this?

UPDATE:

static void Main(string[] args)
{            
    if (args.Length == 0)
    {   
        Application.Run(new MyForm());
    }
    else 
    {
        // case args.Length > 0            
        Console.WriteLine("Start my formless app...");
        new FormLessApp().Start(args);
    }
}

ReadKey is performed within FormLessApp:

public class FormLessApp
{
    [DllImport("kernel32.dll")]
    static extern bool AttachConsole(int input);

        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll", SetLastError = true)]
        static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

     public FormLessApp()
     {
         IntPtr ptr = GetForegroundWindow();

         int  u;

         GetWindowThreadProcessId(ptr, out u);

         Process process = Process.GetProcessById(u);

         if (process.ProcessName == "cmd")    
         {
             AttachConsole(process.Id);
             Console.WriteLine("attached to console");
         }
     }

     public void Start(string[] args)
     {

           // Do some things with args
           Console.WriteLine("Press any key to continue...");
           Console.ReadKey(true);
     }
}

To Reproduce:

Open a windows command prompt (cmd) and from there execute it as:

MyApp.exe param1 param2 .....

Willy
  • 9,848
  • 22
  • 141
  • 284
  • You attach more than one console to your application? It seems like, as mentioned on your previous question, that you need multiple applications not one massive one. – TheLethalCoder May 17 '17 at 10:14
  • @TheLethalCoder When I want to start the application in console mode i start it from console and i pass parameters. In this cas, I attach to the same console by doing AttachConsole, then I do Console.ReadKey, but it seems readkey is done in another different console. – Willy May 17 '17 at 10:28
  • So you only have one console window and one winforms window running in the same app? – TheLethalCoder May 17 '17 at 10:30
  • 1
    Should be not hard to attach reproducable example (just the Main function) for clarity. – Evk May 17 '17 at 10:30
  • @TheLethalCoder When started as a console, I only have one console window. See my update. – Willy May 17 '17 at 10:38
  • @Evk see my update – Willy May 17 '17 at 10:38
  • What is `FormLessApp`? – TheLethalCoder May 17 '17 at 10:40
  • It would be better if you provided a [MCVE] instead of partial code. – TheLethalCoder May 17 '17 at 10:40
  • @TheLethalCoder again see my update – Willy May 17 '17 at 10:50
  • If I copy your code and compile to a Console app it works as expected, if I set the output type to a Windows Application however, I get a `InvalidOperationException`. Either way I cannot reproduce this. – TheLethalCoder May 17 '17 at 10:58
  • @TheLethalCoder You must set it as a Windows Forms Application. Then from the console, start it by doing App.exe param1 param2 ..... – Willy May 17 '17 at 11:07
  • @user1624552 That's an important point on how you start it, you should edit that in – TheLethalCoder May 17 '17 at 11:09
  • @user1624552 By doing it that way I get no other console show up, nor does the cmd window app act as the console, is that what you are trying to achieve? – TheLethalCoder May 17 '17 at 11:10
  • @TheLethalCoder Yes, when you execute it as I have commented, you are attached in the same console (titled as cmd), and there messages are output when you do things like Console.WriteLine. No other cmd windows (console) are opened, you keep in the same as a console app, then if you press any key, you should exit from it, but it is not happening. – Willy May 17 '17 at 11:14
  • Okay, when trying to reproduce this I am not getting any messages printed to the cmd.exe. So I think there might be something else going on that could be your problem. – TheLethalCoder May 17 '17 at 11:17
  • 1
    Related: http://stackoverflow.com/questions/493536/can-one-executable-be-both-a-console-and-gui-application – TheLethalCoder May 17 '17 at 11:17

0 Answers0