Console Manipulation without Windows.h

Posted on 2007-10-15
Last Modified: 2008-01-09
Is there any way, without including windows.h, to perform console furctions like SetCursorPosition, WriteCharString, WriteAttributes (color, blinking, etc) and Save what's on the screen to a memory buffer?

I'm taking some OS/2 C code and want to compile it on windows.  In OS/2 we used these Vio* calls (VioWrtCharStr) that don't exist on windows.  I'd like to not include the whole windows.h/platform SDK - I'd prefer to use something closer to ANSI C or maybe some other library.

Any ideas?
Question by:helpatudallshumwaycom
    LVL 86

    Assisted Solution

    If you need a library for that, 'curses' usually is the one of choice and available for a variety of platforms. See ("Public Domain Curses") which works on DOS, OS/2, Win32, X11 and SDL. If you are targetting only Windows, I'd rather go for native Win32 API calls, though.
    LVL 1

    Author Comment

    Is there any way to limit the amount of the Win32 API calls to just the few console functions I need?  I originally turned away from importing Windows.h because of the magnutude of everything it brought with it.  There are some significant naming conflicts that I'd rather avoid.  I agree, it would be nice to just use the Win32 calls, I just don't know how to get *only* the handful of console functions that I want.

    Specifically, I'm looking for replacements for the following:
    VioWrtCharStr(char*,length,row,col,0) - writes a string
    VioWrtNAttr(attribute,count,row,col,0) - writes a given attribute N times
    VioSetCurPos(row,col,0) - sets the cursor position
    VioWrtNChar(char, length,row,col,0) - Writes a single character N times
    VioReadCellStr(start,length, row, col, 0) - Reads the current contents of the screen into a buffer
    VioWrtCellStr(char*, length, row, col, 0) - Writes the contents of a buffer onto the screen
    VioGetCurPos(row,col,0) - Gets the cursor position

    How do I just get the Win32 functions necessary to replace those functions?
    LVL 86

    Accepted Solution

    >>Is there any way to limit the amount of the Win32 API calls

    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>

    removes most of the unnecessary stuff. If you really only need console APIS,

    #include <wincon.h>

    and linking with kernel32.lib should be enough.
    LVL 39

    Assisted Solution

    >>>> How do I just get the Win32 functions necessary to replace those functions?
    Make your own header file where you 'declare' only the functions you need. You should avoid using windows types like UINT, DWORD, LPSTR, BOOL if doing so, but use the original C types (unsigned int, char*, int, ...) instead.

    Regards, Alex
    LVL 1

    Author Comment

    So, assuming I am able to just pull in parts of the platform SDK - what functions / libraries / ??? do I use?  Please forgive me, I'm totally and completely unfamiliar with using the Windows SDK - I've got no idea what the libraries are.  I've done some research on MSDN and it looks like SetConsoleCursorPosition() is one that will work for me, but I haven't been able to figure out how to get it to work. :(

    I certainly appreciate the comments and suggestions that have been offered so far - I'll keep trying to figure out what it takes to appropriately implement them.
    LVL 86

    Assisted Solution

    >>what functions / libraries / ??? do I use?

    As above, 'wincon.h' and kernel32.lib (and the necessary C library headers that you might need. 'wincon.h' will reference other include files that you will need too, but that will be done transparently.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now