Share via


Creating User-Defined Types for DLL Functions

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

A user-defined type is a data structure that can store multiple related variables of different types. It corresponds to a structure in C and 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. In addition, 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.

The following code fragment shows how to define the RECT user-defined type, which you use with several Microsoft® 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

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 code fragment, 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

See Also

What Is an API? | Constants and User-Defined Types | Defining Constants for DLL Functions | Passing User-Defined Types