A sample application to implement new library and ncurses

Now that we know the fundamentals, let's start building a sample application which implements ncurses. We will create a C# application which imports a C library or we can say .so file. This library has implemented ncurses functions. P/Invoke allows us to use those ncurses functions in our C# application.

First, we will create a C program which implements ncurses functions using an ncurses header. Let's create an application which takes an input character; this character will be printed in a window, based on the number of rows and columns the user enters from the application where we will import this library. The function drawCharOnWindow() prints the character, based on the number of rows and columns present in the window; it takes character, row, and column as input parameters. We get the size of the window using the ncurses function, getmaxyx(), and if the user enters q, it quits the printing of the character. Open Visual Studio Code and write the following code:

# include <stdio.h>
# include <ncurses.h>

int row, column, // current row and column (Top left is (0,0))
numberOfRows, // number of rows in current window
numberOfColumns; // number of columns in current window

void drawCharOnWindow(char drawChar, int row, int column)

{
move(row, column); // ncurses call to move cursor to given row, given
column

delch(); //ncurses calls to replace character
insch(drawChar); //under cursor by drawChar

refresh(); // ncurses call to update screen
row++; // go to next row
// check for need to shift right or wrap around
if (row == numberOfRows)
{
row = 0;
column++;
if (column == numberOfColumns) column = 0;
}
}
void hello(int i, int j, char c)
{
initscr(); // ncurses call to initialize window
refresh(); // curses call to implement all changes since last refresh
while (1)
{
if (c == 'q') break; // quit?
{
drawCharOnWindow(c, i, j);
}
}
}

Create a library using the following command. It will create a .so file at the location from where the Terminal is open, and we run this command:

gcc -shared -o libHelloSO.so -fPIC HelloSOLib.c -lncurses

Now, we will use this library from the C# program using Mono. The following example application takes input from the user for the character we will print and its location, row, and column value. To use the library which implemented ncurses and to use it in C# , we need to use the namespace, System.Runtime.InteropServices and import DLL, using the DllImport attribute:

using System;
using System.Runtime.InteropServices;

namespace Hello
{
class Program
{
[DllImport("/home/neha/Documents/
InteropWithMonoLib/libHelloSO.so")]
private static extern void hello(int i, int j, int c);

static void Main(string[] args)
{
Console.WriteLine("Enter Character you want to print:\n");
char c = Convert.ToChar(Console.ReadLine());
Console.WriteLine("Enter row numbers till where to want to
see pattern of character:\n");
int i = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter column numbers till where to want
to see pattern of character:\n");
int j = Convert.ToInt32(Console.ReadLine());
for (int a = 0; a < i; a++)
{
for (int b = 0; b < j; b++)
{
hello(i, j, c);
}
}
}
}
}

To create the .exe file, use the following Mono command:

mcs -out:helloNative.exe InteropWithNativeSO.cs

To run the program in Mono, use the following command:

mono helloNative.exe

We learned how to create an executable using Mono and run it; now, we will see how to create a .NET Core 2.0 console application. Open Visual Studio Code and set the location on the Terminal where we want to create the application, and run the following command for the console application:

dotnet new console

After the creation of the application, use the preceding code. Run this code using the following command:

dotnet run

It will take the input parameter, the character to print, row, and column location, and it will print that character in the window at the specified location, as shown here: 

After passing this detail, it prints the character at the specified row and column on the screen, as follows: