c++pointerswinapicomiunknown

Why are COM pointer arguments cast as void instead of IUnknown?


Apologies if this is a stupid question, but I wasn't clear on why COM pointer arguments are typically cast as (void**) instead of (IUnknown**). And then sometimes IUnknown pointers are in fact used, like with IObjectWithSite::SetSite. Can anyone explain this?


Solution

  • In "get-type" interface methods (like IObjectWithSite::QueryInterface, IObjectWithSite::GetSite, IMoniker::BindToObject, etc...), because it wouldn't change anything, you'd have to cast anyway, except when you do require an IUnknown* reference, but you already have it because ... you're using it (the IUknown* reference is always the same pointer per COM rules).

    IObjectWithSite::SetSite is a "set-type" method, so it makes more sense to give you an IUnknown* reference.

    It's probably more arguable in some static methods like CoCreateInstance or CoGetObject I think they could have put IUnknown** there instead of void** but then, they would have two different styles. And you wouldn't be able to use the old IID_PPV_ARGS macro that's so practical to use, and recommended as a coding practice to avoid type cast errors.

    I suggest you get a copy of the authoritative "Essential COM" from Don Box, and read up to page 60 (at least :-).