WriteSwati
asked on
Needs urgent help - Calling a CALLBACK function in the unmanaged dll thro' .net
Hello,
I am using an unmanaged dll in my c#.net project.The
dll uses some callbackfunctions which retrieve the
status of the file stored.I am new to delegates.Can
anyone provide me a solution for how should I use
those callback functions in my project using
delegates?
I am pasting the information of callback functions below:
-------------------------- ---------- ---------- ---
typedef void (*GUI_UPDATE_PROC) ( GUIStatus )
Callback function to receive info about the
VoiceRecorder status. Called everytime the status of
the VoiceRecorder changes in a way that is important
to know for the main application.
To prevent deadlocks, you must not call a
VOICERECORDERLIB_API-funct ion from this callback.
typedef void (*ERROR_PROC) ( const WCHAR* )
Callback function that is called if an error occurs.
The WCHAR pointer must not be deleted or modified. It
contains an error message which can be used to inform
the user why the recorder stopped.
All errors are handled internally (resetting audio
hardware, stopping etc.), this function is mainly
intended to inform the user.
To prevent deadlocks, you must not call a
VOICERECORDERLIB_API-funct ion from this callback.
VOICERECORDERLIB_API void setCallbacks(
GUI_UPDATE_PROC guiProc, ERROR_PROC errorProc )
This function sets the callbacks. Any of the
parameters may be 0.
-------------------------- ----------
Please suggest me how should I call the above three
functions in .net
GUIStatus is the structure defined in the dll.
The structure is as follows:
struct GUIStatus
{
DWORD _dwStatus;
DWORD _dwPositionSecs;
DWORD _dwLengthSecs;
DWORD _dwPositionPercent;
WCHAR _strFilename[MAX_PATH];
bool _bEnabled[5];
bool _bDirty[10];
};
I am converting the structure as below in .net:
-------------------------- ---------- ---
[StructLayout( LayoutKind.Sequential)]
public class GUIStatus
{
//const int Max_Path = 256;
public int _dwStatus ;
public int _dwPositionSecs;
public int _dwLengthSecs;
public int
_dwPositionPercent;
//public char [] _strFilename
= new char[Max_Path];
/* public
System.Text.StringBuilder _strFilename = new
StringBuilder(256);
public int [] _bEnabled = new
int[5];
public int [] _bDirty = new
int[10] ;*/
[ MarshalAs(
UnmanagedType.ByValTStr,Si zeConst=25 6 )]
public String _strFileName;
public int [] _bEnabled = new
int[5];
public int [] _bDirty = new
int[10] ;
};
-------------------------- ---
and the delegates I am defining as below for the call back functions listed above:
-------------------------- -------
public delegate void getGUIStatus(GUIStatus status);
public delegate void error_proc(System.Text.Str ingBuilder strErr);
private void button1_Click(object sender, System.EventArgs e)
{
string strFile = "\\My documents\\foo.wav";
VoiceRecorder.Open(strFile );
VoiceRecorder.Play();
VoiceRecorder.getGUIStatus delegate1;
delegate1 = new voiceRecorderTest.VoiceRec order.getG UIStatus(D isplayTime );
VoiceRecorder.error_proc delegate2;
delegate2 = new voiceRecorderTest.VoiceRec order.erro r_proc(Dis playError) ;
VoiceRecorder.setCallbacks (delegate1 ,delegate2 );
}
-------------------------- -------
please check where I am going wrong.
I would be very much pleased on receipt of the help.
Thanks in advance
Regards,
I am using an unmanaged dll in my c#.net project.The
dll uses some callbackfunctions which retrieve the
status of the file stored.I am new to delegates.Can
anyone provide me a solution for how should I use
those callback functions in my project using
delegates?
I am pasting the information of callback functions below:
--------------------------
typedef void (*GUI_UPDATE_PROC) ( GUIStatus )
Callback function to receive info about the
VoiceRecorder status. Called everytime the status of
the VoiceRecorder changes in a way that is important
to know for the main application.
To prevent deadlocks, you must not call a
VOICERECORDERLIB_API-funct
typedef void (*ERROR_PROC) ( const WCHAR* )
Callback function that is called if an error occurs.
The WCHAR pointer must not be deleted or modified. It
contains an error message which can be used to inform
the user why the recorder stopped.
All errors are handled internally (resetting audio
hardware, stopping etc.), this function is mainly
intended to inform the user.
To prevent deadlocks, you must not call a
VOICERECORDERLIB_API-funct
VOICERECORDERLIB_API void setCallbacks(
GUI_UPDATE_PROC guiProc, ERROR_PROC errorProc )
This function sets the callbacks. Any of the
parameters may be 0.
--------------------------
Please suggest me how should I call the above three
functions in .net
GUIStatus is the structure defined in the dll.
The structure is as follows:
struct GUIStatus
{
DWORD _dwStatus;
DWORD _dwPositionSecs;
DWORD _dwLengthSecs;
DWORD _dwPositionPercent;
WCHAR _strFilename[MAX_PATH];
bool _bEnabled[5];
bool _bDirty[10];
};
I am converting the structure as below in .net:
--------------------------
[StructLayout( LayoutKind.Sequential)]
public class GUIStatus
{
//const int Max_Path = 256;
public int _dwStatus ;
public int _dwPositionSecs;
public int _dwLengthSecs;
public int
_dwPositionPercent;
//public char [] _strFilename
= new char[Max_Path];
/* public
System.Text.StringBuilder _strFilename = new
StringBuilder(256);
public int [] _bEnabled = new
int[5];
public int [] _bDirty = new
int[10] ;*/
[ MarshalAs(
UnmanagedType.ByValTStr,Si
public String _strFileName;
public int [] _bEnabled = new
int[5];
public int [] _bDirty = new
int[10] ;
};
--------------------------
and the delegates I am defining as below for the call back functions listed above:
--------------------------
public delegate void getGUIStatus(GUIStatus status);
public delegate void error_proc(System.Text.Str
private void button1_Click(object sender, System.EventArgs e)
{
string strFile = "\\My documents\\foo.wav";
VoiceRecorder.Open(strFile
VoiceRecorder.Play();
VoiceRecorder.getGUIStatus
delegate1 = new voiceRecorderTest.VoiceRec
VoiceRecorder.error_proc delegate2;
delegate2 = new voiceRecorderTest.VoiceRec
VoiceRecorder.setCallbacks
}
--------------------------
please check where I am going wrong.
I would be very much pleased on receipt of the help.
Thanks in advance
Regards,
ASKER
I am developing the application for Pocket PC
you are really close. but when you are passing a delegate, you need to instantiate an instance of the actual delegate instead.
try this:
delegate1 = new getGUIStauts(voiceRecorder Test.Voice Recorder.g etGUIStatu s);
delegate2 = new error_proc(voiceRecorderTe st.VoiceRe corder.err or_proc);
try this:
delegate1 = new getGUIStauts(voiceRecorder
delegate2 = new error_proc(voiceRecorderTe
ASKER
Sorry to say but the solution didnot work for me.
Can you suggest me some other solution?
Can you suggest me some other solution?
In C# delegates replace the function pointers we all came to know and love from C.
So when you want to pass a method to some other object as a callback, you have that object require a delegate.
When passing the appropriate function (whose signature
must match the delegate) you send it as new instance of the delegate.
try changing:
VoiceRecorder.getGUIStatus delegate1;
delegate1 = new voiceRecorderTest.VoiceRec order.getG UIStatus(D isplayTime );
VoiceRecorder.error_proc delegate2;
delegate2 = new voiceRecorderTest.VoiceRec order.erro r_proc(Dis playError) ;
VoiceRecorder.setCallbacks (delegate1 ,delegate2 );
to:
getGUIStatus delegate1 = new getGUIStatus(
voiceRecorderTest.VoiceRec order.getG UIStatus)
error_proc delegate2 = new error_proc(
voiceRecorderTest.VoiceRec order.erro r_proc)
VoiceRecorder.setCallbacks (delegate1 ,delegate2 );
So when you want to pass a method to some other object as a callback, you have that object require a delegate.
When passing the appropriate function (whose signature
must match the delegate) you send it as new instance of the delegate.
try changing:
VoiceRecorder.getGUIStatus
delegate1 = new voiceRecorderTest.VoiceRec
VoiceRecorder.error_proc delegate2;
delegate2 = new voiceRecorderTest.VoiceRec
VoiceRecorder.setCallbacks
to:
getGUIStatus delegate1 = new getGUIStatus(
voiceRecorderTest.VoiceRec
error_proc delegate2 = new error_proc(
voiceRecorderTest.VoiceRec
VoiceRecorder.setCallbacks
ASKER
Hello,
Can I send you the the .cs file I am developing? as I am getting error with the solution you have suggested.
Also please clear me whether the compact framework is supporting delegates for callback.I am developping the application in compact framework.
Awaiting for your reply.
Can I send you the the .cs file I am developing? as I am getting error with the solution you have suggested.
Also please clear me whether the compact framework is supporting delegates for callback.I am developping the application in compact framework.
Awaiting for your reply.
ASKER
Hello,
Can I send you the the .cs file I am developing? as I am getting error with the solution you have suggested.
Also please clear me whether the compact framework is supporting delegates for callback.I am developping the application in compact framework.
Awaiting for your reply.
Can I send you the the .cs file I am developing? as I am getting error with the solution you have suggested.
Also please clear me whether the compact framework is supporting delegates for callback.I am developping the application in compact framework.
Awaiting for your reply.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER