Home > Computers and Internet > Use of API in .net

Use of API in .net

1. First You create class in which You declare appropriate  API funtions
 
For ex.
 

using System;

using

System.Runtime.InteropServices ;

using

System.Text;

 

public class APIFunctions

{

[DllImport("kernel32", CharSet=CharSet.Unicode)]

static extern bool GetComputerName(string name, ref int len);

[DllImport("kernel32")]

static extern bool Beep(uint dwFreq, uint dwDuration);

[DllImport("User32.dll", ExactSpelling=true)]

private static extern bool MessageBeep(uint type);

[DllImport("winmm.dll")]

public static extern long PlaySound(String lpszName, long hModule, long dwFlags);

public string API_ComputerName

{

get

{

String computerName="abcdefghijklmnopqrstuvwxyz";

int len = 100;

GetComputerName(computerName, ref len);

string a=computerName;

return (computerName.Substring(0,len));

}

}

public void API_BeepSound()

{

Beep(500,500);

}

public void API_PlaySound()

{

long retval;

string fname = "d:\\tada.wav";

retval = PlaySound(fname, 0, 1);

}

}

2. You have to create Object of class and use in either in event or in function,

For ex.

APIFunctions Obj=new APIFunctions();

Obj.API_BeepSound ();

 

MessageBox.Show(Obj.API_ComputerName);

 

Obj.API_PlaySound();

 

 

 

 

 

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s