Encapsulating the DLL

When you encapsulate DLL function calls in a class module, you are defining a VBA interface for those functions. Often, this is called "wrapping" a function. You do not change the function itself, but you provide an easier way to call it. Although it can be a little more work to wrap DLL functions, you will benefit in the long term if you call the functions repeatedly.

The Property Get procedure for each of the Year, Month, Day, Hour, Minute, and Second properties calls the GetLocalTime function, which returns the current value for each portion of the local time into the SYSTEMTIME data structure. To make sure the value returned by the Property Get procedure is current, GetLocalTime is called each time the Property Get procedure runs.

The Property Let procedure for each property also calls GetLocalTime to make sure the values in the data structure are current. Then, it updates the appropriate element of the structure and passes the entire structure to SetLocalTime to change the local system time.

The Property Get and Property Let procedures for the Hour property are shown in Passing User-Defined Types.

When you have created an object-oriented interface for DLL functions, you can create a new instance of the wrapper class and work with its properties and methods. The following example uses the Hour, Minute, and Second properties of the System object to display the local system time in a dialog box:

Sub DisplayLocalSystem()
   Dim sysLocal As System
   Dim strMsg As String
   Dim dteTime  As Date
   
   ' Create new instance of System class.
   Set sysLocal = New System
   
   With sysLocal
      ' Return date value by using Hour, Minute, and
      ' Second properties of System object.
      dteTime = TimeSerial(.Hour, .Minute, .Second)
      strMsg = "The current local system time is: " _
         & CStr(dteTime)
      MsgBox strMsg
   End With
End Sub

You can expand the System class to contain other system-related properties and methods. For example, you might add additional properties that call the GetSystemMetrics function with different constants.

See Also

Wrapping DLL Functions | What Is a Callback Function? | Creating a Callback Function | Calling a Callback Function