Visual Basic

Using a Single Entry Point

One of the most common ways to call into a DLL is to use a single entry point and a service identifier. The service identifier specifies what action to take on a set of parameters. It's like having several routines wrapped into one. Here's some Visual B++ code (better than pseudocode even!) to give you an idea of what this type of call might look like:

Const SERVICE_1 As Integer = 1
  Const SERVICE_2 As Integer = 2
  Private Sub Command1_Click()
      Call Service(SERVICE_1, 1, 2, 3, 4)
      Call Service(SERVICE_2)
  End Sub
  Public Function Service( _
                           ByVal nService As Integer _
                         , ParamArray Params() As Variant _
                         ) As Boolean
      Dim nRequiredParams As Integer
      Service = True
      Select Case nService
          Case SERVICE_1:
              nRequiredParams = 4
              GoSub TestParams:
          Case SERVICE_2:
              nRequiredParams = 0
              GoSub TestParams:
          Case Else:
              Service = False
      End Select
      Exit Function
  TestParams:
      If (UBound(Params) + 1) < nRequiredParams Then _
      Err.Raise Number:=5
      Return
  End Function

You can see that the Service routine can handle practically any number of requests. Also, because the routine accepts a variable number and type of parameters (because it's using a parameter array), it's very flexible. Notice too that we're checking that each service is being passed the number of arguments it expects to be working with.

The beauty of coding entry points like this is that to add another virtual entry point, all you have to do is add another service-the interface to the DLL remains the same.

How do you know whether a Service routine supports a certain service request before you call it? You probably don't need to know because the routine simply takes no action except to set the Service routine's return value to False. Therefore, the only time it can be False is if a service request fails. Of course, many alternative strategies exist for determining whether the Service routine is current enough to be useful to you, from checking its version number to having it check a passed GUID with one of its own to verify that you're not going to be disappointed.

by BrainBellupdated
Advertisement: