[DllImport("kernel32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool Beep(uint dwFreq, uint dwDuration);
Test
Declare Function Beep Lib "kernel32.dll" (dwFreq As Integer, _
dwDuration As Integer) As Boolean
dwFreq
Windows NT:
Specifies the frequency, in hertz, of the sound. This parameter must be in the range 37 through 32,767 (0x25 through 0x7FFF).
Windows 95:
The parameter is ignored.
dwDuration
Windows NT:
Specifies the duration, in milliseconds, of the sound.
Windows 95:
The parameter is ignored.
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Unlike MessageBeep, this function is synchronous. (It doesn't return control to its caller until the sound finishes.)
Please add some sample code!
The following sample plays the PC speaker in a series of ascending frequencies:
using System;
using System.Runtime.InteropServices;
class BeepSample
{
[DllImport("kernel32.dll", SetLastError=true)]
static extern bool Beep(uint dwFreq, uint dwDuration);
static void Main()
{
Console.WriteLine("Testing PC speaker...");
for (uint i = 100; i <= 20000; i++)
{
Beep(i, 5);
}
Console.WriteLine("Testing complete.");
}
}
Added by David Carachi
System.Console.Beep
This function does nothing when imported into a VB6 application. It does however work properly when called directly from a C++ application compiled with MSVC6.
Added by Barny Short
To run this function in VB6 the declaration you have to make is
Declare Function Lib "kernel32.dll" Alias "Beep" (dwFrequency As Long, dwMilliseconds As Long) As Long
Have fun with it. I am lol.