Studio:Variant Types in Perl

From STRIDE Wiki
Revision as of 23:53, 20 August 2009 by Timd (talk | contribs) (Text replace - 'Category:Perl Info' to 'Category:Studio:Perl Info')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

All of the STRIDE components are written as Windows COM components. These objects use Variant types to exchange data with scripting languages. When using perl, the Win32::OLE package is responsible for mapping the Variant types into perl types (scalars and arrays).

In a few cases, the translation from Variant to perl scalar is not completely correct. Since perl scalars generally just "do the right thing" when used in various contexts, any anomalies that occur when mapping specific variant types to scalars has little effect. However, some operations in perl actually behave differently on strings vs. numeric values (e.g. bitwise operations). When this occurs, you should be very explicit in the type of operation you want to perform (e.g. you can prepend 0+ to a scalar value in perl when doing bitwise operations to force numeric context).

In some cases, it may be useful to examine the VT type that is being returned by the STRIDE COM objects (such as ascript or reporter). Here is sample code that shows how to extract the VT type for some property on an object:

use strict;
use Carp;
use Win32::OLE;
use Win32::OLE::Variant;
Win32::OLE->Option(Warn => 3);

my $prop = Win32::OLE::Variant->new();
my $user = $ascript->Functions->Item("MyFunction")->User;
$user->Call();
# here, we examine the VT type of the return value
# by fetching the value into variant object $prop
$user->Dispatch('ReturnValue', $prop);
$ascript->MessageBox(sprintf("V_VT(prop)=%d", $prop->Type));

The numeric value returned by the Type property above maps directly to the VARENUM enum type defined by the Microsoft (typically in WTypes.h). As of this writing, the VT Type values are:

enum VARENUM 
{
    VT_EMPTY    = 0,
    VT_NULL    = 1,
    VT_I2    = 2,
    VT_I4    = 3,
    VT_R4    = 4,
    VT_R8    = 5,
    VT_CY    = 6,
    VT_DATE    = 7,
    VT_BSTR    = 8,
    VT_DISPATCH    = 9,
    VT_ERROR    = 10,
    VT_BOOL    = 11,
    VT_VARIANT    = 12,
    VT_UNKNOWN    = 13,
    VT_DECIMAL    = 14,
    VT_I1    = 16,
    VT_UI1    = 17,
    VT_UI2    = 18,
    VT_UI4    = 19,
    VT_I8    = 20,
    VT_UI8    = 21,
    VT_INT    = 22,
    VT_UINT    = 23,
    VT_VOID    = 24,
    VT_HRESULT    = 25,
    VT_PTR    = 26,
    VT_SAFEARRAY    = 27,
    VT_CARRAY    = 28,
    VT_USERDEFINED    = 29,
    VT_LPSTR    = 30,
    VT_LPWSTR    = 31,
    VT_RECORD    = 36,
    VT_INT_PTR    = 37,
    VT_UINT_PTR    = 38,
    VT_FILETIME    = 64,
    VT_BLOB    = 65,
    VT_STREAM    = 66,
    VT_STORAGE    = 67,
    VT_STREAMED_OBJECT    = 68,
    VT_STORED_OBJECT    = 69,
    VT_BLOB_OBJECT    = 70,
    VT_CF    = 71,
    VT_CLSID    = 72,
    VT_VERSIONED_STREAM    = 73,
    VT_BSTR_BLOB    = 0xfff,
    VT_VECTOR    = 0x1000,
    VT_ARRAY    = 0x2000,
    VT_BYREF    = 0x4000,
    VT_RESERVED    = 0x8000,
    VT_ILLEGAL    = 0xffff,
    VT_ILLEGALMASKED    = 0xfff,
    VT_TYPEMASK    = 0xfff
};