From: Martin Irman on
Hi guys:

We have a console application, which needs to hide the console window
at its startup. This is simple to do:

hWndCons = GetConsoleWindow();
ShowWindow(hWndCons, SW_HIDE);

Now, the first time you start our application after log-on on Windows
Vista, it does not work, the console is not hidden. When you start the
application again - everying is okay, console is hidden. The failure
to hide the console happens only the first time you start our
application after logon. The problem does not appear on Windows XP.

I was wondering if anybody experienced a similar problem?

Thanks,
Martin.


From: Pops on
Martin Irman wrote:
> Hi guys:
>
> We have a console application, which needs to hide the console window at
> its startup. This is simple to do:
>
> hWndCons = GetConsoleWindow();
> ShowWindow(hWndCons, SW_HIDE);
>
> Now, the first time you start our application after log-on on Windows
> Vista, it does not work, the console is not hidden. When you start the
> application again - everying is okay, console is hidden. The failure to
> hide the console happens only the first time you start our application
> after logon. The problem does not appear on Windows XP.
>
> I was wondering if anybody experienced a similar problem?

I am willing to bet that it doesn't actually work after the login - its
just an illusion, but it is more apparent at login and this could be a
speed or contention issue (the OS is doing alot of things for you at
login), that you don't have normally when you run it after the login.

I say that because the system will use a CreateProcess() call to start
your console application and this will automatically allocate a console.
In order to have it not allocate a console, the CreateProcess() needs
to use the DETACHED_PROCESS attribute.

So it is really an "illusion" you are seeing. Its working very fast for
the OS creating the console, load and start your application, and your
application does is hide itself. So it appears like it never created
the console, but it actually did.

Do this, add a small delay right before the hide, like so:

int main(char argc, char *argv[])
{
HWND hWndCons = GetConsoleWindow();
Sleep(1000);
ShowWindow(hWndCons, SW_HIDE);
return 1;
}

and you will see the console. In fact, even without the delay, you can
probably see it if you run it from the deskttop

Anyway, to 100% hide it, you need to have the OS or some other loader
start the console application as a DETACHED_PROCESS.

One way to do this programmically is to compile your application without
a console, i.e, link it as a GUI but without a window :-) The only
thing that will change is your entry point.

#include <windows.h>

int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
// AllocConsole(); <--- Allocate console if you want one

.. do your cnsole thing...

return 0;
}

--
HLS
From: Remy Lebeau on

"Martin Irman" <imartin(a)inro.ca> wrote in message
news:ecSHldvKIHA.5160(a)TK2MSFTNGP05.phx.gbl...

> We have a console application, which needs to hide the console window at
> its startup.

Console apps should not do that themselves. The caller who invoked the
console should be the one to hide/show it as needed.

> Now, the first time you start our application after log-on on Windows
> Vista, it does not work, the console is not hidden.

You are not doing any error handling. Did you verify that
GetConsoleWindow() is not returning NULL? What is the return value of
ShowWindow()?


Gambit


From: Martin Irman on
Pops wrote:
> Martin Irman wrote:
>> Hi guys:
>>
>> We have a console application, which needs to hide the console window
>> at its startup. This is simple to do:
>>
>> hWndCons = GetConsoleWindow();
>> ShowWindow(hWndCons, SW_HIDE);
>>
>> Now, the first time you start our application after log-on on Windows
>> Vista, it does not work, the console is not hidden. When you start the
>> application again - everying is okay, console is hidden. The failure
>> to hide the console happens only the first time you start our
>> application after logon. The problem does not appear on Windows XP.
>>
>> I was wondering if anybody experienced a similar problem?
>
> I am willing to bet that it doesn't actually work after the login - its
> just an illusion, but it is more apparent at login and this could be a
> speed or contention issue (the OS is doing alot of things for you at
> login), that you don't have normally when you run it after the login.

The problem can be reproduced also if I wait for a substantial period
of time before starting our application.

>
> I say that because the system will use a CreateProcess() call to start
> your console application and this will automatically allocate a console.
> In order to have it not allocate a console, the CreateProcess() needs
> to use the DETACHED_PROCESS attribute.
>
> So it is really an "illusion" you are seeing. Its working very fast for
> the OS creating the console, load and start your application, and your
> application does is hide itself. So it appears like it never created
> the console, but it actually did.

Actually, you can always see the console window appearing for a short
moment. That's okay. The problem is that the first time after login
the console window stays ... indefinitely.

>
> Do this, add a small delay right before the hide, like so:
>
> int main(char argc, char *argv[])
> {
> HWND hWndCons = GetConsoleWindow();
> Sleep(1000);
> ShowWindow(hWndCons, SW_HIDE);
> return 1;
> }
>
> and you will see the console. In fact, even without the delay, you can
> probably see it if you run it from the deskttop

Yes, I can see the console window even without the delay.

>
> Anyway, to 100% hide it, you need to have the OS or some other loader
> start the console application as a DETACHED_PROCESS.
>
> One way to do this programmically is to compile your application without
> a console, i.e, link it as a GUI but without a window :-) The only
> thing that will change is your entry point.

Unfortunately, the application has to be able to run in a legacy
command line mode. Similarly to MS Visual Studio which is able to run
with a GUI but also as a command line utility (they solve it by having
to executables ... devenv.com (command-line) and devenv.exe (GUI) ...
we didn't opt for this solution).

>
> #include <windows.h>
>
> int WINAPI WinMain (HINSTANCE hThisInstance,
> HINSTANCE hPrevInstance,
> LPSTR lpszArgument,
> int nFunsterStil)
> {
> // AllocConsole(); <--- Allocate console if you want one

I have to use an already existing console if the application was
started from the command line ... hmm ... I wonder if AttachConsole
could work (but that's a major change in the setup of the application,
I'll try to find a simpler workaround first).

>
> .. do your cnsole thing...
>
> return 0;
> }
>

Thanks for the answer.
Martin.
From: Martin Irman on
Remy Lebeau wrote:
> "Martin Irman" <imartin(a)inro.ca> wrote in message
> news:ecSHldvKIHA.5160(a)TK2MSFTNGP05.phx.gbl...
>
>> We have a console application, which needs to hide the console window at
>> its startup.
>
> Console apps should not do that themselves. The caller who invoked the
> console should be the one to hide/show it as needed.

Yep, right. Unfortunatelly, our app has to work as a GUI application
(while hiding the console) but also in a legacy command line mode.

>
>> Now, the first time you start our application after log-on on Windows
>> Vista, it does not work, the console is not hidden.
>
> You are not doing any error handling. Did you verify that
> GetConsoleWindow() is not returning NULL?What is the return value of
> ShowWindow()?

Yep, GetConsoleWindow() is not returning NULL. Curiously, in the
failing case, ShowWindow() returns 0 meaning that the console window
was hidden before the ShowWindow() call. It seems that the OS does not
necessarily show the console before starting my application. The
command to show the console comes later ... and it can come before my
ShowWindow(SW_HIDE) call or after.

Thanks.