[DllImport("kernel32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool Beep(uint dwFreq, uint dwDuration);
<DllImport("kernel32.dll", SetLastError := True)> _
Private Shared Function Beep(dwFreq As UInteger, dwDuration As UInteger) As Boolean
End Function
Declare Function Lib "kernel32.dll" Alias "Beep" (ByVal dwFrequency As Long, ByVal dwMilliseconds As Long) As Long
dwFreq
Windows NT:
Specifies the frequency, in hertz, of the sound. This parameter must be in the range 37 through 32767 (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
Just to show how simple it is with cygwin. The hard part is figuring out the include files. But you can grep thru /usr/include/*/* to find Beep, which is in utilapiset.h. The first 2 includes seem to be always needed.
This sends sound to the 'speaker' which may not exist in more recent version of windows (or at least the hardware that the Beep() function is 'wired' to may not exist). But if you get a delay when running this, it probably worked even if no sound.
Save to beep.c, and compile at the command line with:
gcc -Wall beep.c /cygdrive/c/windows/system32/kernel32.dll
the /cygdrive/.../kernel32.dll might be unneeded, bit it shows how things work. Less well known DLLs may need to be listed on the command line.
#include <w32api/windef.h>
#include <w32api/winbase.h>
#include <w32api/utilapiset.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
int main() {
uint i;
printf("Testing PC speaker...\n");
for (i = 300; i <= 20000; i += 25)
if (0 == Beep(i, 5)) errx(1, "Beep() fails & returns 0");
printf("Testing complete.\n");
return 0;
}
Added by John Refling
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
-VB.NET 10 addition by: Shawn LaRoche