C # and API

Source: Internet
Author: User

Application Programming Interface is a set of commands, which interfaces the programs with the processors. the most commonly used set of external procedures are those that make up Microsoft Windows itself. the Windows API contains thousands of functions, structures, and constants that you can declare and use in your projects. those functions are written in the C language, however, so they must be declared before you can use them. the declarations for DLL procedures can become fairly complex. specifically to C # It is more complex than VB. you can use API Viewer tool to get API function declaration but you have to keep in mind the type of parameter which is different in C #.

API (Application Programming Interface) isProgramThe command set that interfaces with the processor. The most common method is to call Microsoft Windows internal processes externally. Windows APIs include thousands of functions, structures, and constants that you can use. These functions are written in C language. You must declare them before using them. Defining a DLL process can be quite complex, or even more complex than VB. You can use the API Viewer tool to get the declaration of API functions, but it must be noted that its parameter type is different from that of C.

Most of the advanced programming ages support API programming. the Microsoft Foundation Class Library (MFC) Framework encapsulates a large portion of the Win32 (API ). odbc api functions are useful for faster Ming fast operations on database. with API your application can request lower-level services to perform on computer's operating system. as API supports thousands of functionality from simple message box to encryption or Remote Computing, developers shoshould know how to implement API in their program.

Most advanced Languages Support APIs. Microsoft function library (MFC) encapsulates most Win32 APIs. ODBC APIs greatly improve the database operation speed. By using APIs, you can request more underlying system services. APIs are supported from simple dialogs to complex encryption operations. Developers should know how to use APIs in their programs

API has polictypes depending on OS, processor and functionality.

There are many types of APIS (for different operating systems, processors ............)

OS specific API:

Operating System-specific APIs:

Each operating system has common set of API's and some special e.g. windows NT supports MS-DOS, Win16, Win32, POSIX (Portable Operating System Interface), OS/2 console API and Windows 95 supports MS-DOS, Win16 and Win32 APIs,

Each operating system has a set of public APIs and proprietary APIs. For example: Windows NT supports MS-DOS, Win16, Win32, POSIX (Portable Operating System Interface), OS/2 console API; While Windows 95 supports MS-DOS, Win16 and Win32 API.

Win16 and Win32 APIs:

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.

Win16 is a 16-bit processor that uses a 16-bit value. It is an independent platform. For example, you can run a TSR program in a MS-DOS environment.

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

Win32 is a 32-bit processor and uses a 32-bit value. It can be used in any operating system and is widely used.

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

Win32 api dll generally has a 32 suffix, such as Kernel32 and USER32.

All APIs are implemented using 3 libraries.

All APIs are implemented in the following three DLL files.

        • Kernel
        • User
        • GDI
1. Kernel

It is the library named kernel32.dll, which supports capabilities that are associated with OS such

Its database name is kernel32.dll, which is an API set managed by the operating system

      • Process loading. Load the process
      • Context switching.
      • File I/O. File Operations
      • Memory Management. Memory Management

E.g.GlobalmemorystatusFunction obtains information about the system's current usage of both physical and virtual memory

For example:GlobalmemorystatusFunction to obtain the usage information of the Physical Virtual Memory of the system.

2. User

This is the library named "user32.dll" in win32.

In Win32, its database name is user32.dll

This allows managing the entire user interfaces such

It manages all user interfaces, such:

      • Windows
      • Menus menu
      • Dialog Boxes dialog box
      • Icons Etc., icons, etc.

E.g.DrawiconFunction draws an icon or cursor into the specified device context.

For example:DrawiconDraw an icon on the specified device.

3. GDI (graphical device interface)

This is the library named "gdi32.dll" in win32. it is graphic output library. using GDI windows draws windows, menus and dialog boxes.

This dll is gdi32.dll, which is responsible for the output of images. It uses GDI to draw windows, menus, and dialog boxes.

      • It can create graphical output. output image
      • It can also use for storing graphical images. store images

E.g.CreatebitmapFunction creates a bitmap with the specified width, height, and color format (color planes and bits-per-pixel ).

