Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling DLL from QM
#7
COM interface.
Macro Macro2345
Code:
Copy      Help
//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; }

}
Macro Macro2345
Code:
Copy      Help
interface# IMathFuncs :IUnknown
,double'Add(double'a double'b)
,double'Sub(double'a double'b)
,{1DD0DD58-59C7-4308-93B8-40EEDEDE2415}

dll- "my.dll" IMathFuncs'Math_CreateObject

IMathFuncs m=Math_CreateObject
out m.Add(5.7 2)
out m.Sub(5.7 2)


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)