?
Solved

Needs urgent help - Calling a CALLBACK function in the unmanaged dll thro' .net

Posted on 2003-03-30
10
Medium Priority
?
323 Views
Last Modified: 2010-04-15
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,
0
Comment
Question by:WriteSwati
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
10 Comments
 

Author Comment

by:WriteSwati
ID: 8236676
I am developing the application for Pocket PC
0
 

Author Comment

by:WriteSwati
ID: 8237431
I am developing the application for Pocket PC
0
 
LVL 2

Expert Comment

by:kjo4jc
ID: 8262487
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);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:WriteSwati
ID: 8266808
Sorry to say but the solution didnot work for me.
Can you suggest me some other solution?
0
 
LVL 2

Expert Comment

by:kjo4jc
ID: 8266907
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);
0
 

Author Comment

by:WriteSwati
ID: 8267823
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.
0
 

Author Comment

by:WriteSwati
ID: 8268006
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.
0
 
LVL 2

Accepted Solution

by:
kjo4jc earned 300 total points
ID: 8270403
I generated a quick C# Windows App and added the
following code showing how to utilize a delegate as
a callback:

// specify callback as a delegate
public delegate void barf(int x);

// here is an implementation of the delegate
public void mybarfer(int abc)
{
     MessageBox.Show(abc.ToString());
}

// here is a method taking a delegate to invoke
// as a callback
public void invokeBarf( barf barfFunc )
{
     barfFunc(1234);
}
         
private void Form1_Load(object sender, System.EventArgs e)
{
     // create the instance of the delegate
     barf myBarfFunc = new barf(mybarfer);

     // pass the delegate instance to the function
     // making the callback
     invokeBarf( myBarfFunc );
}
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question