(对不起我的英文,如果无法理解的话)。
最近我遇到了 Marshal.Copy 功能的问题,我一直在尝试使用格式int [转换int的二维数组。 ]
设计一个函数将这个二维数组转换为另一个,但使用格式 int [] [] ,然后我想应用函数
Marshal.Copy (int [] source, int startIndex, IntPtr destination, int lenght)
当我运行我的程序时,在尝试执行转换时会给我一个错误,因为它告诉我我的参数' destination'为空。
事实是我找不到与问题相关的信息并且需要进行这种转换,因为我使用了一个dll到C,我定义了一个C#结构,定义如下:
C#结构:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct board
{
public int rows;
public int columns;
public int difficulty;
public int boardID;
public IntPtr sudokuArray; // int**
public IntPtr sudokuRestrictions; // int**
}
我的C#转换功能:
public void reverseBoard(int[,] newArray, int[,] newRestrictions)
{
int[][] transformedArray = new int[size][];
int[][] transformedRestrictions = new int[size][];
IntPtr[] ptrSubNewArray = new IntPtr[size];
IntPtr[] ptrSubNewRestrictions = new IntPtr[size];
IntPtr ptrNewArray = new IntPtr();
IntPtr ptrNewRestrictions = new IntPtr();
for (int a = 0; a < size; a++)
{
transformedArray[a] = new int[size];
transformedRestrictions[a] = new int[size];
}
for (int b = 0; b < size; b++)
{
for (int c = 0; c < size; c++)
{
transformedArray[b][c] = newArray[b, c];
transformedRestrictions[b][c] = newRestrictions[b, c];
}
}
for (int i = 0; i < size; i++)
{
ptrSubNewArray[i] = new IntPtr();
ptrSubNewRestrictions[i] = new IntPtr();
// Here throws this exception, because of ptrSubNewArray[i]
// 'System.ArgumentNullException'
Marshal.Copy(transformedArray[i], 0, ptrSubNewArray[i], size);
Marshal.Copy(transformedRestrictions[i], 0, ptrSubNewRestrictions[i], size);
}
Marshal.Copy(ptrSubNewArray, 0, ptrNewArray, size);
Marshal.Copy(ptrSubNewRestrictions, 0, ptrNewRestrictions, size);
actualBoard.sudokuArray = ptrNewArray;
actualBoard.sudokuRestrictions = ptrNewRestrictions;
}
如果有人知道如何解决这个问题,那可能会有用。 感谢。