Chapter 27: Using the Windows Application Programming Interface
945
Sometimes you’ll encounter situations where a function uses a data type that you aren’t familiar
with or may not have even heard of. Most likely, the parameter being passed is a data structure,
which is a fancy term for “user-defined data type.” If that term sounds familiar, then you’re proba-
bly thinking about the Access VBA Type statement. Data structures are usually a collection of
fields allocated contiguously (next to each other in memory). The Type statement in Access VBA is
compatible with its C/C++ Struct counterpart, as long as the fields declared within the structure
are compatible. Listing 27.3 shows the data structure passed in the GetVersionEx API and its
Access VBA equivalent.
LISTING 27.3
OSVERSIONINFO Structure
‘C-type OSVERSIONINFO structure syntax
typedef struct _OSVERSIONINFO{
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwBuildNumber;
DWORD dwPlatformId;
TCHAR szCSDVersion[ 128 ];
} OSVERSIONINFO;
‘VBA-typedef for OSVERSIONINFO
Private Type OSVERSIONINFO
dwVersionInfo As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwplatformID As Long
szVersion As String * 128
End Type
Recognizing what you can’t do with the API
I hesitate to place anything in this paragraph, because as soon as I do, someone will find a way to
prove me wrong. Programmers are an inventive bunch. But a few things are difficult to do when
converting APIs to VBA. One is converting APIs that make use of a Callback function.
Sometimes APIs make calls to other functions that process messages and then return values back to
the calling API. To do this, you must be able to pass the address of the entry point of the function.
Access doesn’t provide a way to handle this situation. However, some people have developed their
own DLLs that specifically handle these types of situations. Never say never.