Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

MessageBeep (user32)
 
.
Summary
Plays a waveform sound.

C# Signature:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool MessageBeep(beepType uType);

VB.NET Signature:

<DllImport("user32.dll")> _
Private Shared Function MessageBeep(ByVal uType As beepType) As Boolean
End Function

dylan.NET Signiture:

dllimport user32.dll
callconv winapi
charset auto
method public static pinvokeimpl boolean MessageBeep(var uType as integer)
setattr param|0 marshalas Bool

Tips & Tricks:

Just add the sample code and a using system.runtime.interopservices;. There is no need to add the import System.Runtime.InteropServices statement in dylan.NET.

Just add the sample code and a using system.runtime.interopservices;

With the enum beepType you get intellisense when you type "beep." and you can then select the kind of beep you want. For dylan.NET, use the fields containing the integer values inside the WinSnd class of dnu.dll at http://dylandotnetapps.codeplex.com/

With the enum beepType you get intellisense when you type "beep." and you can then select the kind of beep you want

Sample Code:

    /// <summary>
    /// Enum type that enables intellisense on the private <see cref="Beep"/> method.
    /// </summary>
    /// <remarks>
    /// Used by the public Beep <see cref="Beep"/></remarks>
    public enum beepType : uint
    {
        /// <summary>
        /// A simple windows beep
        /// </summary>            
        SimpleBeep  = 0xFFFFFFFF,
        /// <summary>
        /// A standard windows OK beep
        /// </summary>
        OK    = 0x00,
        /// <summary>
        /// A standard windows Question beep
        /// </summary>
        Question  = 0x20,
        /// <summary>
        /// A standard windows Exclamation beep
        /// </summary>
        Exclamation  = 0x30,
        /// <summary>
        /// A standard windows Asterisk beep
        /// </summary>
        Asterisk  = 0x40,
    }

    [DllImport("User32.dll", ExactSpelling=true)]
    private static extern bool MessageBeep(uint type);

    /// <summary>
    /// Method to call interop for system beep
    /// </summary>
    /// <remarks>Calls Windows to make computer beep</remarks>
    /// <param name="type">The kind of beep you would like to hear</param>
    public static void beep(beepType type)
    {
        MessageBeep((uint)type);
    }

///////////////// ANOTHER EXAMPLE same as the above but shows all beeps.

    class Class1
    {
        public enum beepType
        {
            /// <summary>
            /// A simple windows beep
            /// </summary>            
            SimpleBeep  = -1,
            /// <summary>
            /// A standard windows OK beep
            /// </summary>
            OK    = 0x00,
            /// <summary>
            /// A standard windows Question beep
            /// </summary>
            Question  = 0x20,
            /// <summary>
            /// A standard windows Exclamation beep
            /// </summary>
            Exclamation  = 0x30,
            /// <summary>
            /// A standard windows Asterisk beep
            /// </summary>
            Asterisk  = 0x40,
        }
        [DllImport("User32.dll", ExactSpelling=true)]
        private static extern bool MessageBeep(uint type);

        static void Main(string[] args)
        {
            Class1.beep(beepType.Asterisk);
            Thread.Sleep(1000);
            Class1.beep(beepType.Exclamation);
            Thread.Sleep(1000);
            Class1.beep(beepType.OK);
            Thread.Sleep(1000);
            Class1.beep(beepType.Question);
            Thread.Sleep(1000);
            Class1.beep(beepType.SimpleBeep);
        }
        public static void beep(beepType type)
        {
            MessageBeep((uint)type);
        }

        }
    }

More sample code:

Article http://msdn.microsoft.com/msdnmag/issues/03/07/NET/ ("Calling Win32 DLLs in C# with P/Invoke") gives a helpful wrapper class for MessageBeep.

Wrapper class source code: http://msdn.microsoft.com/msdnmag/issues/03/07/NET/default.aspx?fig=true#fig1

The above url is forbidden. Try: http://msdn.microsoft.com/en-us/magazine/cc164123.aspx

Alternative Managed API:

System.Console.Beep

System.Media.SystemSounds._.Play

Documentation
MessageBeep on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions