Solved

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

Posted on 2007-03-28
6
883 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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 connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

809 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