Solved

What are the Constructs for ~Receiving~ Asynchronous Callback Delegate(?)/Function Ptr(?) data in a DLL/API Written in VB.Net;

Posted on 2008-10-23
1
322 Views
Last Modified: 2012-06-21
I need to know the VB.NET language constructs (example code would be appreciated) to use in an DLL that will emulate a 3rd Party API.  (I'm writting an App that interfaces with the API and need to write my own DLL/API to test certain scenarios that I can't simulate with the 3rd party API).  The 3rd Party API is written in C++ or Delphi(?) and I have successfully written code in my App to interface with it.

I ~DO NOT NEED~ information as to how to interface with the API.

I ~DO NEED~ to know how to accept delegate(?) / function pointer(?) data in my DLL/API emulator and then use that captured delegate(?)/function pointer(?) data in the DLL/API emulator to make 'event-type' calls back to my app and to the delegate(?) callback functions therein, asynchronously.

Thanks!!
0
Comment
Question by:brownpeterg
1 Comment
 

Accepted Solution

by:
brownpeterg earned 0 total points
ID: 22900833
OK so no one wanted to take a stab at this one, so I had to figure it out myself.  Turns out it was not that hard (it never is when you know the answer!), but I set out to try trial and error approach since none of the usual internet sources were much help.  

Hope this helps someone else in the same boat as I was!!

Here's a function written for inside the API, that can be called from the calling program.  The function will "register" multiple Callback Delegates based on the "callBackID" variable used in the calling program.  The first part of the answer to the question is simple the use of the type [Delegate].  This is as equivalent to the C++ function pointer as I have been able to determine.  The Delegate (pointer) sent from the calling program represents any/multiple callback functions in the calling program and each of them may have different numbers and types of function parameters.  The delegate declarations may indicate a generic ([Delegate]) type or may indicate a specific function type with a specific function prototype (there is no equivalent term to 'function prototype' in VB.NET that I am aware of so I use C language terminology here).  I only show two of the declarations below.

Please note this is not tested code, but a translated/simplified code snippet, which may be incomplete, and may not work without some tweaking.  The syntax of individual code lines is correct and does work, but the relationships between classes and imported modules may not be.  (I add this disclaimer here because I hate it when folks provide code examples and lead you to believe they should work just by copying them into your code)

Imports AnotherClass.Delegates
Public Class DLLDelegates
      Delegate Sub sendLogDataDelegate(ByRef TrueFalse As Boolean, ByRef logDataString As String)
      Public Shared LogDataDelegate As myLogDataDelegate '<-note declaration of this delegate is not shown and would exist in the "AnotherClass.Delegates" class
      Public Shared ForcedLogOutDelegate As [Delegate]
End Class

Imports MyAPI.DLLDelegates
Imports MyAPI.APIControl
Public Class APIFunctions
     Public Function msRegisterCallBack(ByRef callBackID As Long, ByVal functionPTR As [Delegate])
             Select Case callBackID
                 Case 0
                      LogDataDelegate = functionPTR
                      sendLogData("xxxxx msRegisterCallBack/LogDataDelegate registered")
                 Case ptLogonStatus
                      LogOnStatusDelegate = functionPTR
                      sendLogData("xxxxx msRegisterCallBack/ptLogonStatus registered")
                 Case ptForcedLogout
                      ForcedLogOutDelegate = functionPTR
                      sendLogData("xxxxx msRegisterCallBack/ptForcedlogout registered")
                 Case ptDataDLComplete
                      DataDLCompleteDelegate = functionPTR
                      sendLogData("xxxxx msRegisterCallBack/ptDataDLComplete registered")
                 Case ptMemoryWarning
                      MemoryWarningDelegate = functionPTR
                      sendLogData("xxxxx msRegisterCallBack/ptMemoryWarning registered")
                 Case Else
                      sendLogData("Found No Callback for Sent CallBackID = " & callBackID)
                      Return 0
             End Select
             Return 0
    End Function
End Class

The next part of my question, how do you create an asynchronous callback to the calling program is answered by the following example code used to create a log data entry.
This is not a very good program architecure I know, but it is useful to exemplify both ends of what is required to first 'register' a callback function in the calling program -as in the above snippet- and then use it -as in the below snippet-.  
For this example I am registering a call back to function that simply writing/appending text to a log file.  This function exists in the calling program.  But through the callback to that function, from the API, I am simply sending text to that function.

Imports MyAPI.DLLDelegates
Imports System.Windows.forms
Public Class APIControl
     Public Shared Sub sendLogData(ByRef strLogData As String)
             Dim ctrl As Control
             ctrl = LogDataDelegate.Target
             ctrl.BeginInvoke(LogDataDelegate, New Object() {True, strLogData})
     End Sub
End Class
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

759 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

19 Experts available now in Live!

Get 1:1 Help Now