c# 平台调用 复杂结构体 指针
来源:互联网 发布:买源码被骗的经历 编辑:程序博客网 时间:2024/05/22 15:49
C结构体定义:
typedef struct _IntImage
{
int height; //图像的高
int width; //宽
int **data; //data:图像二维指针,用法为char** data = new char*[height];
//for (int i=0; i< height; i++)
//data[i] = &(buf[i*width]);
int *buf; //buf:图像数据区,按行存储,buf[0] —buf[width*height]
int variance;
int label;
int bFlip;
}IntImage;
我在C#里定义结构体为:
[DllImport("FaceLib.dll", EntryPoint = "saveIntImage", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int saveIntImage(string filename, ref IntImage intImg);
[StructLayout(LayoutKind.Sequential)]
public unsafe struct IntImage
{
[MarshalAs(UnmanagedType.I4)]
public int height;
[MarshalAs(UnmanagedType.I4)]
public int width;
[MarshalAs(UnmanagedType.ByValArray)]
public IntPtr[] data;
[MarshalAs(UnmanagedType.SysUInt)]
public IntPtr buf;
[MarshalAs(UnmanagedType.I4)]
public int variance;
[MarshalAs(UnmanagedType.I4)]
public int label;
[MarshalAs(UnmanagedType.I4)]
public int bFlip;
}
不知道定义的是否正确?关键是int** data和int* buf两项的定义我不太清除怎么处理。我用了IntPtr buf指向图像存储的缓冲区,IntPtr [] data指向每行数据的首地址。
C里面的调用函数:
int setIntImage(IntImage* img, int value);
我在c#里面由一个由Bitmap图像转换为上述格式定义的图像的函数:
public static void Convert_to_IntImage2(ref IntImage intImage, Bitmap bmp)
{
int i = 0, j = 0, m = 0, n = 0, k = 0;
intImage.height = bmp.Height;
intImage.width = bmp.Width;
Color c = new Color();
int size = (intImage.width) * (intImage.height);
byte[] buffer = new byte[size];
for (i = 0; i < bmp.Width; i++)
for (j = 0; j < bmp.Height; j++)
{
c = bmp.GetPixel(i, j);
buffer[m] = (byte)((39 * c.R + 11 * c.G + 50 * c.B) / 100);
m++;
}
intImage.buf = Marshal.AllocHGlobal(size);
Marshal.Copy(buffer,0,intImage.buf,size);
intImage.data=new IntPtr[bmp.Width];
for (i = 0; i < bmp.Width; i++)
{
intImage.data[i]= Marshal.UnsafeAddrOfPinnedArrayElement(buffer,i*bmp.Height); //获取指定数组指定索引处的地址
}
}
}
在该转换函数里面,不知道结构体中的buf和data如何分配空间和赋值?才能使得给c里面的函数传递参数?出现“尝试读取或写入受保护的内存,这通常指示其他的内存已破坏”。
请问如何平台调用来传递这些复杂的结构体?
- c# 平台调用 复杂结构体 指针
- C#调用DLL复杂函数结构体
- C#调用C++ 平台调用P/Invoke 结构体--含有内置数据类型的一维、二维数组、字符串指针【六】
- C#调用c++Dll结构体数组指针的问题
- C#调用c++Dll结构体数组指针的问题
- C#调用c++Dll结构体数组指针的问题
- C#调用c++Dll结构体数组指针的问题
- c# 调用 c++ dll 参数为结构体数组指针
- C#调用c++Dll结构体数组指针的问题
- C#调用C++ 平台调用P/Invoke 结构体--结构体嵌套【八】
- 平台调用P-INVOKE完全掌握, 结构体和结构体指针
- 平台调用P-INVOKE完全掌握, 结构体和结构体指针
- C#调用C/C++动态库,封装各种复杂结构体。
- C++复杂结构体的C#封装
- c#调用C/C++ DLL,传入指针数组(指针指向自定的结构体)
- C#调用C++ 平台调用P/Invoke 结构体--内存对齐方式、union封装【七】
- C#调用C++ 平台调用P/Invoke 结构体--输入输出参数、返回值、返出值、结构体数组作为参数【五】
- C#调用C++中带结构体指针的方法,憋了3个小时!
- 用json和script标签,解决ajax的跨域问题
- String与Integer 之间的转换
- 第2章 准备——2.4.安装wxWidgets
- EXT中 的身份证逻辑验证
- XML 命名空间
- c# 平台调用 复杂结构体 指针
- 第2章 准备——2.5.安装boost
- 攻击方式学习之(1) - 跨站式脚本(Cross-Site Scripting)
- 一个microsoft的.exe程序文件的启动过程
- 我来找茬 - Google Chrome浏览器七宗罪
- 攻击方式学习之(2) - SQL注入(SQL Injection)
- U.S. hires lawyer for Google/Yahoo inquiry
- JSF与Struts的异同
- 李维的经典话语