Originally published on: Tue, 03 Nov 2009 04:42:21 +0000
( Please note:The good netizens of the alt.comp.msdos.batch.nt Usenet newsgroup pointed out a few things relating to this post.
A new API function is available for Windows 2000 and up called GetConsoleWindow() that alleviates some of the caption-changing operations that I perform in the code below.
There are other utilities which can perform the "hide" operation in addition to many other helpful windows operations. One such utility is "cmdow" available here: http://commandline.co.uk/cmdow/
I used to sell a command-line utility for Windows called RUN-and-HIDE that would launch a batch file in a hidden window. While a number of other scripts became available to do the same thing, I noted that I hadn't seen any that would hide the window of an already-running console process from within that process's executing batch file script.
I performed a cursory search this evening to see if a utility was to be found that could hide a running console process. Not only was I not able to find one, but many posts state firmly that unless you launch the console process, one absolutely can not hide it.
...so, let me show you how... ;-)
Most windows are easy enough to hide via the Windows API ShowWindow() function. You pass ShowWindow() a window-handle and a show-mode and the window may display differently. One option for the mode argument allows the window to be hidden. ( We could just as easily minimize the window, maximize it, move it, ...etc. if we have the window-handle ).
The trick to obtaining the console window-handle is to set the caption to a unique string, then call the Windows FindWindow() function. We need a unique title because FindWindow() will yield the handle for the first window it finds that matches our criteria.
My code will call getpid() to obtain the current process ID for the running utilty and will use that value with a prefix of HIDECMD_ in the running EXE.
Here is the code:
hidecmd.c
To use hidecmd, place a call to it early in your batch file. You may see the window blink as it initially displays, then disappears.
hidedemo.bat
I recommend use of the exit verb to ensure that the batch script terminates.
The source and EXE files for hidecmd can be found here.
http://www.mailsend-online.com/wp/hidecmd.zip
Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless
Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

Save to StumbleUpon
Digg it
Save to Reddit
Share on Facebook
Share on Twitter
More bookmarks
Click **here**
A JavaScript REPL for Android Devices
A Review of Kevin Mitnick's Book Ghost in the Wires
Play MP3 Files with Python on Windows
Setting Text Color in a Batch File
A Data Manipulation Library for TAP
Setting Windows Console Text Colors in C
Auto Save Clipboard Images Redux
Extending SpiderMonkey JavaScript on Windows
Compiling Rhino JavaScript to Java
Embedding JavaScript in a Batch File
A Simple Media Control Interface Script Processor
Why Some Web Sites will go Dark on Jan 18th
Book Review : Paull Allen - Idea Man
A 90's Experiment in Online Systems - The U.S. West CommunityLink Service