Excel DNA with C++

917 views
Skip to first unread message

Chintu

unread,
Aug 10, 2012, 10:36:36 AM8/10/12
to Excel-DNA
Hi

I am using VS2008 to develop a C++ DLL which exports some static
public functions. I then use Excel DNA to call these exported
functions directly from Excel (no VBA involved). It is working fine.
Although now, I want to modify one of the exported functions to output
multiple values (say an array of doubles). Given that C++ doesn't
allow functions to return arrays, I tried returning a pointer to an
array, but this isn't working.

Any ideas on how I can return multiple values from a C++ DLL into
Excel when using ExcelDNA.

Many thanks in advance.

Govert van Drimmelen

unread,
Aug 10, 2012, 11:30:11 AM8/10/12
to Excel-DNA
How are you calling the C++ library from your Excel-DNA add-in?

1. From C# using P/Invoke via C exports
2. The C++ is compiled as C++/CLI and referenced from a C# library.
3. The C++ is compiled as C++/CLI and directly used as an
<ExternalLibrary>

For 1. you'd pass a pointer or structure back, and marshal to a .NET
aray manually.
For 2. or 3. you'd probably use the C++/CLI support for array
references.

-Govert

Govert van Drimmelen

unread,
Aug 11, 2012, 7:11:05 PM8/11/12
to Excel-DNA
Hi,

You can create managed (CLI) arrays in your C++/CLI code, using the
'array' keyword.
Some more info on C++/CLI arrays here: http://www.codeproject.com/Articles/7704/Arrays-in-C-CLI

Excel-DNA only supports arrays of type 'Object' or 'double', and
easiest is to use 2D-arrays. The array can be returned directly from
your C++ function - since it is a managed (CLI) array, it will be
garbage collected once Excel has processed it.
So something like this should work (I didn't test it, though):

public ref class MyFunctions
{
public:
static array<double^,2>^ GetCppValues()
{
array<double^,2>^ values = gcnew array<double^,2>(4,2);
values[0,0] = 1.23;
values[1,0] = 2.34;
// ... etc.
return values;
}
}

For strings and other types, use array<Object^,2>^.

> Would XLW be more suited in this instance, since I am using C++. Please
> let me know your views.

Excel-DNA is best for making .NET add-ins for Excel. If you are using C
++ and have no other reason to use .NET, it's probably simpler to not
add it in the mix. Then you'd rather use one of the other C++
solutions: XLW, or http://xll.codeplex.com, or spend the money and get
XLL+.

See also my answer here: http://exceldna.codeplex.com/discussions/371219.

-Govert




On Aug 12, 12:42 am, Chintu <bsnataraj...@gmail.com> wrote:
> > Thanks for the reply.
> > I am using option 3, by directly including the path to the DLL in the .dna
> > file.  When you say using 'array references', do you mean passing the array
> > as a (reference) parameter. I didn't think that this could be done when we
> > calling an XLL function directly from Excel. It would be a great help if
> > you could point me to an example as I am quite new to this side of
> > development (calling DLL functions from Excel).
>
> > Would XLW be more suited in this instance, since I am using C++. Please
> > let me know your views.

Chintu

unread,
Aug 13, 2012, 9:35:50 AM8/13/12
to Excel-DNA
Hi Govert

Thanks for the reply. I have tried the below code based on your
example. There are no compilation errors or warnings. However, for
some reason, only GetCppValues2() gets exported from the (C++) DLL and
is visible to excel. Any ideas why GetCppValues1 is not being seen by
Excel?

public ref class Class1
{
public:
static array<double^,2>^ GetCppValues1()
{
array<double^,2>^ res = gcnew array<double^,2>(2,1);
res[0,0] = 1.23;
res[1,0] = 2.34;
return res;
}

static double GetCppValues2()
{
return 1.23;
}
};

Since I am using Visual C++, I will be happy if I can keep using Excel
DNA to export C++ DLL functions to Excel.

Govert van Drimmelen

unread,
Feb 26, 2013, 7:03:07 AM2/26/13
to Excel-DNA
Hi Kai,

Perhaps it must be:
array<double,2>^
instead?

-Govert


On Feb 26, 1:22 am, Kai <kleinwi...@gmail.com> wrote:
> Hi, Chintu,
>
> I searched across the web and found your post. I am not sure if you still
> follow this. Have you solved this problem. If you did, would you mind share
> with me? I really could not make it work. Thanks!
>
> Kai
Reply all
Reply to author
Forward
0 new messages