problem using a COM collection within C# 
Author Message
 problem using a COM collection within C#

Hello,

I am writing a C# application that is used as a COM server in a COM client
environment (via interop).  The C# method will be passed a dictionary object
and will need to retrieve values from it.  I can receive the object and get
the Count on it, but have been unsuccessful at retrieving any items.  I
would love to cast it to a System.Collections.Specialized.StringDictionary
object, but I have not been able to do this.

...
public string foo (object oCOM)
{
    Type t = oCOM.GetType();

    // retrieve the count - this returns proper count
    int iCount = t.InvokeMember("Count", BindingFlags.GetProperty, null,
oCOM, new object[] = {});

    // try to get a StringDictionary interface
    StringDictionary oDict =
(StringDictionary)Marshal.CreateWrapperOfType(oCOM,
typeof(StringDictionary));
    // throws  mscorlib error '80070057'  The type must be __ComObject or be
derived from __ComObject. Parameter name: t

    // try to use Item() interface
    int index = 1;
    object oReturn = t.InvokeMember ("Item", BindingFlags.Default
|BindingFlags.GetProperty, null,  oCOM, new object[]{index});
    string sResult = (string) oReturn;
    // the string cast throws the following error:  COMTest1 error
'80004002'  Exception of type System.InvalidCastException was thrown.

    return sResult;

Quote:
}

Any ideas as to how to do this?

Thanks,
Shan McArthur



Sun, 04 Jul 2004 06:39:14 GMT  
 problem using a COM collection within C#
Shan,

    Is the dictionary that you are passing in from the Scripting runtime?
If this is the case, then you definitely can not cast the object passed in
to a StringDictionary instance.  The Marshal.CreateWrapperOfType is used to
create .NET wrappers to COM objects.

    If the dictionary that is being passed in is an instance of the
dictionary from the scripting runtime, then I would recommend using TLBIMP
on the scripting runtime dll, and creating a wrapper of the Dictionary type
that the utility generates for you.  This would make accessing the methods
of the dictionary much easier.

    Hope this helps.

--
                              - Nicholas Paldino [.NET MVP]


Quote:
> Hello,

> I am writing a C# application that is used as a COM server in a COM client
> environment (via interop).  The C# method will be passed a dictionary
object
> and will need to retrieve values from it.  I can receive the object and
get
> the Count on it, but have been unsuccessful at retrieving any items.  I
> would love to cast it to a System.Collections.Specialized.StringDictionary
> object, but I have not been able to do this.

> ...
> public string foo (object oCOM)
> {
>     Type t = oCOM.GetType();

>     // retrieve the count - this returns proper count
>     int iCount = t.InvokeMember("Count", BindingFlags.GetProperty, null,
> oCOM, new object[] = {});

>     // try to get a StringDictionary interface
>     StringDictionary oDict =
> (StringDictionary)Marshal.CreateWrapperOfType(oCOM,
> typeof(StringDictionary));
>     // throws  mscorlib error '80070057'  The type must be __ComObject or
be
> derived from __ComObject. Parameter name: t

>     // try to use Item() interface
>     int index = 1;
>     object oReturn = t.InvokeMember ("Item", BindingFlags.Default
> |BindingFlags.GetProperty, null,  oCOM, new object[]{index});
>     string sResult = (string) oReturn;
>     // the string cast throws the following error:  COMTest1 error
> '80004002'  Exception of type System.InvalidCastException was thrown.

>     return sResult;
> }

> Any ideas as to how to do this?

> Thanks,
> Shan McArthur



Mon, 05 Jul 2004 00:44:50 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Using COM objects within COM objects

2. Populating collection Items within the collection

3. implementing customized Collection Editor using C#.

4. VB6 Collection using C#

5. using a C# uint Enum within a VB client

6. Using one COM exe server within another

7. Using GetLastError within a com object

8. Using the CommonDialog ActiveX Control from within an ATL COM dll

9. Using Winmm.lib from within a COM object

10. Using a VISUAL Basic COM component from within Visual C++

11. Using C++ class in C# within VS.NET ???

12. C++ Class used within C# (Char[] in C++)

 

 
Powered by phpBB® Forum Software