07-25-2014, 07:20 AM
COM interface.
Macro Macro2345
Macro Macro2345
Macro Macro2345
//Put this before a non-class function declaration and definition to export the function with __cdecl calling convention. It is the easiest way, don't need a .def file. Using Microsoft Visual C++.
#define EXPORTC extern "C" __declspec(dllexport)
//I use this C macro to implement IUnknown functions QueryInterface, AddRef and Release that support standard reference counting.
//Can be used in any class that implements a COM interface. Example: STANDARD_IUNKNOWN_METHODS(IMyInterface)
#define STANDARD_IUNKNOWN_METHODS(iface) \
STDMETHODIMP QueryInterface(REFIID iid, void** ppv)\
{\
,if(iid == IID_IUnknown || iid == IID_##iface) { m_cRef++; *ppv = this; return S_OK; }\
,else { *ppv = 0; return E_NOINTERFACE; }\
}\
STDMETHODIMP_(ULONG) AddRef()\
{\
,return ++m_cRef;\
}\
STDMETHODIMP_(ULONG) Release()\
{\
,long ret=--m_cRef;\
,if(!ret) delete this;\
,return ret;\
}
namespace Math
{
,#define IID_IMathFuncs __uuidof(IMathFuncs)
,__interface __declspec(uuid("1DD0DD58-59C7-4308-93B8-40EEDEDE2415")) //replace this with your generated GUID
,IMathFuncs :public IUnknown
,{
,,STDMETHODIMP Add(double a, double b, double& R);
,,STDMETHODIMP Sub(double a, double b, double& R);
,};
,class MathFuncs :public IMathFuncs
,{
,,long m_cRef;
,,public:
,,MathFuncs() { m_cRef=1; }
#pragma region virtual_impl
,,STANDARD_IUNKNOWN_METHODS(IMathFuncs)
,,// Returns a + b
,,STDMETHODIMP Add(double a, double b, double& R);
,,STDMETHODIMP Sub(double a, double b, double& R);
#pragma endregion
,};
,STDMETHODIMP MathFuncs::Add(double a, double b, double& R) { R=a+b; return 0; }
,STDMETHODIMP MathFuncs::Sub(double a, double b, double& R) { R=a-b; return 0; }
EXPORTC
const IMathFuncs* Math_CreateObject() { return new MathFuncs; }
}