Microsoft Office 2000/Visual Basic Programmer's Guide   

Creating User-Defined Types for DLL Functions

A user-defined type is a data structure that can store multiple related variables of different types. It corresponds to a structure in C/C++. In some cases you pass an empty user-defined type to a DLL function, and the function fills in the values for you; in other cases you fill the user-defined type from VBA, and pass it to the DLL function.

You can think of a user-defined type as a chest of drawers. Each drawer can contain different types of items, but together they can be treated as a single chest of related items. And you can retrieve an item from any drawer without worrying about the items stored in any other drawer.

To create a user-defined type, use the Type…End Type statement. Within the Type…End Type statement, list each element that is to contain a value, along with its data type. An element of a user-defined type can be an array.

The following code fragment shows how to define the RECT user-defined type, which you use with several Windows API functions that manage rectangles on the screen. For example, the GetWindowRect function takes a data structure of type RECT, and fills it with information about a window's left, top, right, and bottom positions.

Type RECT
      Left As Long
      Top  As Long
      Right  As Long
      Bottom As Long
End Type

In order to pass a user-defined type to a DLL function, you must create a variable of that type. For example, if you were planning to pass a user-defined type of type RECT to a DLL function, you could include a variable declaration such as the following in the module:

Private rectWindow As RECT

You can refer to an individual element within the user-defined type as shown in the following code fragment:

Debug.Print rectWindow.Left

For more information about passing user-defined types to DLL functions, see "Passing User-Defined Types" later in this chapter.