c# 包装到本机 c++代码,包装一个参数,该参数是指向数组的指针
我有以下简单的 C++ 非托管代码 DLL;
extern "C" __declspec(dllexport) void ArrayMultiplier(float (*pointerArray)[3], int scalar, int length);
void ArrayMultiplier(float (*pointerArray)[3], int scalar, int length)
{
for (int i = 0 ; i < length ; length++)
{
for (int j = 0; j < 3; j++)
{
pointerArray[i][j] = pointerArray[i][j] * scalar;
}
}
}
我尝试在 c# 中为上述内容编写以下包装函数:
[DllImport("sample.dll")]
public static extern void ArrayMultiplier(ref float elements, int scalar, int length);
其中 elements 是一个 2 维 3x3 数组:
public float[][] elements =
{
new float[] {2,5,3},
new float [] {4,8,6},
new float [] {5,28,3}
};
上面给出的代码可以编译,但是当调用包装函数时程序崩溃:
Wrapper.ArrayMultiplier(ref elements, scalar, length);
请在这里帮助我,并告诉我出了什么问题使用上面的代码,或者如何为简单的 C++ 函数编写包装器:
void SimpleFunction(float (*pointerToArray)[3]);
提前谢谢大家
I have the following simple DLL in c++ un-managed code;
extern "C" __declspec(dllexport) void ArrayMultiplier(float (*pointerArray)[3], int scalar, int length);
void ArrayMultiplier(float (*pointerArray)[3], int scalar, int length)
{
for (int i = 0 ; i < length ; length++)
{
for (int j = 0; j < 3; j++)
{
pointerArray[i][j] = pointerArray[i][j] * scalar;
}
}
}
I have tried writing the following wrapper function for the above in c#:
[DllImport("sample.dll")]
public static extern void ArrayMultiplier(ref float elements, int scalar, int length);
where elements is a 2 dimentional 3x3 array:
public float[][] elements =
{
new float[] {2,5,3},
new float [] {4,8,6},
new float [] {5,28,3}
};
The code given above compiles, but the program crashes when the wrapper function is called:
Wrapper.ArrayMultiplier(ref elements, scalar, length);
Please help me here, and tell me whats wrong with the code above, or how a wrapper can be written for a simple c++ function:
void SimpleFunction(float (*pointerToArray)[3]);
Thank you all in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几种方法可以做到这一点。
不安全的路线,适用于二维数组(您拥有的):
通过调用
这将以不安全的方式工作,但您需要将输入数组更改为一维数组。无论如何,我认为这是一个更好的主意,但这只是我的想法。
如果您想保证安全,请添加另一个参数,即数组本身的大小,然后进行一些编组。不过,您再次需要进入一维数组:
相反,您想要进行一些编组,如下所示:
然后直接调用该函数:
然后您的 C++ 将更改为:
如果需要,您可以添加检查反对总长度,以确保您不会走到最后,但这将是一个相当大的速度打击(足以不想使用 C++),就好像语句不是免费的一样。
然而,完成所有这些之后,我不得不问 - 为什么不直接在 C# 中执行此操作,并省去编组等互操作服务的麻烦?对于复杂的事情,C++ 往往更快,但对于快速数组遍历,我发现 C# 表现得相当好。一旦它是一维数组,在 C# 中也可以很快:
There are a few ways to do this.
The unsafe route, which works well with 2D arrays (that you have):
called via
That will work in an unsafe way, but you'd need to change your input arrays into 1D arrays. I think that's a better idea anyway, but that's just the way that I think.
If you want to be safe about it, add another parameter that is the size of the array itself, and then do some marshalling. Again, though, you'll need to go into 1D arrays:
Instead, you want to do some marshalling, like so:
Then just call the function directly:
Your C++ would then change to:
If you want, you can add in the check against total length to ensure that you don't walk off the end, but that'll be a pretty big speed hit (enough to want to not use C++), as if statements aren't free.
Having done all of that, however, I have to ask-- why not just do this directly in C#, and save yourself the hassle of interop services like marshalling? C++ tends to be faster for complicated things, but for a quick array walk, I've seen C# behave pretty well. It can be pretty quick in C# too, once it's a 1D array: