I created this prank program (just for fun) and also at the same time, learn controlling hardware devices, through C#.NET. This program is quite simple; as soon as the program starts, your disc tray ejects and closes and the PC speaker beeps for a second, and this entire routine is executed endlessly. Of course, it ends if the application closes/exits.
For this program, you will need to import the following:
using System.Runtime.InteropServices;
using System.Text;
Next, insert this block of code inside your C# class and outside any method:
[DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi)]
protected static extern int mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, IntPtr hwndCallback);
[DllImport("KERNEL32.DLL", EntryPoint = "Beep")]
public static extern bool Beep(int tone, int length);
Lastly, key in this code in your Main() or any method that you wish to call to trigger the opening and closing of cd tray and beeping of the speaker:
while (true)
{
int ret = mciSendString("set cdaudio door open", null, 0, IntPtr.Zero);
Beep(1000, 1000);
ret = mciSendString("set cdaudio door closed", null, 0, IntPtr.Zero);
Beep(1000, 1000);
}
There you have it. Basically, the important part of this program is when you make the CD ROM drive open and close and the PC Speaker beep. You can use this minor code block in the second and last blockquotes to any program that you wish to make. I just made this tutorial fun and enjoying. Learning is best when it is fun. :)

Hi thanks for putting this on here i now have something to work with, i shall pull it apart and work out what each bit does as i tend to learn better from this sort of thing than the usual helloworld example which is boaring to say the least regards pete
Posted by peter | May 19, 2011, 06:54Sure thing. thanks for dropping by..
Posted by jesh | May 19, 2011, 11:25