none
COM and .NET2 Interoperability

    Question

  • Hi!
    I was started migrating project to Visual Studio 2005 and I discovered 
    problem with iterating through collection from COM object. This was working 
    fine in VS2003, but now I get this error:
    "Unable to cast COM object of type 'System.__ComObject' to class type 
    'OurLibrary.OurClass'. COM components that enter the CLR and do not support IProvideClassInfo or that do not have any interop assembly  registered will be wrapped in the __ComObject type. Instances of this type cannot be cast to any other class;   however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface."
    
    Could anybody help me?
    Wednesday, January 04, 2006 3:09 PM

Answers

  • I have the same problem interfacing with Outlook via COM. 

    Outlook.ApplicationClass outlook = new Outlook.ApplicationClass();

    MailItemClass eMail = (MailItemClass) outlook.CreateItem(OlItemType.olMailItem);

    The cast fails with the message you indicate.  Worked great in VS 2003.

    Interestingly, the code above is used in the Report Viewer code samples at GotReportViewer.com that Microsoft pointed me to for emailing a PDF using their ReportViewer component.

    Ray.

    Wednesday, January 11, 2006 4:01 PM

All replies

  • Hi

    I have the same problem !

    I have created a interop assembly for dso (Microsoft SQL Server Analysis Services)

    with vs2003 this works fine!

    Now i use VS2005! if i want get a object from a collection i get the same error!

    (a object is returned but it can not by casted.

    I have tested it with Interops from 1.1 runtime and from 2.0 runtime=> the same error => probability the error not resides in the interop assembly!

     

    anybody has a idea ?

    werner

     

    Tuesday, January 10, 2006 5:57 AM
  • I have the same problem interfacing with Outlook via COM. 

    Outlook.ApplicationClass outlook = new Outlook.ApplicationClass();

    MailItemClass eMail = (MailItemClass) outlook.CreateItem(OlItemType.olMailItem);

    The cast fails with the message you indicate.  Worked great in VS 2003.

    Interestingly, the code above is used in the Report Viewer code samples at GotReportViewer.com that Microsoft pointed me to for emailing a PDF using their ReportViewer component.

    Ray.

    Wednesday, January 11, 2006 4:01 PM
  • Problem resolved!

    >Outlook.ApplicationClass outlook = new Outlook.ApplicationClass();

    You must ensure, that the type of the variable at the left side of this term is a InterfaceType not a classType!!!

    i dont know the details of the Outlook Interop Types, but the the solution can be:

    Outlook.Application outlook = new Outlook.ApplicationClass();

    Use the ObjectBrowser to find a Type tht is a Interface not a class! (Outlook.Application NOT Outlook.ApplicationClass)

    here is a difference between CLR 1.1 and CLR 2.0 !

    werner

     

     

     

     

     

     

    Thursday, January 12, 2006 6:36 AM
  • I have the same issue and using the MailItem Interface instead of the MailItemClass works, but it does not expose the Item Events like the class does.  For instance, I need to know when the MailItemClass.ItemEvents_Event_Close event fires.

    How do I listen to all the events of the MailItemClass then in .NET 2.0?

    Saturday, February 25, 2006 11:38 PM
  • Did you ever find an answer to this problem? I have it, too, with a custom COM object, not like Outlook.

    Thanks.

    Michael

    Sunday, October 22, 2006 2:48 AM
  • Found an answer for my situation. I was running .NET 1.1 code against a web site set to .NET 2.0 in IIS. Dumb I know, but my colleagues spent a couple days trying to fix this, so I thought someone might benefit. We've been going back and forth between 1.1 and 2.0, and well, stuff happens.

    Sunday, October 22, 2006 3:00 AM
  • Unfortunately have the same problem in VS2008.

                Outlook.Application myOutlook = new Outlook.ApplicationClass();

                MailItemClass mItem = (MailItemClass)myOutlook.CreateItem(OlItemType.olMailItem);

     

    Unable to cast COM object of type 'System.__ComObject' to class type 'Microsoft.Office.Interop.Outlook.MailItemClass'. COM components that enter the CLR and do not support IProvideClassInfo or that do not have any interop assembly registered will be wrapped in the __ComObject type. Instances of this type cannot be cast to any other class; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.


    • Proposed as answer by kmccaa Monday, August 24, 2009 4:14 PM
    Tuesday, December 04, 2007 7:12 PM
  • I believe these instructions will help you solve this:

    http://kmccaa.blogspot.com/2009/08/interop-20-vs-11-breaking-change.html
    Monday, August 24, 2009 4:15 PM
  • Michael,

    Thanks for sharing. Even I had a similar issue due to IIS set to Net 2.0 but was running .Net 1.1 code.

    Thanks.
    Wednesday, September 09, 2009 6:35 AM