Click here to Skip to main content
15,396,239 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm creating a utility that will be started from another application that will supply command line arguments but when it's activated a console window appears and the best I've been able to do is to get it to flash briefly using interop services.

I need for the application to display a form when it's activated but not the console window. I know there has to be a way to do this!
I also need for the activated utility application to return a value depending on user input.

Below is the code that I was getting to work but now it doesn't?, show the console window and it stays up was coming up briefly then disappearing?

static extern IntPtr GetConsoleWindow();

static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

const int SW_HIDE = 0;
const int SW_SHOW = 5;

public Form1(string[] args)

    // Hide
    var handle = GetConsoleWindow();
    ShowWindow(handle, SW_HIDE);


    string html = "<center><h3>Testing this out</h3></center>";
    string htm = "<h3 style=\"text-align: center\">WTF Happened?</h3>";

    webBrowser1.DocumentText = html + htm; ;

Any help much appreciated!

Where is your logic? If you don't want the console, why using GetConsoleWindow? If you are using Forms, why using ShowWindow? You should not use P/Invoke at all, use just pure .NET FCL, otherwise you will compromise your platform compatibility, not getting anything at all in return.

Command-line parameters have nothing to do with console. Perhaps your problem was not understanding this very obvious fact. To work with command-line parameters in the application receiving them, please see my article: Enumeration-based Command Line Utility[^].

You don't have to use my utility or any of my code, you can just learn how to get the command-line parameters passed, through entry point (Main) and System.Environment.

Mike Hankey 13-Mar-15 23:47pm
Ok I don't think you understood my problem or maybe, as usual I didn't explain it simply enough...I get frustrated and lose my ability to explain myself.
From Acronis I fill in the file URL of my application and click a button to start my application. My application starts up and checks if there is a file name in the command line args, other than that there is no other need for these args so forget them.
Now when my application starts up I open a windows form in the lower right corner of the screen with a couple of button to either cancel the current operation or accept it or cancel and this terminates my application with either a Environment.Exit 0 for success or non zero for abort.
Now the crux of the problem my application works perfectly except along with the windows form opening in the lower right hand corner of the screen a console window also opens elsewhere on the screen. I did not create, spawn, pray for, receive as a gift or otherwise ask for this console window so how do I get it not to come up or be created.
If you need further explanation let me know.
What's in the project Properties "Output Type"? ("Console Application", "Windows Application")?
Is that the console of the same process?
The "Output type" project option is very confusing, many misunderstood it...

Still, I have don't understand: you have have unwanted console, what makes you using P/Invoking GetConsoleWindow?

While I'm not sure I understand exactly what you are doing, either :), I have had no trouble implementing code like this:

1. in the primary Application:
private string path =
    @"ValidPathToExe of Secondary App";

private void button1_Click(object sender, EventArgs e)
    // must be a valid URI, i.e., starts with: http:// ...
    Process.Start(path, @"");

2. in the secondary Application with a WebBrowser Control:
private void Form1_Load(object sender, EventArgs e)
    string[] args = Environment.GetCommandLineArgs();

    if (args.Length > 0)
        // for testing only
        //foreach (string arg in args) MessageBox.Show(arg);

        if (args.Length > 1)
            Uri uriResult;

            bool result = Uri.TryCreate(args[1], UriKind.Absolute, out uriResult)
                          && (uriResult.Scheme == Uri.UriSchemeHttp
                              || uriResult.Scheme == Uri.UriSchemeHttps);
            if (result)
                MessageBox.Show("invalid url");
        MessageBox.Show("no valid file path received");
I am assuming that you could use standard properties of the 'Process Class to enable receiving notification Events from the secondary Process' Form ... like EnableRaisingEvents, creating a handler for the OutputDataReceived Event, re-directing standard output, etc.

If this response is "way off," just let me know, and I'll delete it.

An interesting asynchronous overlay for working with .NET processes: [^]. Article about development of MedallionShell here: [^].
Mike Hankey 14-Mar-15 3:51am
Ok y'all they say a picture is worth a 1000 words so here's a link to I hope a better understanding of the problem;
BillWoodruff 14-Mar-15 5:16am
I can't see similar behavior in VS 2013 using FrameWork 4.5 on Win 8.1/64: no Console Window "flashes" by.

But, since I can't see your code that sets up the call to Process.Start, I can't go further.

What is the purpose of those 'Dll imports in your example code shown ? And, what's doing with the WebBrowser ?

The thing I had a problem with in the past (never solved) was getting Asynchronous callbacks from the secondary process back to the main process working. But, getting synchronous worked fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900