Hi,
>You cannot use the class directly. You need to build a COM class which
exposes the methods you need, like mysum in your sample. The COM class
can then internally use your native class to implement the methods.
now i have done a sample project in atl, but do i need to rewrite all my
functions?
I see the sintax is different. is there a way to import in atl project
the class (using a librabry dll?)
I have not exp about com in c++!
You could add you cpp/h files to your ATL project.
Build one ATL COM object per class you want to use in VBA.
Add an instance of your class as private member into the COM class created
by the ATL wizard.
Add all functions you want to use in VBA to that COM class and delegate that
call to your private class member. You may need to do some parameter
conversions as you should stick to OLE automation compatible parameter types
in your COM objects.
So it should somehow look like this:
class ATL_NO_VTABLE YourComObject :
public CComObjectRootE x<CComSingleThr eadModel>,
public CComCoClass<You rComObject, &__uuidof(YourC omObject)>,
public ISupportErrorIn fo,
public IDispatchImpl<I YourComObject, &__uuidof(IYour ComObject)>
{
public:
DECLARE_REGISTR Y_RESOURCEID(ID R_YOURCOMOBJECT )
BEGIN_COM_MAP(Y ourComObject)
COM_INTERFACE_E NTRY(IYourComOb ject)
COM_INTERFACE_E NTRY(IDispatch)
COM_INTERFACE_E NTRY(ISupportEr rorInfo)
END_COM_MAP()
public:
// ISupportsErrorI nfo
STDMETHOD(Inter faceSupportsErr orInfo)(REFIID riid);
// IYourComObject
STDMETHOD(DoSom eThing)(/*[in]*/ BSTR SomeText, /*[in]*/ long SomeNumber,
/*[out, retval]*/ BSTR *retVal);
private:
YourNativeClass impl_;
};
// in your COM cpp file:
STDMETHODIMPL YourComClass::D oSomeThing(/*[in]*/ BSTR SomeText, /*[in]*/
long SomeNumber, /*[out, retval]*/ BSTR *retVal)
{
USES_CONVERSION _EX;
HRESULT hr = S_OK;
if(!retVal)
return E_POINTER;
// I assume your native C++ class uses LPCTSTR as string type:
TCHAR buf[1000];
impl_.DoSomeThi ng(OLE2T_EX(Som eText, 1000), SomeNumber, buf,
_countof(buf));
*retVal = T2BSTR(buf);
return hr;
}
When you want return a failure code return something between 0x80040200 and
0x8004ffff.
You can use the AtlReportError function to specifiy a combination of HRESULT
and error text.
--
SvenC