Home All Groups Group Topic Archive Search About

Can subroutine in VB6 DLL accept array variable argument???

Author
13 Oct 2005 10:26 AM
Felix Cheang
Hi,

I would like to know if it's possible to pass array variable to a VB
DLL. I managed to create an ActiveX DLL on VB and convert it to Windows
DLL by including a .def file to the VB linker.

I have no problem passing variable to the functions in the VB DLL. When
comes to passing array to the subroutine, my program crashes. What I
want to do is to have a subroutine in the DLL file to do some processes
and update an array of variable (which I passed to it) with data so that
EXE can make use of these array variables' data.

Below is one of a sub in the DLL file that I created,

****************************************
Public Sub WriteReport(OutputReportData() As Byte)

'Send data to the device.

Dim Count As Integer
Dim NumberOfBytesRead As Long
Dim NumberOfBytesToSend As Long
Dim NumberOfBytesWritten As Long
Dim ReadBuffer() As Byte
Dim SendBuffer() As Byte

'The SendBuffer array begins at 0, so subtract 1 from the number of
bytes.

ReDim SendBuffer(Capabilities.OutputReportByteLength - 1)

'***********************************************************************
*******
'WriteFile
'Sends a report to the device.
'Returns: success or failure.
'Requires: the handle returned by CreateFile and
'The output report byte length returned by HidP_GetCaps
'***********************************************************************
*******

'The first byte is the Report ID

SendBuffer(0) = 0

'The next bytes are data

For Count = 1 To Capabilities.OutputReportByteLength - 1
    SendBuffer(Count) = OutputReportData(Count - 1)
Next Count

NumberOfBytesWritten = 0

Result = WriteFile _
    (HIDHandle, _
    SendBuffer(0), _
    CLng(Capabilities.OutputReportByteLength), _
    NumberOfBytesWritten, _
    0)
'Call DisplayResultOfAPICall("WriteFile")

'lstResults.AddItem " OutputReportByteLength = " &
Capabilities.OutputReportByteLength
'lstResults.AddItem " NumberOfBytesWritten = " & NumberOfBytesWritten
'lstResults.AddItem " Report ID: " & SendBuffer(0)
'lstResults.AddItem " Report Data:"

'For Count = 1 To UBound(SendBuffer)
'    lstResults.AddItem " " & Hex$(SendBuffer(Count))
'Next Count

End Sub
***********************************************

The DLL crashed when it execute the following statement,

For Count = 1 To Capabilities.OutputReportByteLength - 1
    SendBuffer(Count) = OutputReportData(Count - 1)
Next Count

It gives type mismatch. I believe the mismatch should be on the
OutputReportData(). Of course on the EXE program declaration part, I
declare OutputReportData by reference as follow,

Public Declare Sub WriteReport Lib "USBLib.dll" (ByRef OutputReportData
As Byte)

I declared OutputReportData as public array,

Public OutputReportData(8) As Byte

On the calling sub statement side, I passed the OutputReportData array
argument to the DLL subroutine as follow,

WriteReport OutputReportData(0)

Can someone tell me what's wrong with my code?



*** Sent via Developersdex http://www.developersdex.com ***

Author
13 Oct 2005 3:12 PM
Chris Dunaway
Felix Cheang wrote:

I don't know for certain what is causing your problem, but perhaps
these thoughts might give you some ideas:

> Below is one of a sub in the DLL file that I created,
>
> ****************************************
> Public Sub WriteReport(OutputReportData() As Byte)
>
> Public Declare Sub WriteReport Lib "USBLib.dll" (ByRef OutputReportData
> As Byte)

Your declare for WriteReport does not match the sub.  The sub expects
an argument of type 'array of Byte' whereas your declare is only
specifying a single byte.  Perhaps this is just a typo when you posted
the message.

>
> WriteReport OutputReportData(0)
>