For example:CreatebitmapThe function creates a bitmap with the specified width, height, and color format.

C # and API:

Implementing API in C # Is tuff job for beginners. before implementing API you shoshould know how to implement structure in C #, type conversion, safe/Unsafe code, managed/unmanaged code and lots more.

The API Tool in C # is quite good for beginners. Before using APIs in C #, you should first know how to use structure, type conversion, security, and insecurity in C #.Code.

Before implementing complex API we will start with simple MessageBox API. To implement code for MessageBox API open new C # project and add one button. When button gets clicked the code will display message box.

Before using complex APIs, we first use a simple MessageBox API as a column. Open a C # project and add a button. In the button clicking event, a message box is displayed.

Add a namespace for using external libraries:

Using system. runtime. interopservices;

The following API Declaration

[Dllimport ("user32.dll")]
Public static extern int MessageBox (int h, string M, string C, int type );

Where dllimport attribute used for calling method from unmanaged code. "user32.dll" indicates library name. dllimport attribute specifies the DLL location that contains the implementation of an extern method. the static modifier used to declare a static member, which belongs to the type itself rather than to a specific object, extern is used to indicate that the method is implemented externally. a method that is decorated with the dllimport attribute must have the extern modifier.

The dllimport attribute is used to specify the location of the dynamic connection library containing external methods. "User32.dll" indicates the Database Name and static indicates that it is not a specific object. Extern indicates an external method. Methods With the dllimport attribute must have the modifier extern.

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

MessageBox is a Chinese number and contains four parameters to return an int value.

Using API uses structure to pass and retrieve values, as it is less expensive. it also uses constant data type for passing constant data and simple data type for passing built-in data type as seen in previous declaration of MessageBox function.

Many APIs use structures to pass and return parameters, which can reduce complexity. It also allows fixed parameters like the MessageBox function.

Add the following code to the button clicking event:

Protected void button#click (Object sender, system. eventargs E)
{

MessageBox (0, "API message box", "API Demo", 0 );

}

Compile and run the program. After clicking the button, you can see an information box called by API.

Using structure usage Structure

Working with API

, Which uses complex structure or structure in structure, is somewhat complex than using Simple API. But once you understand the implementation then whole API world is yours.

Complex structures are often used in APIs. But once you understand them, it will be very simple.

In next example we will useGetsysteminfoAPI which returns information about the current system.

The column below, we useGetsysteminfoAPI to obtain information about the current system.

Step 1: Add a C # window and add a button above to add a namespace in the window code page:

Using system. runtime. interopservices;

StatementGetsysteminfo Parameter structure:

[structlayout (layoutkind. Sequential)]

public struct system_info {

Public uint dwoemid;

Public uint dwpagesize;

Public uint lpminimumapplicationaddress;

Public uint lpmaximumapplicationaddress;

Public uint dwactiveprocessormask;

Public uint dwnumberofprocessors;

Public uint dwprocessortype;

Public uint dwallocationgranularity;

Public uint dwprocessorlevel;

Public uint dwprocessorrevision;

}

Declare API functions:

[Dllimport ("Kernel32")]

Static extern void getsysteminfo (ref system_info psi );

where ref is method parameter keyword causes a method to refer to the same variable that was passed into the method.

ref is a keyword in the form of parameter transfer. It directs incoming and outgoing variables to the same variable (transfer by address)

Add following code in button click event in which first create struct object and then pass it to function.

Add the following code to the button-click event.

Protected void button#click (Object sender, system. eventargs E)
{

Try
{

System_info psi = new system_info ();
Getsysteminfo (ref psi );
//
//
//

Once you retrieve the structure perform operations on required Parameter

For example:

Listbox1.items. insert (0, PSI. dwactiveprocessormask. tostring ());

//
//
//



}



Catch (exception ER)
{


MessageBox. Show (ER. Message );

}


}

English version (original from: http://www.c-sharpcorner.com/1/CSandAPIAM.asp)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.