T
TIMERESRCH
I've been able to successfully pass a one dimensional array of integers
generated within VBA (by populating an array with random numbers) to a DLL
(written in C). However, I need to pass a 2D array of decimal numbers taken
from the spreadsheet and stored in VBA, but it's not working. I've tried
numerous experiments, and tried to hunt down an answer via Google, etc.
I'm using this code:
Declare Function MatchInteger32 Lib "vbaexts5.DLL" Alias "MatchInteger" (iMatch
As Long, d() As Variant) As Double
'(I'm using Double as the type because the variant array is designated as
Variant/Double in the Locals window, and I'm trying to return one of the
decimal values I'm sending to the DLL as a test.)
Dim d() As Variant
d() = Worksheets("Demo").Range("L11:M46").Value
'(As I understand it, VBA automatically stores a block of numbers from the
spreadsheet as a variant array when presented with a range as above)
iTmp = 2
'The call to the DLL function:
iRes = MatchInteger32(iTmp, d())
'(In this case iTmp is irrelevant, but I've left it in because when I tried to
remove that argument in the DLL function I got an error message when trying to
build the DLL to the effect that "_MatchInteger@8" was unrecognized; this is
part of an essential line of code in the DLL without which VBA can't find the
MatchInteger function in the DLL and returns an error message (453 I believe).
That line of code is: #pragma comment(linker,
"-export:MatchInteger=_MatchInteger@8") I've left iTmp in for completeness
here, but I don't think it has any bearing on the problem of passing a 2D
array.)
The relevant code in the DLL is as follows:
ix[0] = 0 ; ix[1] = 1 ;
if(FAILED(SafeArrayGetElement(*psa, ix, &iNum)))
goto MatchIntegerError;
Each time the DLL is returning the MatchIntegerError value (I've set it at
8000) back to the spreadsheet, so the SafeArrayGetElement command is not
working. Again, everything worked fine when sending a 1D array of integers, so
it has to be something related to sending a two dimensional structure, or the
variant type of the array...I just don't know.
Surely someone must be using DLLs to do fast calculations; I sure hope you can
show me the way. By the way, I'd also like to know how it's possible to debug
the DLL if it's being called from VBA; I'm new to C and it's a mystery to me.
Thanks in advance.
generated within VBA (by populating an array with random numbers) to a DLL
(written in C). However, I need to pass a 2D array of decimal numbers taken
from the spreadsheet and stored in VBA, but it's not working. I've tried
numerous experiments, and tried to hunt down an answer via Google, etc.
I'm using this code:
Declare Function MatchInteger32 Lib "vbaexts5.DLL" Alias "MatchInteger" (iMatch
As Long, d() As Variant) As Double
'(I'm using Double as the type because the variant array is designated as
Variant/Double in the Locals window, and I'm trying to return one of the
decimal values I'm sending to the DLL as a test.)
Dim d() As Variant
d() = Worksheets("Demo").Range("L11:M46").Value
'(As I understand it, VBA automatically stores a block of numbers from the
spreadsheet as a variant array when presented with a range as above)
iTmp = 2
'The call to the DLL function:
iRes = MatchInteger32(iTmp, d())
'(In this case iTmp is irrelevant, but I've left it in because when I tried to
remove that argument in the DLL function I got an error message when trying to
build the DLL to the effect that "_MatchInteger@8" was unrecognized; this is
part of an essential line of code in the DLL without which VBA can't find the
MatchInteger function in the DLL and returns an error message (453 I believe).
That line of code is: #pragma comment(linker,
"-export:MatchInteger=_MatchInteger@8") I've left iTmp in for completeness
here, but I don't think it has any bearing on the problem of passing a 2D
array.)
The relevant code in the DLL is as follows:
ix[0] = 0 ; ix[1] = 1 ;
if(FAILED(SafeArrayGetElement(*psa, ix, &iNum)))
goto MatchIntegerError;
Each time the DLL is returning the MatchIntegerError value (I've set it at
8000) back to the spreadsheet, so the SafeArrayGetElement command is not
working. Again, everything worked fine when sending a 1D array of integers, so
it has to be something related to sending a two dimensional structure, or the
variant type of the array...I just don't know.
Surely someone must be using DLLs to do fast calculations; I sure hope you can
show me the way. By the way, I'd also like to know how it's possible to debug
the DLL if it's being called from VBA; I'm new to C and it's a mystery to me.
Thanks in advance.