Here, when calling the WriteReport you are only passing in a single
byte and not the array.  I believe you need:

WriteReport OutputReportData

Good Luck.
Author
14 Oct 2005 10:51 AM
Mark Yudkin
An array in VB6 is not conformant with a native array in C, but is a
SAFEARRAY. Since your API doesn't accept a SAFEARRAY, you cannot use a VB6
array. Passing (0) sort of fools the C API, but not the VB6 code.

Show quoteHide quote
"Felix Cheang" <felix-tm_che***@agilent.com> wrote in message
news:ef6FKC%23zFHA.3180@TK2MSFTNGP14.phx.gbl...
> Hi,
>
> I would like to know if it's possible to pass array variable to a VB
> DLL. I managed to create an ActiveX DLL on VB and convert it to Windows
> DLL by including a .def file to the VB linker.
>
> I have no problem passing variable to the functions in the VB DLL. When
> comes to passing array to the subroutine, my program crashes. What I
> want to do is to have a subroutine in the DLL file to do some processes
> and update an array of variable (which I passed to it) with data so that
> EXE can make use of these array variables' data.
>
> Below is one of a sub in the DLL file that I created,
>
> ****************************************
> Public Sub WriteReport(OutputReportData() As Byte)
>
> 'Send data to the device.
>
> Dim Count As Integer
> Dim NumberOfBytesRead As Long
> Dim NumberOfBytesToSend As Long
> Dim NumberOfBytesWritten As Long
> Dim ReadBuffer() As Byte
> Dim SendBuffer() As Byte
>
> 'The SendBuffer array begins at 0, so subtract 1 from the number of
> bytes.
>
> ReDim SendBuffer(Capabilities.OutputReportByteLength - 1)
>
> '***********************************************************************
> *******
> 'WriteFile
> 'Sends a report to the device.
> 'Returns: success or failure.
> 'Requires: the handle returned by CreateFile and
> 'The output report byte length returned by HidP_GetCaps
> '***********************************************************************
> *******
>
> 'The first byte is the Report ID
>
> SendBuffer(0) = 0
>
> 'The next bytes are data
>
> For Count = 1 To Capabilities.OutputReportByteLength - 1
>    SendBuffer(Count) = OutputReportData(Count - 1)
> Next Count
>
> NumberOfBytesWritten = 0
>
> Result = WriteFile _
>    (HIDHandle, _
>    SendBuffer(0), _
>    CLng(Capabilities.OutputReportByteLength), _
>    NumberOfBytesWritten, _
>    0)
> 'Call DisplayResultOfAPICall("WriteFile")
>
> 'lstResults.AddItem " OutputReportByteLength = " &
> Capabilities.OutputReportByteLength
> 'lstResults.AddItem " NumberOfBytesWritten = " & NumberOfBytesWritten
> 'lstResults.AddItem " Report ID: " & SendBuffer(0)
> 'lstResults.AddItem " Report Data:"
>
> 'For Count = 1 To UBound(SendBuffer)
> '    lstResults.AddItem " " & Hex$(SendBuffer(Count))
> 'Next Count
>
> End Sub
> ***********************************************
>
> The DLL crashed when it execute the following statement,
>
> For Count = 1 To Capabilities.OutputReportByteLength - 1
>    SendBuffer(Count) = OutputReportData(Count - 1)
> Next Count
>
> It gives type mismatch. I believe the mismatch should be on the
> OutputReportData(). Of course on the EXE program declaration part, I
> declare OutputReportData by reference as follow,
>
> Public Declare Sub WriteReport Lib "USBLib.dll" (ByRef OutputReportData
> As Byte)
>
> I declared OutputReportData as public array,
>
> Public OutputReportData(8) As Byte
>
> On the calling sub statement side, I passed the OutputReportData array
> argument to the DLL subroutine as follow,
>
> WriteReport OutputReportData(0)
>
> Can someone tell me what's wrong with my code?
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***