Solved

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

Posted on 2007-03-28
6
854 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now