473,805 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2184
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 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

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 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

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
9251
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 only exporting to single worksheet. but i need to export data to multiple worksheets. it is very urgent to us. so please help me in code.
3
3970
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 that class OR previously cretaed objects .. So those can be used by users by using the header file ... of my " class "
0
1490
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 managed C# dll before. Currently, I have an unmanaged dll, a managed C++ wrapper dll and a C# dll. I have managed to get enough exported from the unmanaged code to get a vaguely correct picture on the screen, however there is some odd behaviour. ...
2
2264
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 which will use A as its base class.
8
1596
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 would be able to use the classes (and their methods) by calling the objects and invoking their methods - is this possible, given the fact that the C++ compiler decorates names ? looking forward to an informed answer -tkx
2
2417
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 exporting ALL data from the datagrid control. Exporting data works fine when I show all data on the datagrid control. I'd like to shows only 30 records on the datagrid control instead of ALL data using page navigation, and perform exporting...
6
2568
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 select the column orders. Instead the datasource is a class which implements IList, containing a collection of my data. My problem again is that I don't know how to control the order of the columns that are exported.
1
2268
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 composition ? Regards, Sahoo
12
7261
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 and symbols (both C++ and C) from a DLL. In the case of C functions, i also know how to export them by ordinal # - my main problem revolves around how to do the ff: 1). Obtaining the mangled names from the C++ DLL 2). How to map them (if any...
15
7398
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&); private:
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.