Search code examples
vbscriptenumsautomationidl

Using an enum from a type library in VBScript


I'm implementing a COM interface for an existing application written in C++. The COM interface is used for automating the application from VBScript.

One of the methods I want to call via th COM interface has a parameter that, in C++, has an enum type. I have defined a corresponding enum type in the IDL file, and oleview shows the values are registered:

// Copied from type library viewer    
typedef enum
{
   MyValueA = 0,
   MyValueB = 1,
   MyValueC = 2
} MyEnum;

However, when I pass one of these values in VBScript, the value received by the CPP implementation is always 0. I assume I'm not using the correct VBScript syntax. Passing an integer value directly works, and passing something random (like ghfitgr) also results in 0, which is probably what is happening to MyValueB, etc.

I found a claim that enum.member should be used, which would be MyEnum.MyValue, but that results in a syntax error (object required: MyEnum). What am I doing wrong?


Solution

  • Late-bindng VBScript can't/won't pluck those info from the .dll, all you get are objects (by CreateObject()) and what they provide. So spare yourself a lot of hassle & hacks by defining the values with decent names using Const.

    To clarify:

    I meant: Const in the VBScript code.