Exporting class in vba

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I wonder how to export a call in vba from unmanaged vc++
Usually i can export function ...with stdcall
In vb.net I can do that with com class.
I would like to instantiate an object from vba.
ie in vba

myobj()
myobj.calcultex()
.... and son on
 
Hi,
I wonder how to export a call in vba from unmanaged vc++
Usually i can export function ...with stdcall
In vb.net I can do that with com class.
I would like to instantiate an object from vba.
ie in vba

I didn't get that. Do you want to:

1) export a VBA class and use it in unmanaged C++
I don't think that is possible

2) export an unmanaged C++ function and use it in VBA
__stdcall functions can be used with a declare function statement

3) export an unmanaged C++ class and use it in VBA
You must make that class a COM class or wrap its functionality in a COM
class.
 
Hi,
3)
i want to export an unmanaged C++ class and use it in VBA
"You must make that class a COM class or wrap its functionality in a COM
class."

Yes create a unmanaged c++ class and export that in vba excel
What project solution I have to use and an code of example ie for the class
mymath

class mymath
{ public:
mymath();
double mysum(const double&,const double&)
~mymath
};

can I put all the library i use and stl?
You are saying I need a com wrapper, so I think I will be able to use that
in vba
ie.

sub test()
dim x as new mymath
msgbox mymath.mysum(3,3)
end sub

regards
 
Hi
i want to export an unmanaged C++ class and use it in VBA
class mymath
{ public:
mymath();
double mysum(const double&,const double&)
~mymath
};

can I put all the library i use and stl?
You are saying I need a com wrapper, so I think I will be able to use
that in vba

sub test()
dim x as new mymath
msgbox mymath.mysum(3,3)
end sub

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.

Do you know how to build COM objects? In VC++ ATL is a good way to build COM
objects. But if you have no experience in that area you should definitly get
a book or see if you can get enough information about COM and ATL from MSDN.
 
thank you
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++!

regards
 
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::DoSomeThing(/*[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.
 
many many thanks!
I will try asap.
I dont think i have to adjust some parameter types cause i use just double
and safearray. ( the code was a function exported in a dllwin32 and now
structured as class)
Thank you
Just a last question, do you have some suggestion for a tutorial online for
atl com and vba?
rgds

SvenC said:
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::DoSomeThing(/*[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.
 
Hi,
Just a last question, do you have some suggestion for a tutorial online
for atl com and vba?

I learnded most from books and MSDN.

IIRC this was a good source: Professional ATL COM Programming from Richard
Grimes WROX press. They have also a Beginners book.
 
Back
Top