[DllImport ("coredll.dll")]
private static extern MMSYSERR waveOutOpen(ref IntPtr phwo, uint uDeviceID, ref WAVEFORMATEX pwfx, uint dwCallback, uint dwInstance, uint fdwOpen );
(TODO) As TODO
None.
Do you know one? Please contribute it!
Taken from "Using P/Invoke to Call Unmanaged APIs from Your Managed Classes" found here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/pinvoke.asp
Some of the sample code on MSDN as part of the PInvoke library gave the dllimport syntax like this:
[DllImport ("coredll.dll")]
private static extern Wave.MMSYSERR waveOutOpen(ref IntPtr phwo, uint uDeviceID, Wave.WAVEFORMATEX pwfx, IntPtr dwCallback, uint dwInstance, uint fdwOpen);
I could only get this to work by changing the following:
Wave.WAVEFORMATEX pwfx => ref WAVEFORMATEX pwfx // reference pointer rather than the format struct.
I kept getting the System.NotSupportedException when I tried to follow the MSDN recommended approach of passing the struct to the waveOutOpen rather than the ref. (If you downloaded the Pinvoke sample library, it was called WaveOut.cs located here: C:\Program Files\.NET Compact Framework Samples\PInvoke Library\Code\CS).
using System;
using System.Data;
using System.Runtime.InteropServices;
namespace waveoutopen {
class Tester
{
static void Main(string[] args)
{
Tester t = new Tester();
t.Run();
}
public void Run()
{
WAVEFORMATEX testFormat = new WAVEFORMATEX();
testFormat.wFormatTag = 1;
testFormat.wBitsPerSample = 16; // 8,16
testFormat.nChannels = 2;
testFormat.nSamplesPerSec = 44100;
testFormat.nBlockAlign = (ushort)(testFormat.nChannels *
(testFormat.wBitsPerSample / 8));
testFormat.nAvgBytesPerSec = (testFormat.nSamplesPerSec *
testFormat.nBlockAlign);
testFormat.cbSize = 0;
IntPtr m_hwi = IntPtr.Zero;
IntPtr hwnd = IntPtr.Zero;
MMSYSERR result = waveOutOpen(ref m_hwi, WAVE_MAPPER,
ref testFormat, 0, 0, 0);
Console.WriteLine("and the result of open is {0}",result.ToString());
}
private const int MMSYSERR_BASE = 0;
public const uint WAVE_MAPPER = unchecked((uint)(-1));
public enum MMSYSERR : int
{
NOERROR = 0,
ERROR = (MMSYSERR_BASE + 1),
BADDEVICEID = (MMSYSERR_BASE + 2),
NOTENABLED = (MMSYSERR_BASE + 3),
ALLOCATED = (MMSYSERR_BASE + 4),
INVALHANDLE = (MMSYSERR_BASE + 5),
NODRIVER = (MMSYSERR_BASE + 6),
NOMEM = (MMSYSERR_BASE + 7),
NOTSUPPORTED = (MMSYSERR_BASE + 8),
BADERRNUM = (MMSYSERR_BASE + 9),
INVALFLAG = (MMSYSERR_BASE + 10),
INVALPARAM = (MMSYSERR_BASE + 11),
HANDLEBUSY = (MMSYSERR_BASE + 12),
INVALIDALIAS = (MMSYSERR_BASE + 13),
BADDB = (MMSYSERR_BASE + 14),
KEYNOTFOUND = (MMSYSERR_BASE + 15),
READERROR = (MMSYSERR_BASE + 16),
WRITEERROR = (MMSYSERR_BASE + 17),
DELETEERROR = (MMSYSERR_BASE + 18),
VALNOTFOUND = (MMSYSERR_BASE + 19),
NODRIVERCB = (MMSYSERR_BASE + 20),
LASTERROR = (MMSYSERR_BASE + 20),
BADFORMAT = (MMSYSERR_BASE + 32)
}
[DllImport ("coredll.dll")]
private static extern MMSYSERR waveOutOpen(
ref IntPtr phwo,
uint uDeviceID,
ref WAVEFORMATEX pwfx,
uint dwCallback,
uint dwInstance,
uint fdwOpen
);
[StructLayout(LayoutKind.Sequential)]
public unsafe struct WAVEFORMATEX
{
public ushort wFormatTag;
public ushort nChannels;
public uint nSamplesPerSec;
public uint nAvgBytesPerSec;
public ushort nBlockAlign;
public ushort wBitsPerSample;
public ushort cbSize;
}
}
}