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
326 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
[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
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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