Remarque
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de vous connecter ou de modifier des répertoires.
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de modifier des répertoires.
Mise à jour : novembre 2007
L'exemple suivant montre comment utiliser un appel de code non managé (PInvoke) pour lire un fichier audio sur un appareil mobile.
Exemple
Cet exemple lit un fichier audio en utilisant PlaySound sur l'appareil mobile. Ce code utilise System.Runtime.InteropServices pour appeler la méthode PlaySound de la DLL CoreDll.DLL du Compact FrameWork.
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));
}
}
}
Compilation du code
Créez un nouveau projet Application Smartphone C# dans Visual Studio et appelez-le MobileSoundPInvoke.
Copiez le code dans l'exemple précédent et collez-le dans le fichier Form1.cs de votre projet MobileSoundPInvoke d'une application console.
Programmation fiable
- Veillez à utiliser les paramètres appropriés envoyés à la fonction CMobilePlaySound (string szSound, IntPtr hMod, int flags), y compris le chemin d'accès et le nom du fichier .wav.
Sécurité
Pour plus d'informations sur la sécurité, consultez Sécurité du .NET Framework (en anglais)
Voir aussi
Tâches
Appel de plate-forme, exemple de technologie
Concepts
Exemples de projet Smart Device
Comment faire dans le développement Smart Device
Autres ressources
Utilisation d'un PInvoke explicite en C++ (attribut DllImport)