Jim Lawless' Blog


Getting the Windows Console Text Color

Originally published on: Wed, 19 May 2010 00:45:10 +0000

From the comments section of my post Setting Text Color in a Batch File:

Agreed. This is a great little program. However, I would love a corresponding 'getcolor.exe' program to return the current colors.

The problem is my scripts are run by different users who may not be running with the default background and foreground colors. Plus, if I had a 'getcolor' ability, I could do reverse video and bold and they would work regardless of the current colors.

I could hopefully grab your source code and do this myself but if you wanted to take a stab at it first, I'd be happy to have you do it :-)

I didn't initially have much of a narrative for this one, but I ran into interesting opportunities when trying to run the code.

The utility is called getcolor You must supply at least one command-line parameter. If the parameter is -f, the foreground color attribute will be written to the standard output device in hexadecimal notation.

If the parameter is -b, the background color attribute will be written to stdout in hex.

You may supply a second parameter that should represent an output text filename where the attribute will alternately be written. Initially, I had tried redirecting the output to a file, but the attributes returned were then no longer reflecting the current console buffer, since stdout was being redirected. I saw some weird results.

Here's a batch file that demonstrates how to obtain the foreground and background colors in two separate environmental variables.


@echo off
getcolor -b tmp.txt
set /P back=<tmp.txt
getcolor -f tmp.txt
set /P fore=<tmp.txt
echo The current console color is %back%%fore%

getcolor.c


// Get the output color attribute for the current console window
//
// License: MIT / X11
// Copyright (c) 2010 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 <string.h>

void syntax();
void *getConsoleFunction(char *name);

BOOL (WINAPI *doGetConsoleScreenBufferInfo)(HANDLE hConsoleOutput,
   CONSOLE_SCREEN_BUFFER_INFO *inf);


int main(int argc,char **argv) {
   HANDLE hCon;
   WORD color;
   CONSOLE_SCREEN_BUFFER_INFO csbi;
   FILE *fout;
   
   if(argc<2) {
      syntax();
      return 1;
   }
   
   hCon=GetStdHandle(STD_OUTPUT_HANDLE);
   
   doGetConsoleScreenBufferInfo=getConsoleFunction("GetConsoleScreenBufferInfo");

   if(doGetConsoleScreenBufferInfo==NULL) {
      fprintf(stderr,"Sorry! get is incompatible with this version of Windows.");
      return 1;
   }
   if(argc==3) {
      fout=fopen(argv[2],"w");
      if(fout==NULL) {
         fprintf(stderr,"Cannot open output file %s\n",argv[2]);
         return 1;
      }
   }
   else {
      fout=stdout;
   }

   (*doGetConsoleScreenBufferInfo)(hCon,&amp;csbi);
   color=csbi.wAttributes;
   if(!stricmp(argv[1],"-b")) {
      fprintf(fout,"%x",color>>4);
   }
   else
   if(!stricmp(argv[1],"-f")) {
      fprintf(fout,"%x",color&amp;0x0f);
   }
   else {
      syntax();
      if(fout!=stdout)
         fclose(fout);
      return 1;
   }
   if(fout!=stdout)
         fclose(fout);
   return 0;
}

void syntax() {
   fprintf(stderr,"getcolor - Get the current console color attribute - by Jim Lawless\n\n");
   fprintf(stderr,"Syntax:\n\tgetcolor -f [optional output filename]\n");
   fprintf(stderr,"Write foreground color digit\n\nor\n\n");
   fprintf(stderr,"\tgetcolor-b [optional output filename]\n");
   fprintf(stderr,"Write background color digit\n\n");
   fprintf(stderr,"See:\nhttp://www.mailsend-online.com/blog?p=94\nfor usage instructions and C source code.\n");
}

void *getConsoleFunction(char *name) {
   static HMODULE kernel32=(HMODULE)0xffffffff;
   if(kernel32==0)
      return NULL;
   if(kernel32==(HMODULE)0xffffffff) {
      kernel32=LoadLibrary("kernel32.dll");
      if(kernel32==0)
         return 0;
   }
   return GetProcAddress(kernel32,name);
}

The source code and executable file for getcolor can be downloaded from a single archive at: http://www.mailsend-online.com/wp/getcolor.zip

Unless otherwise noted, all code and text entries are Copyright ©2010 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: An Interview with the Author of the French Silk Assembler
Next post:Computers I Have Known


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

Internet Protocols and Rhino JavaScript

Hiding Batch File Console Windows

Extracting URL Addresses from Text in C

Shrouding CSharp and Java Source Code with AWK

Obfuscated Ruby

A Quine in C

A Simple Media Control Interface Script Processor

Jim Butterfield : The Commodore Guru

Removing IE Popups in C

Directory Traversal in Rhino JavaScript


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