Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. InvalidCastException

InvalidCastException

Scheduled Pinned Locked Moved COM
comcsharphostingquestionworkspace
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hubert
    wrote on last edited by
    #1

    Hi! I am playing with that piece of code (C#): //changing directory for COM access Environment.CurrentDirectory=@"C:\Program Files\CDN OPT!MA\"; //initializing main COM class of application CDNBase.ApplicationClass app=new CDNBase.ApplicationClass(); //login and other blah blah CDNBase.ILogin l=app.LockApp(255,100,null); CDNBase.AdoSession sesja=l.CreateSession(); //initializing collection of objects CDNBase.IAdoCollection collection=(CDNBase.IAdoCollection)sesja.CreateObject("CDN.DokumentyHaMag",null); //creating new object object obj=collection.AddNew(null); Everything works well right now. All CDNBase.* are declared in interop modules. I need to cast object obj to its interface. so: CDNAX.IDokumentHaMag doc=(CDNAX.IDokumentHaMag)obj; (CDNAX is interop module) here the InvalidCastException comes. The obj exposes this interface - i am sure ;-) I've tried to use InvokeMethod on this object using methods and properties from IDokumentHaMag, and it works well. What am I doing wrong?

    J 1 Reply Last reply
    0
    • H Hubert

      Hi! I am playing with that piece of code (C#): //changing directory for COM access Environment.CurrentDirectory=@"C:\Program Files\CDN OPT!MA\"; //initializing main COM class of application CDNBase.ApplicationClass app=new CDNBase.ApplicationClass(); //login and other blah blah CDNBase.ILogin l=app.LockApp(255,100,null); CDNBase.AdoSession sesja=l.CreateSession(); //initializing collection of objects CDNBase.IAdoCollection collection=(CDNBase.IAdoCollection)sesja.CreateObject("CDN.DokumentyHaMag",null); //creating new object object obj=collection.AddNew(null); Everything works well right now. All CDNBase.* are declared in interop modules. I need to cast object obj to its interface. so: CDNAX.IDokumentHaMag doc=(CDNAX.IDokumentHaMag)obj; (CDNAX is interop module) here the InvalidCastException comes. The obj exposes this interface - i am sure ;-) I've tried to use InvokeMethod on this object using methods and properties from IDokumentHaMag, and it works well. What am I doing wrong?

      J Offline
      J Offline
      Jorgen Sigvardsson
      wrote on last edited by
      #2

      ..::Hubert::.. wrote: The obj exposes this interface - i am sure But are you 100% sure that CDNAX.IDokumentHaMag is what you think it is? Run the code using COM Trace[^]. It might provide you with some insight on what's wrong. Also, if you are writing your (or have the source for) COM stuff with ATL, try compiling it with by defining ATL_DEBUG_QI in your stdafx.h. It'll output a debug trace for each QueryInterface calls made on objects in that server. -- Booohoo!

      H 1 Reply Last reply
      0
      • J Jorgen Sigvardsson

        ..::Hubert::.. wrote: The obj exposes this interface - i am sure But are you 100% sure that CDNAX.IDokumentHaMag is what you think it is? Run the code using COM Trace[^]. It might provide you with some insight on what's wrong. Also, if you are writing your (or have the source for) COM stuff with ATL, try compiling it with by defining ATL_DEBUG_QI in your stdafx.h. It'll output a debug trace for each QueryInterface calls made on objects in that server. -- Booohoo!

        H Offline
        H Offline
        Hubert
        wrote on last edited by
        #3

        Hi I think, the problem is inside COM - maybe something is missed or incorectly declared, but I have no access to the source :-(. There are also more types in this namespace: DokumentHaMag (interface derived from IDokumentHaMag) and DokumentHaMagClass (derived from DokumentHaMag interface). When I try to create new object of class DocumentHaMagClass, another interesting exception is thrown: "Query Interface failed for object x" (I don't remember exact text of this exception - I have no access to the compiler right now). Another interesting thing is creating RCW. I've tried to create RCW based on tlbs and dlls. Doing this in Visual Studio works well, but when I try to use tlbimp on CDNAX tlb, exception is thrown: "An element with the same key already exists in the Hashtable." with some GUID value. I'm gona try the tool you linked, it may help me to find the problem. thanks for your reply h.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups