C # Operation API is transferred from Wisdom Valley

xiaoxiao2021-03-06  41

C # Operation API: As a beginner, use API in C # is indeed a headache. You must know how to use structures, type conversions, security / unsafe code, control / uncontrollable code, etc. in C #. Everything starts from simple start, and the complex everyone cannot accept it. We start with a simple MessageBox. First open vs.net, create a new C # project and add a Button button. When this button is clicked, a MessageBox dialog is displayed. Often we need to reference foreign libraries, so you 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);

Here DLLIMPORT attributes are used to call a method from uncontrollable code. "User32.dll" sets the class library name. The DLLIMPORT property specifies the location of the DLL, including the external method of the call. Static modifiers declare a static element, and this element belongs to the type itself instead of the object specified above. Extern means that this method will be executed outside the project, and the method of importing using the DLLIMPORT must use the extern modifier. MessageBox is a function name with 4 parameters that return values ​​for numbers. Most APIs can pass and return values. Add Click Click Event Code:

Protected Void Button1_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, this is the API function you use. Operating an API with a structure with a structure is more complicated than using a simple API. But once you have the process of the API, the entire API world will be in your master. In the following example we will use the GetSystemInfo API to get information about the entire system. The first step is to open the C # to establish an FORM project, the same addition, enter the following code in the code window, import Namespace:

Using system.Runtime.InteropServices;

Declare a structure, it will be a parameter for 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;

Declare API function:

[DLLIMPORT ("kernel32")] static extern void getSystemInfo (Ref system_info psi); Add the following code to the click event handle: First 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 parameters.

e.listbox1.insertitem (0, psi.dwactiveprocessormask.tostring ()) ;: // //} Catch (Excetion ER) {MessageBox.show (Er.Message);}}

// Created By Ajit Mungale // supplementary program 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 collecting memory where [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; // acquire system information [DllImport ( "kernel32")] static extern void GetSystemInfo (ref SYSTEM_INFO pSI); // get 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.locatio N = new system.drawing.point (20, 8); listbox1.size = new system.drawing.size (368, 147); listbox1.tabindex = 1; menuabout.text = "& quara; 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 {Form abt = new about (object sender, System.EventArgs e) (); 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.toTString ()); ListBox1.insertItem (6,"

转载请注明原文地址:https://www.9cbs.com/read-55961.html

New Post(0)