?
Solved

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

Posted on 2003-03-30
10
Medium Priority
?
326 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
  • 5
  • 3
8 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

571 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