Jim Lawless' Blog


Hiding Batch File Console Windows

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


// hidecmd.c
// Hide the current console window for a running batch
// script.
//
// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include <windows.h>
#include <stdio.h>
#include <process.h>
#pragma comment(lib,"user32.lib")
int main(int argc,char **argv) {
   int pid;
   char wrk[40];
   char old[256];
   HWND h;
      // get the process ID to use as a unique
      // number
   pid=getpid();
   sprintf(wrk,"HIDECMD_%d",pid);
      // preserve the old console window title
   GetConsoleTitle(old,sizeof(old));
      // replace it with a unique titles
   SetConsoleTitle(wrk);
      // give the system time to change the title
   Sleep(40);
   
      //.now, find the window handle by title
   h=FindWindow(NULL,wrk);
      // hide the window
   ShowWindow(h,SW_HIDE);
      // replace the old title
   SetConsoleTitle(old);
      // wait just a bit for the update again
   Sleep(40);
}

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


hidecmd
dir %windir%
exit

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

del_icio_us Save to del.icio.us
stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks



Previous post: My Foray into Shareware
Next post:My Big Shareware Splash


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

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

Taking Shape

We've Moved!


Popular Posts

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


Random Posts

Invoking the Default Windows Screen-Saver

A Simple Parser for a Small Command Line Interface

Along Came AWK

E-mail cleansing

Generating Primes with XSLT

WSH2EXE part 1

Cheating the LZW

Getting the Windows Console Text Color

Obfuscated C

Screen Capture from Multiple Monitors in Java


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer