1

I am attempting to understand creating/using COM components without the help of MFC/ATL to know its inner workings. I am using this codeguru article for reference.Following are the steps followed by me.

  • Created a Wind32 Dll,
  • Added a MIDL file and declared the interface IAdd and library name DemoMath; compiled the code using MIDL compiler.
  • Created CAddObj class deriving IAdd interface,provided implementation for IAdd and IUnknown interfaces.
  • Created class CAddFactory deriving from IClassFactory interface;provided implementation for IClassFactory methods.

Now creating DllGetClassObject to give client an option to invoke this function to get an instance of the class factory.

Following is the code:

#include "stdafx.h"
#include <objbase.h>
#include "AddObjFactory.h"
#include "IAdd_i.c"
STDAPI DllGetClassObject(const CLSID& clsid,
                         const IID& iid,
                         void** ppv)
{
    //
    //Check if the requested COM object is implemented in this DLL
    //There can be more than 1 COM object implemented in a DLL
    //

    if (clsid == CLSID_AddObject)
    {
        //
        //iid specifies the requested interface for the factory object
        //The client can request for IUnknown, IClassFactory,
        //IClassFactory2
        //
        CAddFactory *pAddFact = new CAddFactory;
        if (pAddFact == NULL)
            return E_OUTOFMEMORY;
        else
        {
            return pAddFact->QueryInterface(iid , ppv);
        }
    }

    //
    //if control reaches here then that implies that the object
    //specified by the user is not implemented in this DLL
    //

    return CLASS_E_CLASSNOTAVAILABLE;
}

Now where is CLSID_AddObject constant suppose to be defined or Is it generated while compiling MIDL file(I didn't find it though)?

2
  • 2
    In many pre-C++98-standard compilers, new returned zero on failure. However, that hasn't been the case for over a decade, even on Microsoft's compilers. You need to catch the std::bad_alloc exception instead. Commented Jun 11, 2013 at 11:20
  • 1
    You have a memory leak in your DllGetClassObject() implementation. QueryInterface() increments the reference count if successful. If failed, your object does not get freed. You should start the object with a reference count of 1, call QueryInterface() on it, and then call Release() on it regardless of whether QueryInterface() succeeds or fails. That way, if successful, the reference count is only 1 (in the caller) when DllGetClassObject() exits, and if failed then the object gets freed correctly. Commented Jun 11, 2013 at 19:54

1 Answer 1

1

coclass IDL item will typically get you CLSID:

library Foo
{
//...
    [
        //...
    ]
    coclass AddObject
    {
        //...
    };

then on your "IAdd_i.c" you are already including:

MIDL_DEFINE_GUID(CLSID, CLSID_AddObject, ...);

this is what defines CLSID_AddObject.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the answer,did u mean interface IAdd instead of IAddObject(since this does not exist) in coclass AddObject? and I assume there needs to be another uuid I should associate the coclass with,right?
IAddObject is irrelevant, just typical declaration. I updated to only leave the relevant part. coclass itself is the important thing.
@Hans Passant I didn't get why you removed your answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.