5.1.0.1 Opaque Handles and Private Data Structures
Opaque handles are used in the TSPI to refer to the data structures representing lines, phones and call appearances. These appear as parameters to most of the functions and callbacks. There are few functions that refer to the device using a device ID instead of an opaque handle. In such a case, the reference is to the device itself rather than to a data structure. Functions that work in this fashion are typically those called at early initialization times before data structures have been created and opaque handles have been exchanged.
Within the set of public data structures used to pass information between the TAPI DLL and the Service Provider on behalf of an application, there are some large ones with a complex logical structure. All of these have their logical structure "flattened" into a linear physical layout. This means that data structures do not contain pointers to substructures that contain variable size components of information. Instead, data structures with such variable sized parts use the following meta structure:
DWORD dwTotalSize; dwNeededSize; dwUsedSize;
<fixed size fields>
DWORD dw<VarSizeField1>Size; dw<VarSizeField1>Offset;
<fixed size fields>
DWORD dw<VarSizeField2>Size; dw<VarSizeField2>Offset;
<common extensions>
<var sized field1>
<var sized field2>
The dwTotalSize field is the size in bytes allocated to this data structure, it marks the end of the data structure and is set by the application before it invokes the API function and eventually the TSPI function that uses this data structure. The Service Provider must not read or write beyond this size.
The dwNeededSize field is filled in by the Service Provider. It indicates how many bytes are needed to return all the information requested. The existence of variable size fields makes it often impossible for the application to a priori estimate the data structure size it needs to allocate. This field simply returns the number of bytes really needed for the information. This number could be smaller, equal, or larger than dwTotalSize, and includes the space for the dwTotalSize field itself. If larger, then the returned structure will only be partially filled. If the information fields the app is interested in are available in the partial structure, then nothing else need to be done. Otherwise, the app typically allocates a structure of size at least dwNeededSize and invokes the operation again. Usually, enough space will be available this time to return all the information, although it is possible the size could have increased again.
The dwUsedSize field is filled in by the Service Provider if it returns information to the application to indicate the actual size in bytes of the data structure that contains useful information. If, for example, a structure was allocated too small and the truncated field is a variable size field, then dwNeededSize will be larger than dwTotalSize, and the truncated field will be left empty. dwUsedSize could therefore be smaller than dwTotalSize. Partial field values are not returned.
Following this header is the fixed part of the data structure. It contains regular fields and size/offset pairs that describe the actual variable sized fields. The offset field contains the offset in bytes of the variable sized field from the beginning of the record. The size field contains the size in bytes of the variable sized field. If a variable sized field is empty, then the size field is zero and the offset is set to zero. Variable sized field that would be truncated if the total structure size is insufficient, are left empty. I.e., their size field is set to zero and the offset is set to zero. The variable sized fields follow the fixed fields.