How can I pass a collection of objects from VB6 to .NET?
Asked Answered
S

4

2

I need to pass a collection of key/value pairs of strings from VB6 to .NET. In VB6 code they exist in a native collection. But I am not sure what I can reference in my .NET project to be able to accept a parameter of type Collection in one of my methods.

I tried adding a reference to Visual Basic for Applications but got back "A reference to 'Visual Basic For Applications' could not be added."

Am I going about it the wrong way?

Superincumbent answered 27/3, 2013 at 0:3 Comment(0)
A
4

You could use something like this in c#:

[Guid("fb5e929a-2f8b-481e-9516-97edf5099df4")]
[ComVisible(true)]
public interface myInterface{
public void addObject(string key, string value);
}

And in your class, you could have this:

private collection
public addObject(string key, string value)
{
collection.Add(key, value);
}

This should allow you to call addObject in vb6 and passing your data. Then .net will add it to a collection, so instead of passing your whole collection from vb6 to .net, you pass them one by one.

You can read more about the GUID here.

More info about COM with an exemple of code between vb6 and c# here.

Arron answered 27/3, 2013 at 0:26 Comment(0)
T
3

Rather than using COM, I have found it far simpler to just serialize my data as JSON and send it over the Interop chasm as plain text. I resisted it at first, but it is now my go-to solution. Give it a try if the "proper" solutions prove frustrating.

Tumbler answered 27/3, 2013 at 2:19 Comment(1)
The collection is just a small piece of the puzzle. If I start writing serializing code for the entire object structure, we'll have .NET 9 out.Superincumbent
N
1

Try passing it to .NET by making a HashTable in Visual Basic 6.0:

Set dictionary = Server.CreateObject("System.Collections.HashTable")
With dictionary
    For i = 1 To 100
        .Add Chr(i), "some text value"
    Next
End With

Then in a C# or VB.NET COM exposed class

public string LoadHashTable(Object tDict)
{
   return String.Format("Type : \"{0}\", Count : {1}", ((Hashtable)tDict).GetType().ToString(), ((Hashtable)tDict).Count);
}

An example of a COM exposed class is in Stack Overflow question Building a COM interop library for ASP Classic using 4.0 framework and Visual Studio 2010

Remember to register it in both x86 and x64:

%windir%\Microsoft.NET\Framework\v4.0.30319\regasm MyThing.dll /tlb:MyThing.tlb /codebase MyThing

%windir%\Microsoft.NET\Framework64\v4.0.30319\regasm MyThing.dll /tlb:MyThing.tlb /codebase MyThing
Navigation answered 27/3, 2013 at 1:42 Comment(2)
Why would I need to register it for x64? VB6 is 32 bit only?Superincumbent
I am coming from a classic ASP background so I just reminded you to do both if required. VB6 exes you will only need 32 bit you are right.Navigation
U
1

Your VB6 function can return a Dictionary from the Microsoft Scripting Runtime library. In .NET, you could add a COM reference to it. I found it useful to convert it to a System.Collections.Generic.Dictionary. I created a generic extension to do so allowing me to specify the type for the keys and values.

        public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this Scripting.Dictionary p_ScriptingDictionary)
    {
        string codeName = m_ModuleName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()";

        try
        {
            var dic = p_ScriptingDictionary.Cast<object>().ToDictionary(
                keySelector: elm => (TKey)elm,
                elementSelector: elm => (TValue)p_ScriptingDictionary.get_Item(ref elm));
            return dic;
        }
        catch (Exception ex)
        {
            throw new ApplicationException("Error converting Scripting.Dictionary to Systems.Collections.Generic.Dictionary.", ex);
        }
    }
Ubangi answered 28/3, 2023 at 2:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.