PC Review


Reply
Thread Tools Rate Thread

CallBack Function In Win API

 
 
=?Utf-8?B?UWluZ2RvbmcgWi4=?=
Guest
Posts: n/a
 
      14th Jun 2006
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;
}
}

}

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Old style API function callback conversion to managed C++ harishashim@gmail.com Microsoft VC .NET 7 30th Aug 2006 05:01 AM
callback function in AVISAVE API Subhash Bhartiya via .NET 247 Microsoft VB .NET 1 1st Sep 2004 11:17 AM
API GetTickCount and API for sound Prasad Vanka Microsoft Excel Programming 1 5th Aug 2004 01:17 PM
SOAP, google api, yahoo api and code example wanted Jack-of-all-traits Microsoft Access 0 26th Feb 2004 05:33 AM
Readprinter Api and Enumprinter Api =?Utf-8?B?QWJ1YmFrYXI=?= Microsoft Dot NET 0 20th Feb 2004 05:56 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:31 AM.