?
Solved

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

Posted on 2007-03-28
6
Medium Priority
?
1,007 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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 800 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
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.

 
LVL 41

Accepted Solution

by:
graye earned 1200 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
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.
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

757 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