VB6 Interop and TlbImp vs AddRef


I’d always assumed that creating a Runtime Callable Wrapper by adding a reference to the COM component in Visual Studio would generate the same interop code as was generated by using the command line tool TlbImp. That would seem not to be true in at least one case that I’ve discovered, take this VB6 method code for example:

   1:  Public Function ProcessArray(ByRef numbers() As Integer)
   2:      Dim biggest As Integer
   3:      r = 0
   4:      For i = 0 To UBound(numbers)
   5:          If numbers(i) > r Then
   6:              r = numbers(i)
   7:          End If
   8:      Next i
   9:      ProcessArray = r
  10:  End Function

Adding a reference in Visual Studio results in an interop method that looks like this:

clip_image002

Which is not type safe (ByRef System.Array).

Generating the wrapper using TlbImp gave me this signature:

clip_image004

Why do we get this difference? Visual Studio still runs TlbImp, but with the /sysarray switch which forces the use of System.Array. The advantage of the System.Array is that it supports non-zero lower bounds! Check the documentation for its CreateInstance method.

Skip to main content