473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Exporting class in vba

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

Jul 8 '07 #1
7 2164
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.

--
SvenC

Jul 8 '07 #2
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

"SvenC" wrote:
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.

--
SvenC

Jul 8 '07 #3
Hi
i want to export an unmanaged C++ class and use it in VBA
<snip/>
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.

--
SvenC

Jul 8 '07 #4
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

"SvenC" wrote:
Hi
i want to export an unmanaged C++ class and use it in VBA
<snip/>
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.

--
SvenC

Jul 9 '07 #5
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 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.

--
SvenC

Jul 10 '07 #6
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" wrote:
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 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.

--
SvenC

Jul 10 '07 #7
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.

--
SvenC

Jul 10 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: sridevi | last post by:
Hello How to export data from ms-access database to excel worksheet using ASP. mainly i need to export data to multiple worksheets. it is very urgent to us. i have a sample code which works...
3
by: chetan | last post by:
Hi , myself Chetan Is There anybody could help me ? I am working on the project in c++ ,, I am in great confusion that should I export c++ member functions OR methods to create objects of...
0
by: Niall | last post by:
I'm attempting to put a C# front end on my unmanaged C++ ray tracer code. It's still in experimental stage, as I have never done anything with exporting types from an unmanaged dll through to a...
2
by: paul | last post by:
I am exporting a class from a managed dll created in Visual C++ 2005 Express. In A.h header file I have // A.h public ref class A {}; In the same module, I create a new class to be exported...
8
by: Alfonso Morra | last post by:
I am familiar with creating lean and mean WIN32 DLLS and exporting C functions via a DEF file (or cconv decorators), but I am wndering how I can export my C++ objects from my DLLs? Ideally, I...
2
by: bienwell | last post by:
Hi, I have a question about exporting data from datagrid control into Excel file in ASP.NET. On my Web page, I have a linkbutton "Export data". This link will call a Sub Function to perform...
6
by: Opa | last post by:
Hi, I have a DataGrid, whose sourceI am exporting to Excel. This works fine except for the Column ordering. My datasource is not a datatable, with a typical SELECT statement where I can...
1
by: sfs | last post by:
Hi All, Can anyone tell me what is the necessity of exporting the destructor from the DLL ? Is it only necessary when the class is being inherited by another class in another DLL? How about...
12
by: 2b|!2b==? | last post by:
I want to export my C++ classes in a DLL, using ordinal # - rather than by name. Will anyone care to enumerate through the steps required to do this? I am already failiar with exporting classes...
15
by: Grey Alien | last post by:
I have a class that contains a std::map variable. I need to export the class via a DLL. the class looks something like this: class MyClass { public: MyClass(); MyClass(const MyClass&); ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.