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

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!!
brownpetergAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brownpetergAuthor Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.