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
Solved

Enable VB.Net callback function to be used by unmanaged C++ DLL

Posted on 2007-03-28
6
895 Views
Last Modified: 2013-11-26
This is VB.Net Question:
I need to set up an asynchronous callback from a DLL (written in C++) to which I documentation for, but do not have visibility into its source code.  

My application must first 'register' the callback function by sending the address of a delegate to the DLL.  Once registered, the callback is intended to then be used one or more times until the application terminates, whenever the dll determines it needs to use it, or until I 'de-register' it (effectively my callback is intended to be an event handler like a 'mouse_click')

Question Part 1.  How do I create the delegate or write the callback function inorder for the DLL to properly use my callback function?

Question Part 2.  In -registering- my callback I use a separate delegate to enable my app to use the 'registration' function in the DLL.  Isn't this (shouldn't it be) a straightforward use of a function in the DLL.  IOWs, I can't see how the registering the callback function would be anything more a function call that does not need a delegate.

Obviously, I'm relatively new to callbacks (and delegates), explanations and code examples would be appreciated.
0
Comment
Question by:brownpeterg
  • 3
  • 2
6 Comments
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 200 total points
ID: 18812923
First you create a Delegate that the matches the signature of the callback.

Let's say your callback had this signature:

    Public Sub Foo(ByVal ID As Integer, ByVal msg As String)

So your Delegate would simply be:

    Public Delegate Sub FooDelegate(ByVal ID As Integer, ByVal msg As String)

Now setup your actual sub that will be the callback...again having the same signature:

    Private Sub MyCallbackSub(ByVal ID As Integer, ByVal msg As String)
        Debug.Print(ID & ": " & msg)
    End Sub

Next you create a variable of type "FooDelegate" and point it to your callback using the AddressOf keyword:

    Dim fd As New FooDelegate(AddressOf MyCallbackSub)

Finally, you pass your Delegate, "fd", into your DLLs register function:

    SomeCppDll.Register(fd)

That should be it...
0
 

Author Comment

by:brownpeterg
ID: 18813569
What you suggest is pretty much what I've done.  Everything compiles but no callbacks occur (I may yet have to use a function that "starts the whole thing", but for discussion sake let me ask this.  I've researched a construct that seems to be related and that I hoped/expected someone might tell me I needed to use here (and how); "IAsyncResult" seems to be related.  Can you confirm it is or is not in this case?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18813822
I'm not sure as I've never used IAsyncResult.

I know you can use the method above as I've used it successfully with the Win API EnumWindows() which uses a callback.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 41

Accepted Solution

by:
graye earned 300 total points
ID: 18822814
Take a look at the following artilce... it pretty much covers exactly what you're trying to do (use delegates with an unmanaged DLL)

http://home.hot.rr.com/graye/Articles/Delgates_AdressOf.htm

You might find it helpful...
0
 

Author Comment

by:brownpeterg
ID: 18829366
Well you were both right (and I was making a dumb mistake).  The error was in passing creation of the prototype (in my code) for the (remote) dll function.  I was declaring the functionpointer "byref" instead of "byval."  
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18829383
Glad you figured it out...  =)
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

839 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