C# And API's


API (Application Programming Interface) is aset of commands, which interfaces the programs with the processors. Themost commonly used set of external procedures are those that make upMicrosoft Windows itself. The Windows API contains thousands offunctions, structures, and constants that you can declare and use inyour projects. Those functions are written in the C language, however,so they must be declared before you can use them. The declarations forDLL procedures can become fairly complex. Specifically to C# it is morecomplex than VB. You can use API viewer tool to get API functiondeclaration but you have to keep in mind the type of parameter which isdifferent in C#.

Most of the advanced languages support APIprogramming. The Microsoft Foundation Class Library (MFC) frameworkencapsulates a large portion of the Win32 (API). ODBC API Functions areuseful for performing fast operations on database. With API yourapplication can request lower-level services to perform on computer’soperating system. As API supports thousands of functionality fromsimple Message Box to Encryption or Remote computing, developers shouldknow how to implement API in their program.

API\?s has many types depending on OS, processor and functionality.

OS specific API:

Each operating system has common set of API’s and some special


Windows NT supports MS-DOS, Win16, Win32,POSIX (Portable Operating System Interface), OS/2 console API andWindows 95 supports MS-DOS, Win16 and Win32 APIs.

Win16 & Win32 API:

Win16 is an API created for 16-bit processor and relies on 16 bit values. It has platform independent nature.

e.g. you can tie Win16 programs to MS-DOS feature like TSR programs.

Win32 is an API created for 32-bit processorand relies on 32 bit values. It is portable to any operating system,wide range of processors and platform independent nature.

Win32 API?s has ?32? prefix after the library name e.g. KERNEL32, USER32 etc?

All APIs are implemented using 3 Libraries.



It is the library named KERNEL32.DLL, which supports capabilities that are associated with OS such as

-Process loading.
-Context switching.
-File I/O.
-Memory management.

e.g. The GlobalMemoryStatus function obtains information about the system’s current usage of both physical and virtual memory


This is the library named “USER32.DLL” in Win32.

This allows managing the entire user interfaces such as

-Dialog Boxes
-Icons etc.

e.g. The DrawIcon function draws an icon or cursor into the specified device context.

3. GDI (Graphical Device Interface)

This is the library named “GDI32.dll” inWin32. It is Graphic output library. Using GDI Windows draws windows,menus and dialog boxes.

-It can create Graphical Output.
-It can also use for storing graphical images.

e.g. The CreateBitmap function creates abitmap with the specified width, height, and color format (color planesand bits-per-pixel).

C# and API:

Implementing API?s in C# is tuff job forbeginners. Before implementing API you should know how to implementstructure in C#, type conversion, safe/unsafe code, managed/unmanagedcode and lots more.

Before implementing complex API?s we willstart with simple MessageBox API. To implement code for MessageBox APIopen new C# project and add one button. When button gets clicked thecode will display Message Box.

Since we are using external library add namespace:

using System.Runtime.InteropServices;
Add following lines to declare API>


public static extern int MessageBox(int h, string m, string c, int type);

WhereDllImport attribute used for callingmethod from unmanaged code. “User32.dll” indicates library name.DllImport attribute specifies the dll location that contains theimplementation of an extern method. The static modifier used to declarea static member, which belongs to the type itself rather than to aspecific object, extern is used to indicate that the method isimplemented externally. A method that is decorated with the DllImportattribute must have the extern modifier.

MessageBox is function name, which returns int and takes 4 parameters as shown in declaration.

Many API?s uses structure to pass andretrieve values, as it is less expensive. It also uses constant datatype for passing constant data and simple data type for passingBuilt-in data type as seen in previous declaration of MessageBoxfunction.

Add following code for button click event:

Pages: 1 2

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!