Hi,
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 CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<YourComObject, &__uuidof(YourComObject)>,
public ISupportErrorInfo,
public IDispatchImpl<IYourComObject, &__uuidof(IYourComObject)>
{
public:
DECLARE_REGISTRY_RESOURCEID(IDR_YOURCOMOBJECT)
BEGIN_COM_MAP(YourComObject)
COM_INTERFACE_ENTRY(IYourComObject)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(ISupportErrorInfo)
END_COM_MAP()
public:
// ISupportsErrorInfo
STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
// IYourComObject
STDMETHOD(DoSomeThing)(/*[in]*/ BSTR SomeText, /*[in]*/ long SomeNumber,
/*[out, retval]*/ BSTR *retVal);
private:
YourNativeClass impl_;
};
// in your COM cpp file:
STDMETHODIMPL YourComClass:
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_.DoSomeThing(OLE2T_EX(SomeText, 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.