从托管 C++ 传递 2D 数组到非托管 C++
我正在为非托管 C++ 代码开发托管 C++ 包装器,并且有一个问题。 为了简单起见,假设我需要将一个 2D 数组从 C# 代码传递到托管 C++,再传递到非托管 C++。我对一维数组没有问题,但坚持使用二维版本。将其转换为一维是一种选择,但我想看看是否还有其他方法。
为了简单起见,假设我想使用中间包装器将 2D 数组发送到非托管代码并更改其值。
所以这里是调用托管 C++ 的 C# 代码:
MNumeric wrapper = new MNumeric(); //managed C++ object
int[,] dArr = new int[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
dArr[i, j] = 10;
}
}
wrapper.ChangeArray(dArr, Convert.ToInt32(Math.Sqrt(dArr.Length)))
托管 C++:
void MNumeric::ChangeArray(cli::array<int,2> ^arr, int mySize)
{
//some code to call Unmanaged C++ passing managed 2D array ???
}
非托管 C++
void UNumeric::ChangeArray(int** arr, int mySize)
{
for(int i=0;i<mySize;i++)
{
for(int j=0;j<mySize;j++)
{
arr[i][j]=i;
}
}
}
非常感谢您的帮助。
看起来我修复了一个错误,但又出现了另一个错误。我的 C++ 托管代码现在看起来像这样。
void MNumeric::ChangeArray(cli::array<int,2> ^arr, int mySize)
{
pin_ptr<int> p_arr = &arr[0,0];
u_num->ChangeArray((int**)p_arr, mySize);
}
其中 u_num 只是指向 UNumeric 类的指针。 我现在得到的错误如下:
尝试读取或写入受保护的内存。这通常表明其他内存已损坏。
任何想法表示赞赏。
I'm working on a managed C++ wrapper for unmanaged C++ code and have a question.
Just for simplicity let's say that I need to pass a 2D array from C# code to Managed C++ to Unmanaged C++. I have no problem with 1D array but stuck with 2D version. Converting it to 1D is the option, but I want to take a look if there are other ways.
For simplicity let say I want to send 2D array to unmanaged code using intermediate wrapper and change its values.
so here is C# code with a call to managed C++:
MNumeric wrapper = new MNumeric(); //managed C++ object
int[,] dArr = new int[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
dArr[i, j] = 10;
}
}
wrapper.ChangeArray(dArr, Convert.ToInt32(Math.Sqrt(dArr.Length)))
Managed C++:
void MNumeric::ChangeArray(cli::array<int,2> ^arr, int mySize)
{
//some code to call Unmanaged C++ passing managed 2D array ???
}
Unmanaged C++
void UNumeric::ChangeArray(int** arr, int mySize)
{
for(int i=0;i<mySize;i++)
{
for(int j=0;j<mySize;j++)
{
arr[i][j]=i;
}
}
}
Thanks a lot for your help.
Looks like I fix one error but got another. My C++ Managed code looks like this now.
void MNumeric::ChangeArray(cli::array<int,2> ^arr, int mySize)
{
pin_ptr<int> p_arr = &arr[0,0];
u_num->ChangeArray((int**)p_arr, mySize);
}
where u_num is just a pointer to UNumeric class.
The error I got now is the following:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Any ideas appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该在此处使用强制转换,它可能会隐藏潜在的重要警告。值得注意的是,二维数组不能转换为
int**
。int**
是一个指向指针数组的指针。int*[]
是一个指向数组的数组的指针。您有一个接受指针数组的函数。它需要一个托管的int[][]
,而不是一个int[,]
。如果你没有进行强制转换,你的编译器会因为你尝试这样做而彻底对你大喊大叫。You should not use a cast here, it could hide potentially important warnings. Notably, a 2D array is not convertible to an
int**
. Anint**
, is a pointer to an array of pointers. Anint*[]
is a pointer to an array of arrays. You have a function that takes an array of pointers. It wants a managedint[][]
, not anint[,]
. Your compiler would thoroughly have shouted at you for attempting this, if you hadn't casted.