CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    COM - Can a simple C++ class be used as a COM object ?

    I have some doubts...

    I have coded a C++ class that includes methods
    Code:
    HRESULT STDMETHODCALLTYPE QueryInterface(
        /* [in] */ REFIID riid, 
        /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject);
    ULONG STDMETHODCALLTYPE AddRef(void);
    ULONG STDMETHODCALLTYPE Release(void);
    Where AddRef and Release don't do anything and QueryInterface fails on every attempt. (meaning it sets *ppvObject to NULL and returns S_FALSE).

    The rest of the class is comprised of the implementation of another COM interface namely ITextHost. Now I'm creating a COM object that takes an ITextHost interface as a parameter. Is it OK to pass it static_cast<ITextHost*>(this) ?

    My C++ class does not need to be a creatable COM object. It only needs to be called through COM once it has been created. It is not supposed to be derived from IDispatch, but only from IUnknown (hence QueryInterface, AddRef and Release)

    My guess is that I should at least allow QueryInterface to succeed for IUnknown and ITextHost.

    BUT, provided this, is this really supposed to work ? It does work in my example code (on my machine), but it seems a bit dodgy.
    Attached Files Attached Files

  2. #2
    Join Date
    Oct 2002
    Location
    Tx, US
    Posts
    208
    hello,
    If u want to use ur C++ class internal to ur COM component why ur deriving it from IUnknown.
    just make it plain C++ class which implementing ur custom interface.

    Vinod

  3. #3
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    The point is that I pass it to a function which expects a COM object, so I have to at least derive from IUnknown.

    And no, it's not internal. I'm passing my class to a function in some other DLL that expects a COM object.

  4. #4
    Join Date
    Sep 2002
    Posts
    77
    Your code will work as long as you are taking special care of what your a doing with the ITextHost interface pointer.

    But my question is why are you taking the risk of running into a problem?
    Why don't you create a simple COM object which is "empty" besides of implementing the ITextHost interface? You will be on a clean way, it will have no overhead, no special considerations...

  5. #5
    Join Date
    May 2002
    Location
    Russia
    Posts
    1,571
    ...
    , is this really supposed to work?
    in general case, yes. you need to implement QI to return both interfaces

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Originally posted by alpha137
    But my question is why are you taking the risk of running into a problem?
    Why don't you create a simple COM object which is "empty" besides of implementing the ITextHost interface? You will be on a clean way, it will have no overhead, no special considerations...
    Well, the point is that I would like to use it in a project where the class implementing ITextHost is only one of many in an inheritance tree. If you have tried inheritence in COM, you know why I prefer doing this.

  7. #7
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Originally posted by alexey_1979
    ...

    in general case, yes. you need to implement QI to return both interfaces
    Cool, thanks alexey and alpha

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured