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



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.

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


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

Setting Text Color in a Batch File

A Data Manipulation Library for TAP

Along Came AWK

Setting Windows Console Text Colors in C

Auto Save Clipboard Images Redux

Obfuscated C

Extending SpiderMonkey JavaScript on Windows

Compiling Rhino JavaScript to Java

Embedding JavaScript in a Batch File

A Simple Media Control Interface Script Processor


Full List of Posts

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


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service