Jim Lawless' Blog


Getting the Windows Console Text Color

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

Please note! If you're having difficulties compiling the C source code presented below, please see my post: Compiling C from the Command Line with Pelles C

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



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: An Interview with the Author of the French Silk Assembler
Next post:Computers I Have Known


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

Understanding TRS-80 CMD Files

Twimmando No More

My Blog Engine

Changing the C64 Text Color in C

Java in a Windows EXE with launch4j

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

Hiding Batch File Console Windows

We've Moved!

Obfuscated Ruby

Spellbound by Web Programming


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