C#中调用API,二转载

调用API全部代码 

//Created By Ajit Mungale 

//程序补充 飞刀 

namespaceUsingAPI{ 
    
    usingSystem; 
    
    usingSystem.Drawing; 
    
    usingSystem.Collections; 
    
    usingSystem.ComponentModel; 
    
    usingSystem.WinForms; 
    
    usingSystem.Data; 
    
    usingSystem.Runtime.InteropServices; 
    
    //Struct 收集系统信息 
    
    [StructLayout(LayoutKind.Sequential)] 
    
    publicstructSYSTEM_INFO { 
        
        publicuintdwOemId; 
        
        publicuintdwPageSize; 
        
        publicuintlpMinimumApplicationAddress; 
        
        publicuintlpMaximumApplicationAddress; 
        
        publicuintdwActiveProcessorMask; 
        
        publicuintdwNumberOfProcessors; 
        
        publicuintdwProcessorType; 
        
        publicuintdwAllocationGranularity; 
        
        publicuintdwProcessorLevel; 
        
        publicuintdwProcessorRevision; 
        
    } 
    
    //struct 收集内存情况 
    
    [StructLayout(LayoutKind.Sequential)] 
    
    publicstructMEMORYSTATUS    { 
        
        publicuintdwLength; 
        
        publicuintdwMemoryLoad; 
        
        publicuintdwTotalPhys; 
        
        publicuintdwAvailPhys; 
        
        publicuintdwTotalPageFile; 
        
        publicuintdwAvailPageFile; 
        
        publicuintdwTotalVirtual; 
        
        publicuintdwAvailVirtual; 
        
    } 
    
    publicclassForm1 : System.WinForms.Form    { 
        
        privateSystem.ComponentModel.Containercomponents; 
        
        privateSystem.WinForms.MenuItemmenuAbout; 
        
        privateSystem.WinForms.MainMenumainMenu1; 
        
        privateSystem.WinForms.ListBoxlistBox1; 
        
        privateSystem.WinForms.Buttonbutton1; 
        
        //获取系统信息 
        
        [DllImport("kernel32")] 
        
        staticexternvoidGetSystemInfo(refSYSTEM_INFOpSI); 
        
        //获取内存信息 
        
        [DllImport("kernel32")] 
        
        staticexternvoidGlobalMemoryStatus(refMEMORYSTATUSbuf); 
        
        //处理器类型 
        
        publicconstintPROCESSOR_INTEL_386 = 386; 
        
        publicconstintPROCESSOR_INTEL_486 = 486; 
        
        publicconstintPROCESSOR_INTEL_PENTIUM = 586; 
        
        publicconstintPROCESSOR_MIPS_R4000 = 4000; 
        
        publicconstintPROCESSOR_ALPHA_21064 = 21064; 
        
        publicForm1() 
        
        { 
            
            InitializeComponent(); 
            
        } 
        
        publicoverridevoidDispose() 
        
        { 
            
            base.Dispose(); 
            
            components.Dispose(); 
            
        } 
        
        privatevoidInitializeComponent() 
        
        { 
            
            this.components = newSystem.ComponentModel.Container (); 
            
            this.mainMenu1 = newSystem.WinForms.MainMenu (); 
            
            this.button1 = newSystem.WinForms.Button (); 
            
            this.listBox1 = newSystem.WinForms.ListBox (); 
            
            this.menuAbout = newSystem.WinForms.MenuItem (); 
            
            mainMenu1.MenuItems.All = newSystem.WinForms.MenuItem[1] {this.menuAbout}; 
            
            button1.Location = newSystem.Drawing.Point (148, 168); 
            
            button1.Size = newSystem.Drawing.Size (112, 32); 
            
            button1.TabIndex = 0; 
            
            button1.Text = "&Get Info"; 
            
            button1.Click += newSystem.EventHandler (this.button1_Click); 
            
            listBox1.Location = newSystem.Drawing.Point (20, 8); 
            
            listBox1.Size = newSystem.Drawing.Size (368, 147); 
            
            listBox1.TabIndex = 1; 
            
            menuAbout.Text = "&About"; 
            
            menuAbout.Index = 0; 
            
            menuAbout.Click += newSystem.EventHandler (this.menuAbout_Click); 
            
            this.Text = "System Information - Using API"; 
            
            this.MaximizeBox = false; 
            
            this.AutoScaleBaseSize = newSystem.Drawing.Size (5, 13); 
            
            this.MinimizeBox = false; 
            
            this.Menu = this.mainMenu1; 
            
            this.ClientSize = newSystem.Drawing.Size (408, 213); 
            
            this.Controls.Add (this.listBox1); 
            
            this.Controls.Add (this.button1); 
            
        } 
        
        protectedvoidmenuAbout_Click (objectsender, System.EventArgse) 
        
        { 
            
            Formabt=newabout() ; 
            
            abt.ShowDialog(); 
            
        } 
        
        protectedvoidbutton1_Click (objectsender, System.EventArgse) 
        
        { 
            
            try            { 
                
                SYSTEM_INFOpSI = newSYSTEM_INFO(); 
                
                GetSystemInfo(refpSI); 
                
                stringCPUType; 
                
                switch (pSI.dwProcessorType) 
                
                { 
                    
                    casePROCESSOR_INTEL_386 : 
                    
                    CPUType= "Intel 386"; 
                    
                    break; 
                    
                    casePROCESSOR_INTEL_486 : 
                    
                    CPUType = "Intel 486" ; 
                    
                    break; 
                    
                    casePROCESSOR_INTEL_PENTIUM : 
                    
                    CPUType = "Intel Pentium"; 
                    
                    break; 
                    
                    casePROCESSOR_MIPS_R4000 : 
                    
                    CPUType = "MIPS R4000"; 
                    
                    break; 
                    
                    casePROCESSOR_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 获取返回值****************/ 
                
                MEMORYSTATUSmemSt = newMEMORYSTATUS (); 
                GlobalMemoryStatus (refmemSt);
                
                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(Exceptioner) 
            
            { 
                
                MessageBox.Show (er.Message); 
                
            } 
            
        } 
        
        publicstaticvoidMain(string[] args) 
        
        { 
            
            try            { 
                
                Application.Run(newForm1()); 
                
            } 
            
            catch(Exceptioner) 
            
            { 
                
                MessageBox.Show (er.Message ); 
                
            } 
            
        } 
        
    } 
    
}