Link to home
Start Free TrialLog in
Avatar of WriteSwati
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-function 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-function 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,SizeConst=256 )]    
               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.StringBuilder  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.VoiceRecorder.getGUIStatus(DisplayTime);
               VoiceRecorder.error_proc delegate2;
               delegate2 = new voiceRecorderTest.VoiceRecorder.error_proc(DisplayError);

               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,
Avatar of WriteSwati
WriteSwati

ASKER

I am developing the application for Pocket PC
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(voiceRecorderTest.VoiceRecorder.getGUIStatus);

delegate2 = new error_proc(voiceRecorderTest.VoiceRecorder.error_proc);
Sorry to say but the solution didnot work for me.
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.VoiceRecorder.getGUIStatus(DisplayTime);

VoiceRecorder.error_proc delegate2;
delegate2 = new voiceRecorderTest.VoiceRecorder.error_proc(DisplayError);

VoiceRecorder.setCallbacks(delegate1,delegate2);

to:

getGUIStatus delegate1 = new getGUIStatus(
voiceRecorderTest.VoiceRecorder.getGUIStatus)

error_proc delegate2 = new error_proc(
voiceRecorderTest.VoiceRecorder.error_proc)

VoiceRecorder.setCallbacks(delegate1,delegate2);
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of kjo4jc
kjo4jc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial