将一个字符串数组从VBA发送到C ++ DLL,然后迭代这些字符串

时间:2017-06-14 15:32:17

标签: c++ excel vba excel-vba dll

我正在制作一个C ++ DLL,允许在Excel中进行串行通信。它大部分都在工作,但我在VBA中挂起了字符串数组。我的DLL的这个特定功能应该是一个(char *数组,表示它们长度的整数,指向双数组开头的指针,我将为每个字符串存储解析数据)和一个表示每个字符串中有多少个双精度数的整数line(numRows))

这是DLL代码。

//first read a lines from the serial port and then parse it into dataArray, does this until there is no data in buffer or array is full
//returns how many rows that it read
DLL_EXPORT int WINAPI parseAllLinesFromSerialPort(char* lineBuff, int bufferLength, double* dataArray, int numRows) {
    int currentRow = 0;
    bufferAvailable = true; //it is first assumed that there is available data to be read, this is checked down the line, if false the functions return
    while((bufferAvailable) && (currentRow < numRows)) {
         parseLine((lineBuff + (currentRow*bufferLength)), bufferLength, (dataArray + (currentRow*numColumns)));
         if (bufferAvailable) {
               currentRow++;
         }
    }
    return currentRow;
}

因此,如果我只传递第一个字符串,双数组部分工作正常,但是,如果我每次尝试发送下一个字符串,它只是在我第一次调用DLL函数时崩溃(我相信这是崩溃的地方) )。我没有正确地遍历字符串数组,是不是我认为它的大小,或者在下面的VBA中我将数组初始化错误或将其传递给DLL错误?

这是VBA代码。 具体请参见collectAllDataAvailable()函数,因为这是调用该DLL函数的函数。

Option Explicit

'reads all avialable lines from serial
Public Declare Function parseAllLinesFromSerialPort _
Lib "C:\Users\alexw\Desktop\Parsing\bin\Debug\ArduinoTesterDll.dll" _
(line As String, ByVal length As Integer, dataArray As Double, ByVal numRows As Long) As Long


'Global Variables
Public currentDataRow As Long 'keeps track of the currentRow that data is being read into
Public Const numRows As Long = 100 'number of rows grabed at a time by collectAllDataAvailable
Public Const stringLength As Integer = numRows * 8

'selects then clears the dataCollection
Public Sub clearDataCollectionAndBuffers()
    'clears the whole right side under row 5 to column AZ
    Main.Range("K6:AZ1048575").Select
    Selection.ClearContents
    'Clears the Serial Window
    Main.dataBox.Text = ""
    'sets the next data read row to begin at 6
    currentDataRow = 6
    'clears the serial buffers
    Call clearSerialPort 'See Module 1
End Sub

'collects all rows available, up to 46000, currently 1000 lines, set above as a global numRows
Public Sub collectAllDataAvailable()
    Dim J As Integer ' increment variables for a for loop
    Dim I As Integer

    Dim startingCol As Integer 'for K this would be 11
    startingCol = 11

    Dim numCol As Long 'how many data point (3 for #,#,#)
    Dim numRowsRead As Long 'how many rows are read (were available)
    numCol = getNumColumns 'see module 2 for function getNumColumns

    Dim arrayLength As Long
    arrayLength = (numCol * numRows) 'grabs global numRows lines at a time if available, this is a maximum

    Dim dataArray() As Double
    ReDim dataArray(0 To (arrayLength - 1)) As Double

    Dim lineArray() As String * stringLength 'global, needs to be big enough hold each row od data
    ReDim lineArray(0 To (numRows - 1)) As String * stringLength

    numRowsRead = parseAllLinesFromSerialPort(lineArray(0), Len(lineArray(0)), dataArray(0), numRows)

    For J = 0 To (numRowsRead - 1)
        Main.dataBox.Text = Main.dataBox.Text & lineArray(J)
        For I = 0 To (numCol - 1)
            Main.Cells((J + currentDataRow), (I + startingCol)).Value = dataArray(I + (J * numCol))
        Next I
    Next J

    currentDataRow = currentDataRow + numRowsRead
    'only stoped in Main sheets code for stopDataButton
    If Main.dataCollectionActive Then Application.OnTime Now + TimeValue("00:00:01"), "collectAllDataAvailable"
End Sub

Public Sub writeCommand()
    Dim bytesWritten As Integer
    bytesWritten = writeToSerialPort(Main.Range("Command").Value, Len(Main.Range("Command").Value))
End Sub

当我传递一个字符串时,不要担心数组并迭代它。然后VBA和excel工作正常(除了我放弃其他字符串我想要保留的事实,因为每次我用DLL中的一个字符串调用parseLine时我们都会相互写入)。我希望能够将我正在解析的每个字符串保存到Double数组中,这样当我从DLL返回时,我拥有了所有解析的字符串,以便我可以显示它们。但是,当我尝试实现这个字符串数组时,Excel现在只是在尝试调用parseAllLinesFromSerialPort函数时崩溃。知道我在这里做错了什么吗?此外,我将采取任何其他建议来清理我当前的代码,因为我正在为这项工作做这个项目。

0 个答案:

没有答案