也许,有人可以帮助我。以下代码将C#中的二维字符串数组传递给C ++,可以很好地处理GCHandle,它会产生错误:类型' System.ArgumentException'的未处理异常。发生在mscorlib.dll中 附加信息:对象不包含任何原始信息。 实际上,它与双2D完美配合。
public static extern void DDentry
(
[In][MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.BStr)] string[,] arrayReadDat, int iDim1, int iDim2
);
private void button6_Click_1(object sender, EventArgs e)
{
Excel.Application appExcelREAD = new Excel.Application();
appExcelREAD.Visible = true;
string workbookPath = "C:\\Users\\Redstone\\Desktop\\eva_excel_input.xlsx"; // input file
Excel.Workbook excelWorkbook1 = appExcelREAD.Workbooks.Open(workbookPath,
0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
Excel.Worksheet excelWorksheet1 = appExcelREAD.Worksheets["Tabelle1"];
int lastRow = excelWorksheet1.UsedRange.Rows.Count;
int lastCol = excelWorksheet1.UsedRange.Columns.Count;
string[,] arrayReadDat = new string[lastRow+1, lastCol+1];
for (int i = 2; i <= lastRow; i++)
{
for (int j = 1; j <= lastCol; j++)
{
arrayReadDat[i, j] = excelWorksheet1.Cells[i, j].Value2.ToString();
}
}
excelWorkbook1.Close();
appExcelREAD.Quit();
GCHandle gch = GCHandle.Alloc(arrayReadDat, GCHandleType.Pinned);
IntPtr pArrayData = gch.AddrOfPinnedObject();
DDentry(arrayReadDat, lastRow+1, lastCol+1);
gch.Free();