How Do I Extract/Set Data From/To a Variant Type of a COM Object? (Magic xpa 4.x)
Many COM objects use the VT_VARIANT data type. The definition of the VT_VARIANT, is, in essence, that it can hold any data type. However, the object you are using will be expecting data of a certain format for a specific function.
Usually, Magic xpa will handle the conversions automatically. If you know that a certain VT_VARIANT parameter is expecting an array, for instance, and you set up a vector, you just pass the vector in and Magic xpa does the rest (see How Do I Send and Retrieve Array Values from COM Objects? for an example). Similarly, if the object is sending back a negative integer in a VT_VARIANT, and you accept the parameter in a numeric variable that allows negatives, Magic xpa will handle the underlying conversion correctly.
However, if you need a more precise level of control, you can use a BLOB data type to pass data back and forth, and use the Variant functions to set up exactly the type of variant you want to send. You can also use the same functions to extract data from a variant that is sent back from the object.
Here is a summary of the functions used.
You can use the VariantCreate() function to populate the variant BLOB. In this example, we used VariantCreate() to move a Date and a Time into the VT_DATE variant, which can actually hold a date/time stamp.
The syntax of VariantCreate() is:
VariantCreate(VT Type, Value, Time value)
where:
-
VT Type: A number representing the data type. For instance, in our example, 7 represents the date type. See Variant Data Types for a list of the types.
-
Value: The value you are putting into the variant. This could be any data type, a date, string, number, or BLOB.
-
Time value: (optional) This allows you to move a time into a VT_Date type. In this example, we pass in a Time as the third parameter.
The syntax of VariantGet() is:
VariantGet(Variant Value, Attribute)
where:
In this example, the variant is a VT_DATE type, which holds both a Date and a Time, so we can extract both from the same variant using VariantGet().
|
|
A
|
Alpha
|
N
|
Numeric
|
L
|
Logical
|
D
|
Date
|
T
|
Time
|
B
|
Boolean
|
U
|
Unicode
|
Here is a list of the data type that are used in the Variant functions.
|
|
|
|
0
|
VT_EMPTY
|
No value specified
|
|
1
|
VT_NULL
|
SQL-style Null
|
|
2
|
VT_I2
|
Signed 2-byte integer
|
–32,768 to 32,767
|
3
|
VT_I4
|
Signed 4-byte integer
|
–2,147,483,648 to 2,147,483,647
|
4
|
VT_R4
|
Signed 4-byte real
|
1.1E -38 to 3.4E +38 (7 digits)
|
5
|
VT_R8
|
Signed 8-byte real
|
2.2E -308 to 1.7 E +308 (15 digits)
|
6
|
VT_CY
|
Currency
|
|
7
|
VT_DATE
|
Date
|
|
8
|
VT_BSTR
|
Automation string
|
|
9
|
VT_DISPATCH
|
A pointer to an object that implements IDispatch
|
|
10
|
VT_ERROR
|
SCODE
|
|
11
|
VT_BOOL
|
Boolean
|
|
12
|
VT_VARIANT
|
|
|
13
|
VT_UNKNOWN
|
A pointer to an object that implements IUnknown
|
|
14
|
VT_DECIMAL
|
Decimal
|
|
16
|
VT_I1
|
1-byte character
|
–128 to 127
|
17
|
VT_UI1
|
Unsigned 1-byte character
|
0 to 255
|
18
|
VT_UI2
|
Unsigned 2-byte integer
|
0 to 65,535
|
19
|
VT_UI4
|
Unsigned 4-byte integer
|
0 to 4,294,967,295
|
22
|
VT_INT
|
Signed machine integer
|
|
23
|
VT_UINT
|
Unsigned machine integer
|
|
36
|
VT_RECORD
|
User defined type
|
|
8192
|
VT_ARRAY
|
|
An array of data type
|
16384
|
VT_BYREF
|
|
A reference to data type
|