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
Save to del.icio.us
Save to StumbleUpon
Digg it
Save to Reddit
Share on Facebook
Share on Twitter
More bookmarks
Subscribe!
Auto Save Clipboard Images Redux
Extending SpiderMonkey JavaScript on Windows
Rhino JavaScript to EXE with launch4j
Compiling Rhino JavaScript to Java
Directory Traversal in Rhino JavaScript
A Command-Line MP3 Player for Windows
Auto Save Images from the Clipboard
Java in a Windows EXE with launch4j
An Interview with Tom Zimmer: Forth System Developer
Setting Windows Console Text Colors in C
Invoking the Default Windows Screen-Saver
A Simple Parser for a Small Command Line Interface
Getting the Windows Console Text Color
Screen Capture from Multiple Monitors in Java
MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer