CoCreateInstance returning E_NOINTERFACE even though interface is found

I have a COM class CMyCOMServer implementing IMyInterface in one application, both with correct GUIDs. CMyCOMServer::QueryInterface will return S_OK (and cast itself to the right type) if IUnknown or IMyInterface is requested, otherwise it returns E_NOINTERFACE.

In another app on the same PC, I call:

HRESULT hr = ::CoCreateInstance(__uuidof(CMyCOMServer), 0, CLSCTX_SERVER, __uuidof(IMyInterface ),(void **)&pInterface);

It returns E_NOINTERFACE. So I assumed I was doing something wrong and added a breakpoint on CMyCOMServer::QueryInterface. I found that when CoCreateInstance is called, QueryInterface is triggered several times for different interfaces:

  • First, IUnknown is requested - no problem
  • Then, several interfaces like IMarshall etc are requested... these are not supported so E_NOINTERFACE is returned
  • Finally, IMyInterface is requested. I verify QueryInterface returns S_OK and sets (IMyInterface *)this as the interface pointer, as expected

So my confusion is why the calling CoCreateInstance is leaving me a NULL pointer and return code of E_NOINTERFACE, when the COM server app is clearly returning the interface I ask for?

EDIT: my client app calls CoInitialize(NULL) at startup, this makes no difference.

-------------Problems Reply------------

If your COM server is running in a different process, or a different apartment in the same process, COM needs to know how to package and transmit parameters when you make calls to your interface. This process is called "marshaling".

If you define a custom interface, you need to implement marshaling for it using one of the following approaches.

  • Standard marshaling: have the MIDL compiler to generate a proxy and stub which you must register on the system. This is probably the best option since you have already defined your interface.
  • OLE Automation marshaling: you define an automation compatible custom interface and use the marshaller which is already part of the COM framework
  • Custom marshaling: you implement the methods of IMarshal

When you are debugging your COM server, although you see that you are returning your custom interface in the call to QueryInterface, it does not make it across the process boundary because COM cannot figure out how to marshal that interface, hence the client sees E_NOINTERFACE.

UPDATE (based on your comment)

If this is an existing COM server app then you probably already have a proxy/stub. You need to register this on both the client and server. Could it be that you were testing this on a new machine(s) and you simply forgot to register this? To register you simply do regsvr32 on the proxy/stub dll.

This happens because COM subsystem tries to marshal your custom interface (IMyInterface) and simply has no idea how to do that. That happens either because the server is out-proc or because the server is in-proc and the thread of the consumer application that calls CoCreateInstance() has called CoInitialize()/ CoInitializeEx() incorrectly so that "multithreaded apartment" is requested as mentioned in the article user Thomas refers to in the other answer.

If you only need an in-proc server you could suppress marshalling by ensuring that the thread calling CoCreateInstance() either calls CoInitialize() or CoInitializeEx() with COINIT_APARTMENTTHREADED to enforce "single-threaded apartment".

If you need an out-proc server you can't get around marshalling. In the latter case you could do one of the following:

  • implement IMarshal - least preferable
  • add proxy/stubs and register them for your custom interface
  • (not sure if it will work for out-proc, but it's the simplest) if your interface can be marshalled with automation marshaller simply include a typlib into the resources of your COM server and register that typelib in the registry.

Could this be the threading model problem that Raymond Chen wrote about?

Edit in reply to the comment:

If your threading model is incompatible with the threading model of the object you're creating, then COM marshalling kicks in. And if the marshalling stuff isn't there, the error that comes out is E_NOINTERFACE, because the marshalling interface is missing.

It's more about threading models than about marshalling, really.

The previous comments about E_NOINTERFACE returned because marshalling interface is missing was very helpful, however, for us the answer/fix was to force the main application (the one calling CoCreateInstance) to be STA (single threaded apartment), and this was done by setting an advanced linker option, i.e.:

"CLR Thread Attribute" is set to "STA threading attribute"

or on the link command line you do:

"/CLRTHREADATTRIBUTE:STA"

This prevents a mix of MTA and STA, which causes a call across threads.

Hope someone else finds this helpful.

Category:c# Views:3 Time:2009-11-23

Related post

  • FindConnectionPoint returns E_NOINTERFACE 2011-12-08

    I am trying to build Connection Points in existing COM component. I derive the class from CCmdTarget and call MFC macros in class declaration and implementation as described in MSDN. Component is correctly build, linked, and run. However, when I debu

  • CreateWICTextureFromFile E_NOINTERFACE No such interface supported 2015-01-07

    So I have been trying to load a texture from a file using the CreateWICTextureFromFile function, but I can't for the life off me figure out why it doesn't work. As the title says it returns the HRESULT E_NOINTERFACE No such interface supported. I hav

  • CComPtr CoCreateInstance returns 0x80070582 (Class already exists.) 2010-05-18

    I have a StartComObjects function called when the user presses the Login button and a StopComObjects function called when the user presses the Cancel button. The StartComObjects function uses CComPtr.CoCreateInstance to create the COM object and sets

  • Default method return value in Java interfaces 2010-09-29

    While working with annotations I stumbled accross the following piece of code (it's the Hibernate @NotNull annotation): @Target(value = {ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETE

  • TypeDescriptor doesn't return members from inherited interfaces 2010-10-27

    my problem is that TypeDescriptor doesn't return members from inherited interfaces, is this how it is supposed to be working ? or is it a bug ? [TestFixture] public class DescriptorTests { [Test] public void Test() { // count = 1 Assert.AreEqual(2, T

  • Java - Overriding return type of extended interface when return type uses generics for own method parameter types 2011-02-20

    i've stumbled upon a curiosity in the java inheritance, and I wanted you to ask for better ideas on that: Assume two interfaces A and A1 Interface A1 extends A Interface A has a method which returns a generic type. The generic type would be like Gene

  • How to create a WCF web service within an ASP.NET application that can return instances of an interface as a transparent proxy 2012-02-07

    My use-case: I already have a working ASP.NET application I would like to implement a new Web Service as part of that application I am supposed to use a WCF service (*.svc), not an ASP.NET web service (*.asmx) The service needs to have one operation,

  • Return SAFEARRAY of custom interface types to VB6 through COM 2011-04-12

    Is it possible to return an array of defined interface objects from a C++ COM function (VC6) to a VB6 client? I've scoured the web and haven't been able to come across anything that describes what I need to do. I've seen a lot of passing BSTR and VAR

  • QueryInterface on a VB6 component returns E_NOINTERFACE in debugger only 2011-11-23

    I have an interface (ITask) that implemented by a VB6 COM object. I have a C++ COM object that uses the interface, and it generally works fine. However, when I run the VB6 debugger to debug the VB6 COM object, my C++ object is all of a sudden getting

  • CoCreateInstance returns "Class not registered" 2012-03-19

    I've been at this for hours now and it's starting to get upsetting. :( I have a COM DLL that I created in .NET 4.0, that I want to be able to access with a legacy application made in VC++ 6. Its visible to COM, I already successfully created the TLB

  • How to search for all methods in a project that return implementation of Collection interface? 2009-02-25

    I've been reading Josh Bloch's 'Effective Java 2nd Edition'. Item 43 states 'Return empty arrays or collections, not nulls'. My question is how can I search for all methods in a project that return an implementation of java.util.Collection interface?

  • Rhino Mocks DynamicMultiMock - setting expectation with return value on additional interface 2010-03-11

    I am creating a DynamicMultiMock as follows: this.serviceClient = this.mocks.DynamicMultiMock<ISlippyPlateProcedureService>(typeof(ICommunicationObject)); Then setting the following expectation: Expect.Call(((ICommunicationObject)this.serviceCl

  • How do I properly return a List of Interfaces type? 2010-11-25

    With the following code, I receive an "unable to implicitly cast" compilation error on the "return books;" line. I thought that because am returning a list of book objects that implement IPublication this should work fine? public interface IPublicati

  • How to properly return values from unmanaged interface method? 2010-12-14

    I'm in the process of extending GeckoFx (http://geckofx.org) and am having some problems returning array values from unmanaged interfaces provided by XPCom. I've added a great amount of new feature support to GeckoFx using the latest XulRunner 1.9.2.

  • Returning a collection of interfaces 2011-01-17

    I have created the following interface public interface ISolutionSpace { public boolean isFeasible(); public boolean isSolution(); public Set<ISolutionSpace> generateChildren(); } However, in the implementation of ISolutionSpace in a class call

  • Returning C++ polymorphic objects (interfaces) 2011-01-20

    I'd like to know what is considered nowadays the best practice when returning a pointer to a polymorphic object from a function, for example when using factories. If I transfer the ownership, should I return boost::unique_ptr<Interface>? What s

  • return a list of interfaced objects 2011-05-02

    I have a class Category that implements the interface CategoryManager. Next there is Card that maintains a List<Category>. Now I would like to return this list, but with categories as CategoryManager, hence List<CategoryManager>. The reas

  • An enum method that returns enums of specific interface 2011-11-08

    I have the following interface public interface DeviceKey { String getKey(); } I also have various enums that all extends this interface. In a class that contains all the enums I want a method that based on a string (key), can return the enum corresp

  • requestRouteToHost returns false on wifi interface 2011-11-30

    public boolean requestRouteToHost (int networkType, int hostAddress) This method in ConnectivityManager if used with networkType set to wifi interface always returns false. requestRouteToHost android says it was never implemented for the wifi interfa

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.158 (s). 11 q(s)