VB Migration Partner

KNOWLEDGE BASE - Classes and COM objects


Previous | Index | Next 

[HOWTO] Expose arrays with nonzero lower index to COM clients

If you are migrating a DLL that must continue to serve one or more COM clients, it might happen that one of your methods must return an array with a nonzero lower bound, which aren’t supported natively by either VB.NET and C#. This article illustrates a workaround. Let’s start with the following VB6 method:

    Public Function Models() As String()
	    Dim arResult(1 To 2) As String
	    arResult(1) = "Ford Escort"
	    arResult(2) = "Volkswagen Touran"
	    Models = arResult
    End Function

Here’s how the method should look in .NET to work correctly:

    'VB.NET
    Imports System.Runtime.InteropServices
    Public Function Models() As  Array
	    Dim arResult(2) As String
	    arResult(1) = "Ford Escort"
	    arResult(2) = "Volkswagen Touran"
	    Return manageLowerBound(arResult)
    End Function

    Private Function manageLowerBound(ByVal oldArray() As String) As Array
	    Dim lowerBounds As Int32() = {1}
	    Dim lengths As Int32() = {oldArray.GetUpperBound(0)}
	    Dim oneBasedArray As Array = Array.CreateInstance(GetType(String),
                    lengths, lowerBounds)
 
	    For i As Integer = oneBasedArray.GetLowerBound(0) To oneBasedArray.GetUpperBound(0)
		    oneBasedArray(i) = oldArray(i)
	    Next
	    Return oneBasedArray
    End Function

    // C#
    ising System.Runtime.InteropServices;

    public [return: MarshalAs(UnmanagedType.SafeArray, _
    {
 	    SafeArraySubType=VarEnum.VT_BSTR)] Array Models()System.Array
	    string[] arResult = new string[3];
	    arResult[1] = "Ford Escort";
	    arResult[2] = "Volkswagen Touran";
	    return manageLowerBound(arResult);
    }

    private Array manageLowerBound(string[] oldArray)
    {
	    int[] lowerBounds = new int[]{1};
	    int[] lengths = new int[]{oldArray.GetUpperBound(0)};
	    Array oneBasedArray = Array.CreateInstance(typeof(String), lengths, lowerBounds);
 
	    for (int i = oneBasedArray.GetLowerBound(0); i <= oneBasedArray.GetUpperBound(0); i++)
		    oneBasedArray[i] = oldArray[i];
	    return oneBasedArray;
    }






	

Previous | Index | Next 




Follow Francesco Balena on VB6 migration’s group on

LinkedIn





Read Microsoft Corp’s official case study of a VB6 conversion using VB Migration Partner.




Code Architects and its partners offers remote and onsite migration services.

More details




Subscribe to our free newsletter for useful VB6 migration tips and techniques.

newsletter



To learn more about your VB6 applications, run VB6 Analyzer on your source code and send us the generated text file. You will receive a detailed report about your VB6 applications and how VB Migration Partner can help you to quickly and effectively migrate it to .NET.

Get free advice



A fully-working, time-limited Trial Edition of VB Migration Partner allows you to test it against your actual code

Get the Trial




The price of VB Migration Partner depends on the size of the VB6 application, the type of license, and other factors

Request a quote




Migrating a VB6 application in 10 easy steps

Comparing VB Migration Partner with Upgrade Wizard

Migration tools: Feature Comparison Table

All whitepapers