ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Passing Byte array to DLL (https://www.excelbanter.com/excel-programming/360554-passing-byte-array-dll.html)

tom

Passing Byte array to DLL
 
I'm trying to pass an 8 bit array with 80 elements from Excel VBA to a C++
DLL that is a wrapper for National Instruments functions. I get a 'Type
Mismatch' when I compile. I've tried 2 ways to define the array in the DLL:
BYTE and uInt8 (both may be aliases to 'char'). I've tried Byte and Variant
in the VBA with the same results.

If I create a loop in VBA and pass 1 element at a time it works. Does anyone
know why I get the Type Mismatch when I try to pass the whole array?

Mat P:son[_2_]

Passing Byte array to DLL
 
Hi Tom,

a) What does your C++ method signature look like
b) What does your VBA sub/function declaration statement look like?

If you want to transfer byte date then a VBA byte array is your best bet --
the VARIANT is a pretty complex thing, and there's no real need to use it in
your case.

I put together a few lines to give you a feeling for what things could look
like in VBA

' Declare your C++ function(?) something
' like this (byte array passed by reference)
Declare Function MyFunc Lib "MyCppLib" ( _
lpBytes As Byte, _
ByVal nCount As Long) As Long

Sub Test()
' Create a byte array
Dim myBytes(1 To 80) As Byte

' Fill the byte array with something useful...

' Invoke your C++ function -- pass the first elem in the array since it
' will work as a pointer to the whole array, just like in good old C/C++
Dim lRetVal as Long
lRetVal = MyFunc(myBytes(1), 80)

' Check return value...
End Sub



Cheers,
/MP

"Tom" wrote:

I'm trying to pass an 8 bit array with 80 elements from Excel VBA to a C++
DLL that is a wrapper for National Instruments functions. I get a 'Type
Mismatch' when I compile. I've tried 2 ways to define the array in the DLL:
BYTE and uInt8 (both may be aliases to 'char'). I've tried Byte and Variant
in the VBA with the same results.

If I create a loop in VBA and pass 1 element at a time it works. Does anyone
know why I get the Type Mismatch when I try to pass the whole array?


tom

Passing Byte array to DLL
 
Mat,

Thanks for your response. I found my bug by referencing your sample code.

Thanks a bunch!

Tom



"Mat P:son" wrote:

Hi Tom,

a) What does your C++ method signature look like
b) What does your VBA sub/function declaration statement look like?

If you want to transfer byte date then a VBA byte array is your best bet --
the VARIANT is a pretty complex thing, and there's no real need to use it in
your case.

I put together a few lines to give you a feeling for what things could look
like in VBA

' Declare your C++ function(?) something
' like this (byte array passed by reference)
Declare Function MyFunc Lib "MyCppLib" ( _
lpBytes As Byte, _
ByVal nCount As Long) As Long

Sub Test()
' Create a byte array
Dim myBytes(1 To 80) As Byte

' Fill the byte array with something useful...

' Invoke your C++ function -- pass the first elem in the array since it
' will work as a pointer to the whole array, just like in good old C/C++
Dim lRetVal as Long
lRetVal = MyFunc(myBytes(1), 80)

' Check return value...
End Sub



Cheers,
/MP

"Tom" wrote:

I'm trying to pass an 8 bit array with 80 elements from Excel VBA to a C++
DLL that is a wrapper for National Instruments functions. I get a 'Type
Mismatch' when I compile. I've tried 2 ways to define the array in the DLL:
BYTE and uInt8 (both may be aliases to 'char'). I've tried Byte and Variant
in the VBA with the same results.

If I create a loop in VBA and pass 1 element at a time it works. Does anyone
know why I get the Type Mismatch when I try to pass the whole array?



All times are GMT +1. The time now is 04:40 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com