Click here to Skip to main content
15,896,111 members
Articles / Programming Languages / C#

Calling API functions using C#

Rate me:
Please Sign up or sign in to vote.
4.81/5 (41 votes)
10 Oct 20015 min read 665.4K   9.9K   125  
This article helps you to get an idea about calling API functions in C#.
//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 to retrive system info
	[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 to retrive memory status
	[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;
		
		
		//To get system information
		[DllImport("kernel32")]
		static extern void GetSystemInfo(ref SYSTEM_INFO pSI);
		
		//To get Memory status
		[DllImport("kernel32")]
		static extern void GlobalMemoryStatus(ref MEMORYSTATUS buf);
		
      
		//Cnstants used for processor types
		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 ();
			//@this.TrayHeight = 90;
			//@this.TrayLargeIcon = false;
			//@this.TrayAutoArrange = true;
			//@mainMenu1.SetLocation (new System.Drawing.Point (7, 7));
			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());
				// Processor Level (Req filtering to get level)
				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());
				
			/**************	To retrive info from 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 ); 
			}

        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions