As a beginner, using APIs in C # is a headache. When using APIs, you must know how to use structure, type conversion, secure/Insecure code, controllable/uncontrollable code in C.
Everything starts from simple, and complicated people cannot accept it at the moment. Let's start with implementing a simple MessageBox. Start VS. Net, create a new C # project, and add a Button. When this button is clicked, A MessageBox dialog box is displayed.
Now we need to reference the external database, so we must import a Namespace:
Using System. Runtime. InteropServices;
Then add the following code to declare an API:
[DllImport ("User32.dll")]
Public static extern int MessageBox (int h, string m, string c, int type );
The DllImport attribute is used to call a method from uncontrollable Code ." User32.dll "sets the class library name. The DllImport attribute specifies the dll location, which includes the called external method. The Static modifier declares a Static element, which belongs to the type rather than the object specified above. Extern indicates that this method will be executed outside the project. The method imported using DllImport must use the extern modifier.
MessageBox is the function name and has four parameters. The return value is a number.
Most APIs can pass and return values.
Click Event code:
Protected void button#click (object sender, System. EventArgs e)
{
MessageBox (0, "API Message Box", "API Demo", 0 );
}
Compile and run this program. When you click the button, you will see the dialog box, which is the API function you are using.
Use struct
Operations with struct are much more complex than simple APIs. But once you have mastered the API process, the whole API world will be under your control.
In the following example, we will use the GetSystemInfo API to obtain information about the entire system.
The first step is to open C # To create a Form project, add a Button, enter the following code in the code window, and import the Namespace:
Using System. Runtime. InteropServices;
Declare a struct as a parameter of GetSystemInfo:
[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;
}
StatementAPIFunction:
[DllImport ("kernel32")]
Static extern void GetSystemInfo (ref SYSTEM_INFO pSI );
Add the following code to the button click event processing:
Create a SYSTEM_INFO struct and pass it to the GetSystemInfo function.
Protected void button#click (object sender, System. EventArgs e)
{
Try
{
SYSTEM_INFO pSI = new SYSTEM_INFO ();
GetSystemInfo (ref pSI );
//
//
//
Once you receive the returned struct, you can perform the operation with the returned parameters.
E. g. listBox1.InsertItem (0, pSI. dwActiveProcessorMask. ToString ());:
//
//
//
}
Catch (Exception er)
{
MessageBox. Show (er. Message );
}
}
// Created By Ajit Mungale
// The program supplements the flying knife
Namespace UsingAPI
{
Using System;
Using System. Drawing;
Using System. Collections;
Using System. ComponentModel;
Using System. WinForms;
Using System. Data;
Using System. Runtime. InteropServices;
// Struct collects system information
[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;
}
// Struct memory collection
[StructLayout (LayoutKind. Sequential)]
Public struct MEMORYSTATUS
{
Public uint dwLength;
Public uint dwMemoryLoad;
Public uint dwTotalPhys;
Public uint dwAvailPhys;
Public uint dwTotalPageFile;
Public uint dwAvailPageFile;
Public uint dwTotalVirtual;
Public uint dwAvailVirtual;
}
Public class Form1: System. WinForms. Form
{
Private System. ComponentModel. Container components;
Private System. WinForms. MenuItem menuAbout;
Private System. WinForms. MainMenu mainMenu1;
Private System. WinForms. ListBox listBox1;
Private System. WinForms. Button button1;