title: Unable to cast COM object of type 'System.__ComObject' to class type AgentInfo
body: <p>I have two C# projects, one is a dll and another is a windows forms app.</p>
  
  <p>I have a CoClass defined in the dll as follows` '</p>
  
  <pre><code>    [ComVisible(true),
      Guid("1620BE13-A68F-4FA3-B5C8-31092D626CDA"),
      ProgId("AgentDLLServer.AgentInfo"),
      ClassInterface(ClassInterfaceType.AutoDispatch),
              ComSourceInterfaces(typeof(IAgentInfoEvents))
      ]
      public class AgentInfo : _IAgentInfo {}
  </code></pre>
  
  <p>It implements the interface _IAgentInfo, which is defined as follows</p>
  
  <pre><code> [
       ComVisible(true),
       Guid("CF803265-AE9D-4308-B790-560FCF63DD4C"),
       InterfaceType(ComInterfaceType.InterfaceIsDual)
      ]
      public interface _IAgentInfo{}
  </code></pre>
  
  <p>Both are defined in a dll, which is registered successfully using </p>
  
  <pre><code>regasm /tlb
  </code></pre>
  
  <p>In another C# windows client application, I try to access AgentInfo by casting an object obtained from either the Running Object Table, or from another interface as follows` </p>
  
  <pre><code>_IAgentInfo info =(_IAgentInfo) RotNativeMethods.GetObject("BizBrainAgentService.AgentInfo");`.
  </code></pre>
  
  <p>where the above code retrieves the object from the ROT
  or , I have another interface obtained from the ROT, as defined as follows</p>
  
  <pre><code>    [ 
      ComVisible(true),
      Guid("9B539A5F-5671-48AD-BF9B-7A9AF150CE39"),
      InterfaceType(ComInterfaceType.InterfaceIsDual)
      ]
      public interface _IAgentDLLServer
      { AgentInfo GetAgentInfo();}
  </code></pre>
  
  <p>where I get a reference to the interface _IAgentDLLServer from the ROT, and then call the method GetAgentInfo() on it</p>
  
  <pre><code>`_IAgentDLLServer server=  (_IAgentDLLServer)RotNativeMethods.GetObject("BizBrainAgentService.AgentServer") `AgentInfo info=server.GetAgentInfo();
  </code></pre>
  
  <p>I can cast it to _IAgentInfo, but when I try to cast the returned object to AgentInfo, as follows</p>
  
  <pre><code> AgentInfo info =(_IAgentInfo) rotNativeMethods.GetObject("BizBrainAgentService.AgentInfo");
  </code></pre>
  
  <p>I get the following error</p>
  
  <pre><code>Unable to cast COM object of type 'System.__ComObject' to class type 'AgentDLLService.AgentInfo'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
  </code></pre>
  
  <p>I tried the following solutions</p>
  
  <p>a. STAThread on the main method, because a post suggested that the thread on 
  which this object was running did not have access to type information
  as per
  <a href="https://stackoverflow.com/questions/16938467/why-cannot-i-cast-my-com-object-to-the-interface-it-implements-in-c">Why cannot I cast my COM object to the interface it implements in C#?
  </a></p>
  
  <p>b. Changed the app config file as follows</p>
  
  <pre><code>    &lt;configuration&gt;
       &lt;startup&gt;
           &lt;supportedRuntime version="v4.0.30319" sku=".NETFramework,Version=v4.5"/&gt;
     &lt;/startup&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>and the version matches that in the InProcServer32 of the registry</p>
  
  <pre><code>HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{1620BE13-A68F-4FA3-B5C8-31092D626CDA}\InprocServer32\1.0.0.0\RuntimeVersion, 
  </code></pre>
  
  <p>as per </p>
  
  <p><a href="https://msdn.microsoft.com/en-us/windows/compatibility/net-framework-4-5-is-default?f=255&amp;MSPPError=-2147217396" rel="nofollow noreferrer">.NET Framework 4.5 is default and .NET Framework 3.5 is optional
  </a>
  and<a href="http://www.visualstudioextensibility.com/2016/09/09/the-strange-case-of-system-invalidcastexception-unable-to-cast-com-object-of-type-system-__comobject-to-class-type-system-windows-forms-usercontrol-showing-toolwindow/" rel="nofollow noreferrer">The strange case of System.InvalidCastException (“Unable to cast COM object of type ‘System.__ComObject’ to class type System.Windows.Forms.UserControl”) showing toolwindow</a></p>
  
  <p>c. I tried the ComImport method</p>
  
  <pre><code>[ComImport,
      Guid("1620BE13-A68F-4FA3-B5C8-31092D626CDA")]
      public class AgentInfo { } 
  </code></pre>
  
  <p>in the class where I want to use this object, as per
  <a href="http://mike-ward.net/2008/09/02/a-lean-method-for-invoking-com-in-c/" rel="nofollow noreferrer">A lean method for invoking COM in C#</a></p>
  
  <p>d. Double casting the object</p>
  
  <p>AgentInfo info=(AgentInfo)(object)rotNativeMethods.GetObject("BizBrainAgentService.AgentInfo");</p>
  
  <p>as per
  <a href="https://stackoverflow.com/questions/27179568/why-cant-i-cast-this-interface-to-a-concrete-class">Why can't I cast this interface to a concrete class?
  </a></p>
  
  <p>e Using the as operator</p>
  
  <pre><code>object obj=rotNativeMethods.GetObject("BizBrainAgentService.AgentInfo");
  AgentInfo info=obj as AgentInfo
  </code></pre>
  
  <p>f. Implementing the IProvideClassInfo and IProvideClassInfo2 interfaces [Importing them using the ComImport attribute] on the agentInfoClass</p>
  
  <p>After all these attempts, I wonder whether it is possible to return a COM CoClass from a COM Interface, or the Running Object Table, as opposed to a COM Interface.</p>
  
  <p>Also, another question is, is AgentInfo being treated as a C# /dot net type instead of a COM type, according to the message. Is this really so? In that case, the cast would fail.</p>
  
  <p>I am aware that returning a CoClass rather than an interface may not be good practice, but I need to be able to listen to events from the AgentInfo object, and that does not appear to be possible from interfaces.</p>