Holy cow, I wrote a book!
There are two ways of declaring COM interfaces, the hard way and the easy way.
The easy way is to use an IDL file and let the MIDL compiler generate your COM interface for you. If you let MIDL do the work, then you also get __uuidof support at no extra charge, which is a very nice bonus.
The hard way is to do it all by hand. If you choose this route, your interface will look something like this:
#undef INTERFACE #define INTERFACE ISample2 DECLARE_INTERFACE_(ISample2, ISample) { BEGIN_INTERFACE // *** IUnknown methods *** STDMETHOD(QueryInterface)(THIS_ REFIID riid, void **ppv) PURE; STDMETHOD_(ULONG,AddRef)(THIS) PURE; STDMETHOD_(ULONG,Release)(THIS) PURE; // ** ISample methods *** STDMETHOD(Method1)(THIS) PURE; STDMETHOD_(int, Method2)(THIS) PURE; // *** ISample2 methods *** STDMETHOD(Method3)(THIS_ int iParameter) PURE; STDMETHOD_(int, Method4)(THIS_ int iParameter) PURE; END_INTERFACE };
What are the rules?
INTERFACE
#undef
#define
DECLARE_INTERFACE
DECLARE_INTERFACE_
ISample2
ISample
STDMETHOD
STDMETHOD_
HRESULT
(THIS)
THIS_
PURE
BEGIN_INTERFACE
END_INTERFACE
There is a reason for each of these rules. They have to do with being able to use the same header for both C and C++ declarations and with interoperability with different compilers and platforms.
THIS
DECLARE_INTERFACE*
And you wonder why I called it "the hard way".
Similar rules apply when you are implementing an interface. Use the STDMETHODIMP and STDMETHODIMP_ macros to declare your implementations so that they get the proper calling convention attached to them. We'll see examples of this next time.
STDMETHODIMP
STDMETHODIMP_