简体   繁体   中英

Can type mismatch error from VBScript caused by assessing non-variant array returned by COM interface be resolved by changing languages?

I am using a windows application that provides a COM interface for extension scripts. The scant documentation provides a few examples in VBScript so I started implementing my automation in VBScript.

This would look something like:

Set App = GetObject(, "MYAPP.Application")
MyObj = App.Object
MyArr = MyObj.ArrData

IsArray(MyArr)  ' True
TypeName(MyArr) ' Long()
Lbound(MyArr)   ' 0
Ubound(MyArr)   ' 10
MyArr(1)        ' Type Mismatch Error

On closer inspection, the array object is of type Long() (Array of Longs). After researching the problem, it is my understanding that VBScript cannot access elements of an array by index unless the array is of type Variant; that this is a limitation of VBScript as a language. I am not able to implement changes as in this answer because I don't have source access to the application I am trying to extend.

Is the error I am seeing purely a limitation of the language? Will I be able to access the array variables returned by the COM interface by porting my code to another language like C#, for example?

I have confirmed that porting to C# solves the type mismatch error encountered when accessing non-variant array data returned by the COM interface without requiring any change to the COM server.

After including the appropriate references, the code becomes:

var app = (MYAPP.Application)Microsoft.VisualBasic.Interaction.GetObject(null, "MYAPP.Application");
var myObj = (MYAPP.Obj) app.GetObject();
var myArr = (int[]) myObj.GetArrData();
if (myArr.Length() > 0)
{
    System.Console.WriteLine(myArr[0]); // prints value at index 0 without error
}

The limitation is therefore with VBScript; the type mismatch is due to the language being able to handle only variant type arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM