Click here to Skip to main content
15,889,216 members
Articles / Programming Languages / C#
Article

ConsoleLibrary, C-Functions for use in C# console applications

Rate me:
Please Sign up or sign in to vote.
4.65/5 (20 votes)
16 May 2004CPOL2 min read 167K   1.2K   40   43
A C++ class library with functions like GetKey(), SetCursor(), Cls() ... for use in C# console applications

Introduction

While programming my first C# console applications I missed functions to read the console-keycode without waiting for the pressed key. As a C++ programmer I searched for functions like _kbhit() or _getch(). But I only found functions like Console.Read() or Console.ReadLine(). Both are still waiting until a key is pressed. But I do not want to wait. I searched on the CODE PROJECT for the keywords (kbhit, getch, GetKey) to get a quickly solution. But unfortunately I get no single hit!!! So I had to solve it on my own. And here is my solution.

The Solution

I build a C++ Class Library with the necessary classes and functions in it. I named it ConsoleLibrary because there are a couple of functions that are useful for an C# console application. After that I created four C# console applications demos that referenced the ConsoleLibrary, to show you how to manage the new functions.

  • ConsoleDemo1
    This is an example for a keyboard-test, using the ConsoleLibrary.
  • ConsoleDemo2
    This is an example for a display-test, using the ConsoleLibrary.
  • ConsoleDemo3
    This is an example for showing an ASCII-table, using the ConsoleLibrary.
  • ConsoleDemo4
    This is an example for programming a game (SNAKE), using the ConsoleLibrary.

Using the code

There are two kinds of using the ConsoleLibrary functions. First of them is to insert the ConsoleLibrary project in your solution file. After that you can make an direct reference in your console application project to the ConsoleLibrary. In this case your are able to debug into the ConsoleLibrary sourcecode, and you can make changes in it.

The second way to use the ConsoleLibrary functions is to make an reference to the ConsoleLibrary.dll. Use this way when you don't want to make changes anyway.

In both cases don't forget the using ConsoleLibrary;

Then you can directly use the functions in your sourcecode. Here are some examples:

To flush the keyboard buffer:

C#
Keyboard.FlushKeys();

To check the keycode in a loop:

C#
do
{
  // get the key...
  key = Keyboard.GetKey();
  // check the key...
  if (key != (int)KeyCode.KeyNone)
  {
    if (key == (int)KeyCode.KeyF1)
      Console.WriteLine("You've pressed F1.");
  }

  // do something else
  ...
} while (key!=(int)KeyCode.KeyEscape);

To set the caption:

C#
Display.SetConsoleTitleA(Copyright);

To clear the screen:

C#
Display.Cls();

To set fore- and background color:

C#
Display.SetForeColor(Color.Yellow);
Display.SetBackColor(Color.Lightred);

To show or hide the cursor

C#
Display.HideCursor();
Display.ShowCursor();

To set the cursor-position

C#
Display.SetCursorPosition(2,10);

To sleep

C#
Process.Sleep(80);

There are also other functions available. Please have a look in the sourcecode of the ConsoleLibrary.

Points of Interest

With this ConsoleLibrary I was able to transfer my first posted article Einstein's riddle solved in C++ from C++ to C#. If you're interested in this C# sourcecode please give me a hint.

I hope my ConsoleLibrary is also useful for you. If so, I'm happy and wish you kind regards...

Manfred...

History

  • 13.05.2004 Submit this article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: how to use C files in C# project ??? Pin
ManiB24-Jan-05 3:01
ManiB24-Jan-05 3:01 
GeneralRe: how to use C files in C# project ??? Pin
Yossi_pRJ24-Jan-05 21:53
Yossi_pRJ24-Jan-05 21:53 
GeneralRe: how to use C files in C# project ??? Pin
ManiB24-Jan-05 22:58
ManiB24-Jan-05 22:58 
GeneralRe: how to use C files in C# project ??? Pin
Yossi_pRJ25-Jan-05 3:01
Yossi_pRJ25-Jan-05 3:01 
GeneralRe: how to use C files in C# project ??? Pin
ManiB25-Jan-05 5:42
ManiB25-Jan-05 5:42 
GeneralRe: how to use C files in C# project ??? Pin
Yossi_pRJ25-Jan-05 6:06
Yossi_pRJ25-Jan-05 6:06 
Questioncan u use c code in c# project? Pin
Yossi_pRJ16-Jan-05 3:35
Yossi_pRJ16-Jan-05 3:35 
AnswerRe: can u use c code in c# project? Pin
ManiB16-Jan-05 23:44
ManiB16-Jan-05 23:44 
of course you can!

