从VBA发送一个string数组到一个C ++ DLL,然后遍历这些string

我正在制作一个C ++ DLL以允许在Excel中进行串行通信。 这是大部分工作,但我挂在VBA的string数组。 我的DLL的这个特定函数应该包含一个char *数组,一个表示它们长度的整数,一个指向double数组开头的指针,其中我将存储每个string的parsing数据,以及一个整数,表示每个数字中有多less个双精度行(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; } 

所以,如果我只是传递第一个string,双数组部分工作得很好,但是,如果我每次都尝试发送下一个string,那么当我第一次调用DLL函数(我相信这是崩溃的地方)时,它会崩溃。 我没有迭代通过string数组的权利,是不是我认为它的大小,或者在下面的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 

当我传递一个string,而不用担心一个数组,并遍历它。 然后,VBA和Excel工作正常(除了我松散的其他string,我想保留,因为他们被写在对方每次我用一个string,我已经在DLL中调用parseLine)。 我希望能够将每个我分析的string保存到Double数组中,这样当我从DLL返回时,我已经parsing了所有可以显示的string。 但是,当我试图实现这个string数组时,Excel现在只是在尝试调用parseAllLinesFromSerialPort函数时崩溃。 任何想法我在这里做错了吗? 此外,我会采取任何其他的build议来清理我当前的代码,因为我正在做这个项目的工作。