C #Operation in progressAPI:
As a beginner, using APIs in C # is a headache. You must know how to use structure, type conversion, security, and insecurity in C # Between APIs.Code, Controllable/uncontrollable code, and many other knowledge.
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 thisProgramWhen 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;
// Obtain system information
[Dllimport ("Kernel32")]
Static extern void getsysteminfo (ref system_info psi );
// Obtain 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_timeout = 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. Fig. Point (148,168 );
Button1.size = new system. Drawing. Size (112, 32 );
Button1.tabindex = 0;
Button1.text = "& get info ";
Button1.click + = new system. eventhandler (this. button#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 button#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_case:
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 ());
/************* Obtain the return value from globalmemorystatus ****************/
Memorystatus memst = new memorystatus ();
Globalmemorystatus (ref memst );
Listbox1.insertitem (10, "Available page file:" + (memst. dwavailpagefiles/1024). tostring ());
Listbox1.insertitem (11, "available physical memory:" + (memst. dwavailphys/1024). tostring ());
Listbox1.insertitem (12, "available virtual memory:" + (memst. dwavailvirtual/1024). tostring ());
Listbox1.insertitem (13, "size of structur:" + memst. dwlength. tostring ());
Listbox1.insertitem (14, "memory in use:" + memst. dwmemoryload. tostring ());
Listbox1.insertitem (15, "total page size:" + (memst. dwtotalpagefiles/1024). tostring ());
Listbox1.insertitem (16, "total physical memory:" + (memst. dwtotalphys/1024). tostring ());
Listbox1.insertitem (17," 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 );
}
}
}
}