Here are the three Steps to build a CLibrary

1.) First you create a new (empty) Projectmap. Name it 'CLibrary'
Now you have an empty Projectmap!

2.) Then you create the project for your C-Code
Rightklick on the empty Projectmap to add a new project.
Choose under Visual C++-Projects the Classlibrary (.NET) and name it 'CLibrary'
Your C-Code will be placed in this project.
You have to add the LIBC.LIB library to this project. Because the standard c functions are stored in it.
Open then project-properties, choose all configurations, then Linker, and add to input: additional dependency: 'LIBC.LIB'
Open the Stdafx.h file and add #include "windows.h"
Open then CLibrary.h file and rename the class Class1 to CLib.
Add all your includes and function to this class.
Build the CLibrary.dll.
<br />
#pragma once<br />
<br />
using namespace System;<br />
<br />
//#using <mscorlib.dll><br />
//#include <conio.h><br />
<br />
<br />
namespace CLibrary<br />
{<br />
	public __gc class CLib<br />
	{<br />
	public:<br />
		// c-Testfunction<br />
		static int SumTest(int a, int b)<br />
		{<br />
			int sum = a + b;<br />
			return sum;<br />
		}<br />
<br />
		// Sleep(int dwMiliseconds) function suspends the execution of the current thread for a specified interval. <br />
		static void Sleep(int dwMiliseconds)<br />
		{<br />
			::Sleep(dwMiliseconds);<br />
		}<br />
	};<br />
}


3.) To test an run your C-Code you have to create a new project.
Rightklick on the Projectmap to add a new project.
Choose under Visual C#-Projects the Console-Application and named it 'CLibraryDemo1'
Your C#-Code will be placed here.
You have to add the Link to our CLibrary.
Rightclick on ClibraryDemo1-Links and add a link. Choose in the link-dialog the Projet-Folder. Doubleclick on the CLibrary-entry.
Open the CLibrary.cs file and add the using CLibrary; rename then class CLass1 to CLibraryDemo1.
Build the CLibraryDemo1 application and test your functions.
<br />
using System;<br />
<br />
using CLibrary;<br />
<br />
namespace CLibraryDemo1<br />
{<br />
	/// <summary><br />
	/// Zusammenfassung für Class1.<br />
	/// </summary><br />
	class CLibraryDemo1<br />
	{<br />
		/// <summary><br />
		/// Der Haupteinstiegspunkt für die Anwendung.<br />
		/// </summary><br />
		[STAThread]<br />
		static void Main(string[] args)<br />
		{<br />
			int sum = CLib.SumTest(1,2);<br />
			CLib.Sleep(100);<br />
		}<br />
	}<br />
}<br />


Good luckBig Grin | :-D

Manfred Becker
GeneralRe: can u use c code in c# project? Pin
Anonymous18-Jan-05 1:27
Anonymous18-Jan-05 1:27 
GeneralRe: can u use c code in c# project? Pin
David L.D18-Jan-05 7:13
sussDavid L.D18-Jan-05 7:13 
GeneralRe: can u use c code in c# project? Pin
ManiB18-Jan-05 20:48
ManiB18-Jan-05 20:48 
GeneralRe: can u use c code in c# project? Pin
David L.D18-Jan-05 23:41
sussDavid L.D18-Jan-05 23:41 
GeneralRe: can u use c code in c# project? Pin
ManiB19-Jan-05 4:05
ManiB19-Jan-05 4:05 
GeneralRe: can u use c code in c# project? Pin
David L.D19-Jan-05 9:09
sussDavid L.D19-Jan-05 9:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.