Discussion:
using VB6 Collections in c#
(too old to reply)
asanford
2005-11-10 22:34:03 UTC
Permalink
I have a VB6-based COM object that I want to utilize in c# (VS2003.) I add a
reference to the COM object (which creates an interop wrapper). I then
create an instance of the wrapper class. The problem I'm having is a method
of that class returns a VB6 Collection object. By default, the generated
interop class maps this to a 'object' return type. I tried casting this
object to a VBA.CollectionClass (I got this type by adding a COM reference to
MSVBVB60.dll), which seems to work (I can call Collection.Count()), but when
I call Collection.Add(ob,key,before,after), I get a COM exception:

System.Runtime.InteropServices.COMException: Exception from HRESULT:
0x800A0005 (CTL_E_ILLEGALFUNCTIONCALL).

Any idea how to get this to work? I really only want the two-parameter
version of Add(), but I don't seem to be able to use this from c#. Here's
how I'm calling:

collection.Add(ref oItem,ref oKey,ref oBefore, ref oAfter);

Any ideas? I got the idea of casting it to the VBA.CollectionClass from this
article:

http://support.microsoft.com/?kbid=323737

Thanks!
"Peter Huang" [MSFT]
2005-11-11 06:34:09 UTC
Permalink
Hi

We do not need to add reference to the vbruntime, when you add reference to
the CollectionFactory.dll, the IDE will automatically reference the VBA.
Here is the code goes which ran OK based on my test.
[STAThread]
static void Main(string[] args)
{
CollectionFactory.clsVBACollectionClass objVBACollection = new
CollectionFactory.clsVBACollectionClass();
VBA.Collection col = objVBACollection.CreateVBACollection();
object m = System.Reflection.Missing.Value;
object name ="Microsoft";
col.Add(ref name,ref m,ref m,ref m);
object i = 1;
Console.Write(col.Item(ref i));
}

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
asanford
2005-11-11 14:49:02 UTC
Permalink
your code snippet solved my problem: using System.Reflection.Missing.Value
for the optional fields fixed the exception! works like a charm now.

thanks a bunch!
Post by "Peter Huang" [MSFT]
Hi
We do not need to add reference to the vbruntime, when you add reference to
the CollectionFactory.dll, the IDE will automatically reference the VBA.
Here is the code goes which ran OK based on my test.
[STAThread]
static void Main(string[] args)
{
CollectionFactory.clsVBACollectionClass objVBACollection = new
CollectionFactory.clsVBACollectionClass();
VBA.Collection col = objVBACollection.CreateVBACollection();
object m = System.Reflection.Missing.Value;
object name ="Microsoft";
col.Add(ref name,ref m,ref m,ref m);
object i = 1;
Console.Write(col.Item(ref i));
}
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT]
2005-11-12 02:46:14 UTC
Permalink
Hi

I am glad my suggestion helps you!
Cheers! :)

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Loading...