View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
NickHK NickHK is offline
external usenet poster
 
Posts: 4,391
Default Passing an Excel Range object to C++ DLL

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

"Martin" wrote in message
oups.com...
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








NickHK wrote:
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

"Martin" wrote in message
oups.com...
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