CallBack Function In Win API

G

Guest

I used WIN API function [waveOutOpen]. There is a callback function in the
parameters. I can make it work in C#, but failed in vb.net. Can any experts
tell me the problem? Thanks.

VB.NET code
============
Imports System.Runtime.InteropServices

Public Class WaveOutTest

Public Const CALLBACK_FUNCTION As Integer = &H30000

Public Delegate Sub WaveCallBack(ByVal hdrvr As IntPtr, ByVal uMsg As
Integer, ByVal dwUser As Integer, ByVal dwParam1 As Integer, ByVal dwParam2
As Integer)

<DllImport("winmm.dll")> Public Shared Function waveOutOpen(ByRef
hWaveOut As IntPtr, ByVal uDeviceID As Integer, ByVal lpFormat As WaveFormat,
ByVal dwCallback As WaveCallBack, ByVal dwInstance As Integer, ByVal dwFlags
As Integer) As Integer
End Function


Private m_WaveOut As New IntPtr
Private m_Format As New WaveFormat
Private m_BufferProc As New WaveCallBack(AddressOf WaveOutCallBack)

Public Sub Test()
m_Format.SetWave(22050, 16, 2)
Dim iReturn As Integer = waveOutOpen(m_WaveOut, -1, m_Format,
m_BufferProc, 0, CALLBACK_FUNCTION)
' Note: iReturn = 10
If iReturn <> 0 Then
Throw New Exception("Open Failed!")
End If
End Sub

Public Shared Sub WaveOutCallBack(ByVal hdrvr As IntPtr, ByVal uMsg As
Integer, ByVal dwUser As Integer, ByVal dwParam1 As Integer, ByVal dwParam2
As Integer)
' Do something here
End Sub

End Class


<StructLayout(LayoutKind.Sequential)> Public Structure WaveFormat
Public wFormatTag As Short
Public nChannels As Short
Public nSamplesPerSec As Integer
Public nAvgBytesPerSec As Integer
Public nBlockAlign As Short
Public wBitsPerSample As Short
Public cbSize As Short


Public Sub SetWave(ByVal rate As Integer, ByVal bits As Integer, ByVal
channels As Integer)
wFormatTag = 1
nChannels = channels
nSamplesPerSec = rate
wBitsPerSample = bits
cbSize = 0
nBlockAlign = (channels * (bits / 8))
nAvgBytesPerSec = nSamplesPerSec * nBlockAlign
End Sub
End Structure


C# Code
=============================
using System;
using System.Runtime.InteropServices;


namespace WindowsApplication14

{

public class WaveOutTest
{
public const int CALLBACK_FUNCTION = 0x00030000;

// callbacks
public delegate void WaveCallBack(IntPtr hdrvr, int uMsg, int dwUser, int
dwParam1, int dwParam2);

[DllImport("winmm.dll")]
public static extern int waveOutOpen(ref IntPtr hWaveOut, int uDeviceID,
WaveFormat lpFormat, WaveCallBack dwCallback, int dwInstance, int dwFlags);


private WaveCallBack m_BufferProc = new WaveCallBack( WaveOutCallBack);
private WaveFormat m_Format;
private IntPtr m_WaveOut = IntPtr.Zero;

public static void WaveOutCallBack(IntPtr hdrvr, int uMsg, int dwUser, int
dwParam1, int dwParam2)
{
//DO something here.
}

public void Test()
{
m_Format = new WaveFormat(22050, 16,2);
int iReturn = waveOutOpen(ref m_WaveOut, -1, m_Format, m_BufferProc, 0,
CALLBACK_FUNCTION);
// NOTE: Success.
if (iReturn!=0)
throw new Exception("Open Failed!");

}

}


[StructLayout(LayoutKind.Sequential)]
public class WaveFormat
{
public short wFormatTag;
public short nChannels;
public int nSamplesPerSec;
public int nAvgBytesPerSec;
public short nBlockAlign;
public short wBitsPerSample;
public short cbSize;

public WaveFormat(int rate, int bits, int channels)
{
wFormatTag = (short)1;
nChannels = (short)channels;
nSamplesPerSec = rate;
wBitsPerSample = (short)bits;
cbSize = 0;

nBlockAlign = (short)(channels * (bits / 8));
nAvgBytesPerSec = nSamplesPerSec * nBlockAlign;
}
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top