Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The following code example illustrates how to use Platform Invoke (PInvoke) to play a wave sound file on a mobile device.
This example code plays a sound file by using PlaySound on the mobile device. This code uses System.Runtime.InteropServices to invoke the PlaySound method of the Compact Framework's CoreDll.DLL.
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace MobileSoundPInvoke
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
public Form1()
{
InitializeComponent();
PlaySound(".\\sound.wav");
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.Menu = this.mainMenu1;
this.Text = "Form1";
}
#endregion
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
static void Main()
{
Application.Run(new Form1());
}
private enum Flags
{
SND_SYNC = 0x0000,
SND_ASYNC = 0x0001,
SND_NODEFAULT = 0x0002,
SND_MEMORY = 0x0004,
SND_LOOP = 0x0008,
SND_NOSTOP = 0x0010,
SND_NOWAIT = 0x00002000,
SND_ALIAS = 0x00010000,
SND_ALIAS_ID = 0x00110000,
SND_FILENAME = 0x00020000,
SND_RESOURCE = 0x00040004
}
[DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
private extern static int MobilePlaySound(string szSound, IntPtr hMod, int flags);
public void PlaySound(string fileName)
{
MobilePlaySound(fileName, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_FILENAME));
}
}
}
Create a new C# Smartphone Application project in Visual Studio and call it MobileSoundPInvoke.
Copy the code from the previous example, and paste it over the Form1.cs file of your MobileSoundPInvoke project in a console application.
- Make sure the appropriate parameters are passed to the CMobilePlaySound (string szSound, IntPtr hMod, int flags) function, including the path and file name for the .wav file.
For more information about security, see .NET Framework Security.
Platform Invoke Technology Sample