Using APIs in C # is a daunting problem for beginners. In the use of APIs you must know how to use structure, type conversion, security/unsafe code, controllable/uncontrollable code in C # and many other knowledge.
All from the simple beginning, complex people for a moment can not accept. Let's start by implementing a simple MessageBox. First open vs.net, create a new C # project, and add a button. When this button is clicked, a MessageBox dialog box is displayed.
That is, we need to refer to a foreign library, so we have to 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);
Here the DllImport property is used to invoke a method from within the controllable code. "User32.dll" Sets the class library name. The DllImport property specifies the location of the DLL, which includes the external method of the call. The static modifier declares a static element that belongs to the type itself rather than the object specified above. extern indicates that this method will be executed outside the project, and the method imported using DllImport must use an extern modifier.
MessageBox is the function name, with 4 parameters, and the return value is a number.
Most APIs can pass and return a value.
Add the Click event code:
protected void Button1_Click (object sender, System.EventArgs e)
{
MessageBox (0, "API message box", "API Demo", 0);
}
Compile and run the program, and when you click the button, you will see the dialog box, which is the API function you use.
Use the structure body
Manipulating an API with a struct is much more complicated than using a simple API. But once you have mastered the API process, the entire API world will be in your hands.
In the following example we will use the GetSystemInfo API to get information about the entire system.
The first step is to open C # to create a form project, the same Add a button button, in the Code window to enter the following code, import namespace:
Using System.Runtime.InteropServices;
Declares a struct that will act as an argument to the 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;
}
Declaring API functions:
[DllImport ("kernel32")]
static extern void GetSystemInfo (ref system_info PSI);
Add the following code to the click event handling of the button:
First, you create a SYSTEM_INFO structure and pass it to the GetSystemInfo function.
protected void Button1_Click (object sender, System.EventArgs e)
{
Try
{
System_info PSI = new System_info ();
GetSystemInfo (ref PSI);
//
//
//
Once you receive the returned structure, you can perform the operation with the returned arguments.
E.g.listbox1.insertitem (0,psi.dwactiveprocessormask.tostring ());:
//
//
//
}
catch (Exception er)
{
MessageBox.Show (Er. message);
}
}
Created by Ajit Mungale
Program Supplemental 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 Collect 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 Collection of memory
[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;
Get System Information
[DllImport ("kernel32")]
static extern void GetSystemInfo (ref system_info PSI);
Getting memory information
[DllImport ("kernel32")]
static extern void GlobalMemoryStatus (ref memorystatus BUF);
Processor type
public const int processor_intel_386 = 386;
public const int processor_intel_486 = 486;
public const int processor_intel_pentium = 586;
public const INT processor_mips_r4000 = 4000;
public const int processor_alpha_21064 = 21064;
Public Form1 ()
{
InitializeComponent ();
}
public override void Dispose ()
{
Base. Dispose ();
Components. Dispose ();
}
private void InitializeComponent ()
{
this.components = new System.ComponentModel.Container ();
this.mainmenu1 = new System.WinForms.MainMenu ();
This.button1 = new System.WinForms.Button ();
This.listbox1 = new System.WinForms.ListBox ();
This.menuabout = new System.WinForms.MenuItem ();
MainMenu1.MenuItems.All = new System.winforms.menuitem[1] {this.menuabout};
Button1. Location = new System.Drawing.Point (148, 168);
Button1. Size = new System.Drawing.Size (112, 32);
Button1. TabIndex = 0;
Button1. Text = "&get Info";
Button1. Click + + new System.EventHandler (This.button1_click);
Listbox1.location = new System.Drawing.Point (20, 8);
Listbox1.size = new System.Drawing.Size (368, 147);
Listbox1.tabindex = 1;
Menuabout.text = "&about";
Menuabout.index = 0;
Menuabout.click + = new System.EventHandler (This.menuabout_click);
This. Text = "System information-using API";
This. MaximizeBox = false;
This. AutoScaleBaseSize = new System.Drawing.Size (5, 13);
This. MinimizeBox = false;
This. Menu = this.mainmenu1;
This. ClientSize = new System.Drawing.Size (408, 213);
This. Controls.Add (This.listbox1);
This. Controls.Add (This.button1);
}
protected void Menuabout_click (object sender, System.EventArgs e)
{
Form abt=new about ();
Abt. ShowDialog ();
}
protected void Button1_Click (object sender, System.EventArgs e)
{
Try
{
System_info PSI = new System_info ();
GetSystemInfo (ref PSI);
String Cputype;
Switch (psi.dwprocessortype)
{
Case PROCESSOR_INTEL_386:
Cputype= "Intel 386";
Break
Case processor_intel_486:
Cputype = "Intel 486";
Break
Case Processor_intel_pentium:
Cputype = "Intel Pentium";
Break
Case processor_mips_r4000:
Cputype = "MIPS R4000";
Break
Case processor_alpha_21064:
Cputype = "DEC Alpha 21064";
Break
Default:
Cputype = "(unknown)";
}
Listbox1.insertitem (0, "Active Processor Mask:" +psi.dwactiveprocessormask.tostring ());
Listbox1.insertitem (1, "Allocation granularity:" +psi.dwallocationgranularity.tostring ());
Listbox1.insertitem (2, "Number of processors:" +psi.dwnumberofprocessors.tostring ());
Listbox1.insertitem (3, "OEM ID:" +psi.dwoemid.tostring ());
Listbox1.insertitem (4, "Page Size:" +psi.dwpagesize.tostring ());
Listbox1.insertitem (5, "Processor level Value:" +psi.dwprocessorlevel.tostring ());
Listbox1.insertitem (6, "Processor Revision:" + pSI.dwProcessorRevision.ToString ());
Listbox1.insertitem (7, "CPU type:" +cputype);
Listbox1.insertitem (8, "Maximum Application Address:" +psi.lpmaximumapplicationaddress.tostring ());
Listbox1.insertitem (9, "Minimum Application Address:" +psi.lpminimumapplicationaddress.tostring ());
/************** gets the return value from the GlobalMemoryStatus ****************/
Memorystatus memst = new Memorystatus ();
GlobalMemoryStatus (ref memst);
Listbox1.insertitem ("Available Page File:" + (memst.dwavailpagefile/1024). ToString ());
Listbox1.insertitem (One, "Available physical Memory:" + (memst.dwavailphys/1024). ToString ());
Listbox1.insertitem ("Available Virtual Memory:" + (memst.dwavailvirtual/1024). ToString ());
Listbox1.insertitem ("Size of Structur:" + memSt.dwLength.ToString ());
Listbox1.insertitem ("Memory in use:" + memSt.dwMemoryLoad.ToString ());
Listbox1.insertitem ("Total Page Size:" + (memst.dwtotalpagefile/1024). ToString ());
Listbox1.insertitem ("Total physical Memory:" + (memst.dwtotalphys/1024). ToString ());
Listbox1.insertitem ("Total Virtual Memory:" + (memst.dwtotalvirtual/1024). ToString ());
}
catch (Exception er)
{
MessageBox.Show (Er. message);
}
}
public static void Main (string[] args)
{
Try
{
Application.Run (New Form1 ());
}
catch (Exception er)
{
MessageBox.Show (Er. message);
}
}
}
}