zoukankan      html  css  js  c++  java
  • C# 中操作API:

    使用结构体

      操作带有结构体的API比使用简单的API要复杂的多。但是一旦你掌握了API的过程,那个整个API世界将在你的掌握之中。

      下面的例子中我们将使用GetSystemInfo API 来获取整个系统的信息。

      第一步还是打开C#建立一个Form工程,同样的添中一个Button按钮,在代码窗中输入下面的代码,导入Namespace

      using System.Runtime.InteropServices;


      声明一个结构体,它将做为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;

      }


      声明API函数:



      [DllImport("kernel32")]

      static extern void GetSystemInfo(ref SYSTEM_INFO pSI);



      添加下面的代码至按钮的点击事件处理中:

      首先创建一个SYSTEM_INFO结构体,并将其传递给GetSystemInfo函数。



      protected void button1_Click (object sender, System.EventArgs e)

      {

          try

          {

              SYSTEM_INFO pSI = new SYSTEM_INFO();

              GetSystemInfo(ref pSI);

              //

              //

              //


      一旦你接收到返回的结构体,那么就可以以返回的参数来执行操作了。



      e.g.listBox1.InsertItem (0,pSI.dwActiveProcessorMask.ToString());:

              //

              //

              //

         }

         catch(Exception er)

         {

              MessageBox.Show (er.Message);

         }

      }

     //Created By Ajit Mungale

      //程序补充飞刀

      namespace UsingAPI

      {

      using System;

      using System.Drawing;

      using System.Collections;

      using System.ComponentModel;

      using System.WinForms;

      using System.Data;

      using System.Runtime.InteropServices;

      //Struct 收集系统信息

      [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 收集内存情况

      [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;

      //获取系统信息

        [DllImport("kernel32")]

        static extern void GetSystemInfo(ref SYSTEM_INFO pSI);

        //获取内存信息

        [DllImport("kernel32")]

        static extern void GlobalMemoryStatus(ref MEMORYSTATUS buf);

        //处理器类型

        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());

             /************** GlobalMemoryStatus 获取返回值****************/

             MEMORYSTATUS memSt = new MEMORYSTATUS ();
             GlobalMemoryStatus (ref memSt);

             listBox1.InsertItem(10,"Available Page File :"+ (memSt.dwAvailPageFile/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.dwTotalPageFile/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 );

           }

       }

      }

    }

  • 相关阅读:
    Django REST framework
    Docker学习11-sonarqube+jenkins持续集成代码审计(下)
    Docker学习10-sonarqube+jenkins持续集成代码审计(上)
    jmeter-4-linux下环境搭建 jmeter+ant+docker-jenkins,持续集成测试完成
    jmeter-3-linux下环境搭建jmeter+ant
    jmeter-2-ant+jenkins持续集成测试
    jmeter-1-apache ant-集成测试
    python-50-pip加速与pip包虚拟环境管理
    git-基本操作
    创建 PHP Composer 包并使用的操作指南
  • 原文地址:https://www.cnblogs.com/atun/p/2053383.html
Copyright © 2011-2022 走看看