C#中調用API

來源:互聯網
上載者:User

介紹

  API(Application Programming Interface),我想大家不會陌生,它是我們Windows編程的常客,雖然基於.Net平台的C#有了強大的類庫,但是,我們還是不能否認API在Windows編程中的重要性。大多數的程式設計語言都支援API編程,而.Net平台中的MFC(Microsoft Foundation Class Library)構架本身就封裝了大部分的API。

  做為程式員,我們需要瞭解API從字面上瞭解便是編程介面,因此,做為開發人員,需要瞭解的只是API的使用方法。

  API根據作業系統、處理器及功能性的不同而擁有很多不同的類型。   作業系統特用的API:

  每種作業系統都有許多通用的API以及一些特用的API,這些特用的API只能在當前作業系統中執行。

  例如:

  Windows NT 支援 MS-DOS, Win16, Win32, POSIX (Portable Operating System Interface), OS/2 console API; 而 Windows 95 支援 MS-DOS, Win16 以及 Win32 APIs.

  Win16 & Win32 API:

  Win16是為十六位處理器開發的,早期的作業系統均支援。

  Win32則是為32位處理器開發。它可移植性強,被大部分的處理器所支援。

  Win32 API在庫名後有一個”32”尾碼。比如KERNEL32,USER32等。

  所有API在下面3個庫中得以運行:

  Kernel
  User
  GDI

  1. KERNEL

  他的庫名為 KERNEL32.DLL, 他主要用於產生與作業系統之間的關聯:

  程式載入
  上下文選擇.
  檔案輸入輸出.
  記憶體管理.
  例如: GlobalMemoryStatus 函數就包括當前實體記憶體及虛擬記憶體的使用資訊。

  2. USER

  這個類庫在Win32中名叫 USER32.DLL。

  它允許管理全部的使用者介面,比如:

  視窗
  菜單
  對話方塊
  表徵圖等.,
  例如: DrawIcon 函數將在指定的裝置關聯上“畫”出表徵圖或者滑鼠。

  3. GDI (Graphical Device Interface)

  它在Win32中的庫名為:GDI32.dll,它是圖形輸出庫。使用GDI Windows“畫”出視窗、菜單以及對話方塊等:

  它能建立圖形輸出.
  它也能儲存圖形檔案.
  例如: CreateBitmap 函數就能通過指定的長、寬、顏色建立一個位元影像。
  

  C# 中操作API:

  作為初學者來說,在C#中使用API確是一件令人頭疼的問題。在使用API之間你必須知道如何在C#中使用結構、類型轉換、安全/不安全的程式碼,可控/不可控代碼等許多知識。

  一切從簡單開始,複雜的大家一時不能接受。我們就從實現一個簡單的MessageBox開始。首先開啟VS.Net ,建立一個新的C#工程,並添加一個Button按鈕。當這個按鈕被點擊,則顯示一個MessageBox對話方塊。

  即然我們需要引用外來庫,所以必須匯入一個Namespace:

  using System.Runtime.InteropServices;

  接著添加下面的代碼來聲明一個API:

  [DllImport("User32.dll")]

  public static extern int MessageBox(int h, string m, string c, int type);

  此處DllImport屬性被用來從不可控代碼中調用一方法。”User32.dll”則設定了類庫名。DllImport屬性指定dll的位置,這個dll中包括調用的外部方法。Static修飾符則聲明一個靜態元素,而這個元素屬於類型本身而不是上面指定的對象。extern則表示這個方法將在工程外部執行,使用DllImport匯入的方法必須使用extern修飾符。

  MessageBox 則是函數名,擁有4個參數,其傳回值為數字。

  大多數的API都能傳遞並傳回值。

  添中Click點擊事件代碼:

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

  {

      MessageBox (0,"API Message Box","API Demo",0);

  }

  編譯並運行這個程式,當你點擊按鈕後,你將會看到對話方塊,這便是你使用的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);

     }

  }

              

    調用API全部代碼

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

       }

   }

  }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.