Passing an Excel Range object to C++ DLL

M

Martin

Hello,
I want to pass an excel selection to a C++ dll in order to get a result
an display it in a cell.
I could allready do it by copying the content of the Range object into
a Double array that is passed by reference to the dll.
Now I would like to avoid the copy, and pass the Range object by
reference to the C++ dll.

Is there a corresponding C++ type (and associated utility functions)
for maniuplating the Range object?

Any link to detailed documentation/example would be appreciated (I do
know really few concerning COM).

Thanks in advance
Martin
 
N

NickHK

Martin,
You can call Windows API functions directly from the worksheet, so make your
VBA Declare statement something similar. So it would be a standard DLL, not
ActiveX.
Need to be able to handle Unicode, or at least calling the APIs this way
needs the W version not the A version.

NickHK
 
M

Martin

Hello Nick,
Thanks for replying! I actually allready have the VBA functions that
call the C++. I'm binding C++ to VBA with the following statment:

Private Declare Sub alterMatrix Lib "TheDLL.dll" Alias
"_alter_matrix@12" (ByRef dTab As Double, ByVal lines As Long, ByVal
col As Long)

And my macro is using it this way:

Private Function ModifyDataset(ByRef theCells As Range)
Dim dataset() As Double

' Get range dimension
nCol = theCells.Columns.Count
nRow = theCells.Rows.Count

' Transform the range object into a dataset
ReDim dataset(nRow - 1, nCol - 1) ' ?????????????????
For r = 0 To nRow - 1
For c = 0 To nCol - 1
' Check datatype
cellVal = theCells(r + 1, c + 1).Value
Select Case VarType(cellVal)
Case Is = vbInteger
dataset(r, c) = CDbl(cellVal)
Case Is = vbLong
dataset(r, c) = CDbl(cellVal)
Case Is = vbSingle
dataset(r, c) = CDbl(cellVal)
Case Is = vbDouble
dataset(r, c) = CDbl(cellVal)
Case Else
MsgBox "The field " & r + 1 & "," & c + 1 & " contains
non numeric data: " & cellVal
dataset(r, c) = 0
End Select
Next
Next

' Call the DLL
alterMatrix dataset(0, 0), nRow, nCol

' Get the data back
For r = 0 To nRow - 1
For c = 0 To nCol - 1
theCells(r + 1, c + 1).Value = dataset(r, c)
Next
Next

End Function

As you may notice, I copy the Range object content (the selected cells)
into an array of double. It works perfectly for my C lib that simply
consider it as a double*. I also made tests that allow to pass strings,
integers to my dll.

I now would like to pass the RANGE object, and read it in the C dll in
order to avoid copying data...

Any advice? I'm trying to read some documentation concerning OLE, but
it's lot of readings for a single object information!

Martin
 
N

NickHK

Martin,
I don't use C++, but..
I assume your DLL does not have a reference to the Excel library. Hence it
has no idea what a Range object and no way to access the values.
So it would seem easiest to pass an array of values, either as you are doing
or a variant (Set varArray=theCells ) if your DLL can accept that.

However, what happens if you pass a range to the DLL that is expecting a
variant or an array ? Do you get automatic conversion of the range to an
array of value ?

But if you are going to check for valid numeric data in VBA, before passing
to the DLL, do you need to change it anyway ?

NickHK
 
M

Martin

NickHK said:
Martin,
I don't use C++, but..
I assume your DLL does not have a reference to the Excel library. Hence it
has no idea what a Range object and no way to access the values.
So it would seem easiest to pass an array of values, either as you are doing
or a variant (Set varArray=theCells ) if your DLL can accept that.

I know it's easier, since this works :) My goal is to minimize COPY of
data. I thus would like to know which Lib, functions, and object I
should use to directly control the Range content.
However, what happens if you pass a range to the DLL that is expecting a
variant or an array ? Do you get automatic conversion of the range to an
array of value ?

There are no automatic stuffs with c++ :)
But if you are going to check for valid numeric data in VBA, before passing
to the DLL, do you need to change it anyway ?

Don't get what you mean!? If there are non numeric values, I display an
error message before calling the DLL.
 
N

NickHK

Martin,
From my limited knowledge, it seems that you DLL needs reference to Excel,
so then passing a range would not be a problem.
But I thought you were trying to avoid that.

NickHK
 
W

webmaster

The easiest way to do this is to use RapidXLL_NET. You can then write
native C / C++ library automatically interface it with both Excel and
..NET with a one liner.

This tool uses the Excel C/C++ API. It is very fast and works around a
union object called an XLOPPER. The original api was created for Excel
95 and has been upgraded a couple of times (newest update for 2007) but
has ALWAYS remained backward compatible. You can look for Excel SDK or
Excel 95 Framework.

Try it free at http://www.RapidXLL.net

Sincerely,

The RapidXLL Team
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top