Working with Constants and Types
Some DLLs require the use of constants or user-defined types, otherwise known as structures or parameters . You must place these in the General Declarations section of your module, along with the Declare statements you have defined.
Working with Constants
Constants are used by many of the API functions. They provide you with an English-like way of sending required values to an API function. You use the constant as an alias for a specific value. Here's an example:Global Const SM_CXSCREEN = 0
Global Const SM_CYSCREEN = 1
You place the constant declarations and function declarations in the General Declarations section of a module. When the GetSystemMetrics function is called in the following example, the SM_CXSCREEN and SM_CYSCREEN constants are passed as arguments to the function:Sub GetScreenInfo()
MsgBox "Screen Resolution is : " & _
GetSystemMetrics(SM_CXSCREEN) & _
" By " & _
GetSystemMetrics(SM_CYSCREEN)
End Sub
When the code in the example passes the SM_CXSCREEN constant to the GetSystemMetrics function, the function returns the horizontal screen resolution; when the code passes the SM_CYSCREEN constant to the function, the code returns the vertical screen resolution.
Working with Types
When working with types, you first must declare the type in the General Declarations section of a module. You then can pass elements of a user-defined type, or you can pass the entire type as a single argument to the API function. The following code shows an example of a Type declaration:Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
strReserved As String * 128
End Type
You declare the Type structure OSVERSIONINFO in the General Declarations section of the module, as shown in Listing 23.1.
Listing 23.1 Declaring the Type Structure OSVERSIONINFO in the General Declarations Section of the Module
Function GetOSInfo()
Dim OSInfo As OSVERSIONINFO
Dim strMajorVersion As String
Dim strMinorVersion As String
Dim strBuildNumber As String
Dim strPlatformId As String
' Set the length member before you call GetVersionEx
OSInfo.dwOSVersionInfoSize = Len(OSInfo)
If GetVersionEx(OSInfo) Then
strMajorVersion = OSInfo.dwMajorVersion
strMinorVersion = OSInfo.dwMinorVersion
strBuildNumber = OSInfo.dwBuildNumber And &HFFFF&
strPlatformId = OSInfo.dwPlatformId
MsgBox "The Major Version Is: " & _
strMajorVersion & vbCrLf & _
"The Minor Version Is: " & strMinorVersion & vbCrLf & _
"The Build Number Is: " & strBuildNumber & vbCrLf & _
"The Platform ID Is: " & _
IIf(strPlatformId = 1, "Win 95 or Win 98", _
"Win NT") & vbCrLf
End If
End Function
In this listing, the statement Dim OSInfo As OSVERSIONIFO creates a Type variable. The entire structure is passed to the GetVersionEx function (declared in basAPICalls), which fills in the elements of the structure with information about the operating system. The code retrieves and stores this information into variables that it displays in a message box.