Re: VBScript OCX receive and supply a safeArray of type VT_UI1- problem

JJ <[email protected]>
Newsgroups alt.comp.os.windows-xp,alt.windows7.general,comp.os.ms-windows.programmer.win32
Organization To protect and to server
Message-ID <[email protected]>
On Tue, 21 May 2024 20:44:34 +0200, R.Wieser wrote:
> Hello all,
> 
> I've just build an OCX for usage within vbscript, and am running into a 
> problem :
> 
> I'm trying to have a method #1 return a SafeArray of type VT_UI1, and later 
> provide it to a method #2.
> 
> The problem is that when I do this :
> 
> Object.Method2(Object.Method1())
> 
> everything works.
> 
> But when I use an intermediate variable like this
> 
> Data = Object.Method1()
> Object.Method2(Data)
> 
> VBScript throws a "Type mismatch" error - and I have no idea why or how to 
> solve it. :-(
> 
> Declarations of both methods in the IDL file :
> 
> HRESULT Method1([out, retval] SAFEARRAY(unsigned char)* Result);
> HRESULT Method2([in] SAFEARRAY(unsigned char) ByteData);
> 
> By the way:
> 
> wscript.echo vartype(Object.Method1())
> Data = Object.Method1()
> wscript.echo vartype(Data)
> 
> both of the above return &H2011 (array of VT_UI1)
> 
> Help ?
> 
> Regards,
> Rudy Wieser

It's mainly because for array, VBScript only have native support for array
of variant. It has no native support for array of non-variant. Meaning that,
it has no built-in conversion between array of variant and array of
non-variant.

In case of array of integers generated by VBScript itself, they're stored as
array of variants where those variants contain an int. VBScript by itself,
can't directly create an array of non-variant. Only array of variant.

To workaround the problem, store the array of non-variant result from
`Method1()` into VBScript generated array first, then store that VBScript
generated array into a variable. To use the result, pass the VBScript
generated array's element (i.e. the array of non-variant) to `Method2()`.
e.g.

Data = array(Object.Method1())
Object.Method2(Data(0))

Related to VBScript limitation...
The `byval` and `byref` keywords are not available for explicitly passing a
value to a method as a copy or a reference. They're only for
subroutine/function declaration. So VBScript by default will pass value to
methods as a copy rather than a reference. Only if the method argument type
was specifically declared as a reference (i.e. only accept a reference),
VBScript will pass a value as a reference.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.