编组包含int和int []的结构从C#到C
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了编组包含int和int []的结构从C#到C,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2763字,纯文字阅读大概需要4分钟。
内容图文
我有一个带有非托管代码和C#UI的C DLL.有一个从C DLL导入的函数,它将一个由我编写的结构作为参数.
在将由我编写的结构(MyImage)从C#编组到C之后,我可以访问其中的int []数组的内容,但内容是不同的.我不知道我在这里失踪了什么,因为我花了很长时间并尝试了一些技巧来解决这个问题(显然还不够).
C#中的MyImage结构:
[StructLayout(LayoutKind.Sequential)]
struct MyImage
{
public int width;
public int height;
public int[] bits; //these represent colors of image - 4 bytes for each pixel
}
C中的MyImage结构:
struct MyImage
{
int width;
int height;
Color* bits; //typedef unsigned int Color;
MyImage(int w, int h)
{
bits = new Color[w*h];
}
Color GetPixel(int x, int y)
{
if (x or y out of image bounds) return UNDEFINED_COLOR;
return bits[y*width+x];
}
}
使用MyImage作为参数的C#函数声明:
[DLLImport("G_DLL.dll")]
public static extern void DisplayImageInPolygon(Point[] p, int n, MyImage texture,
int tex_x0, int tex_y0);
C实现
DLLEXPORT void __stdcall DisplayImageInPolygon(Point *p, int n, MyImage img,
int imgx0, int imgy0)
{
//And below they have improper values (i don't know where they come from)
Color test1 = img.GetPixel(0,0);
Color test2 = img.GetPixel(1,0);
}
因此,在调试问题时,我注意到c struct中的MyImage.bits数组包含不同的数据.
我该如何解决?
解决方法:
由于bits字段是指向本机代码中分配的内存的指针,因此您需要在C#代码中将其声明为IntPtr.
struct MyImage
{
public int width;
public int height;
public IntPtr bits;
}
如果要访问C#代码中的单个像素,则需要编写GetPixel方法,就像在C代码中一样.
请注意,由于bits字段是指向本机代码中分配的内存的指针,因此我希望实际代码具有调用delete []位的struct的析构函数.否则你的代码会泄漏.
这也意味着您将需要在本机代码中创建和销毁实例,而不会在托管代码中执行此操作.这是您目前遵循的政策吗?我怀疑不是基于我在这里看到的代码.
您还需要重新考虑按值传递结构.当你打电话给那个功能时,你真的想要复制它吗?这样做意味着你有两个struct实例,其bits字段都指向同一个内存.但是,哪一个拥有那个记忆?这个结构确实需要通过引用传递.
我认为你的设计遇到了一些问题,但是我看不到足够的代码,或者对你的问题了解得足以给你具体的建议.
在评论中,您声明您的主要目标是将这些位从C#代码传输到C代码.我建议你这样做:
MyImage* NewImage(int w, int h, Color* bits)
{
MyImage* img = new MyImage;
img->w = w;
img->h = h;
img->bits = new Color[w*h];
for (int i=0; i<w*h; i++)
img->bits[i] = bits[i];
return img;
}
void DeleteImage(MyImage* img)
{
delete[] img->bits;
delete img;
}
void DoSomethingWithImage(MyImage* img)
{
// do whatever it is you need to do
}
在C#端你可以像这样声明它:
[DllImport(@"dllname.dll", CallingConvention=CallingConvention.Cdecl)]
static extern IntPtr NewImage(int w, int h, int[] bits);
[DllImport(@"dllname.dll", CallingConvention=CallingConvention.Cdecl)]
static extern void DeleteImage(ImtPtr img);
[DllImport(@"dllname.dll", CallingConvention=CallingConvention.Cdecl)]
static extern void DoSomethingWithImage(ImtPtr img);
内容总结
以上是互联网集市为您收集整理的编组包含int和int []的结构从C#到C全部内容,希望文章能够帮你解决编组包含int和int []的结构从C#到C所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。