Solved

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

Posted on 2007-03-28
6
910 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
[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
  • 3
  • 2
6 Comments
 
LVL 86

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 86

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 86

Expert Comment

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

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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 NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

726 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