c# 结构体 赋值
场景:C#中结构体数组指针怎么赋值
C#中结构体数组指针如何赋值
结构体定义如下
我想在类DemoClient定义成员变量时定义这个结构体类型的指针
然后怎么给它一个初始地址和一定大小的内存空间。
PS:我是在做C++程序转C#。C++里是这样的
------解决方案--------------------
LPBITMAPINFOHEADER lpbi;就可以了。分配内存的事情你不用考虑,CLR帮你去做。
------解决方案--------------------
//Class
[StructLayout(LayoutKind.Sequential)]
public struct Class
{
public int number;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
public Student[] students; // 结构体数组定义
}
// 调用复杂结构体测试
int size = Marshal.SizeOf(typeof(Class)) * 50;
IntPtr pBuff = Marshal.AllocHGlobal(size); //直接分配50个元素的空间,比Marshal.copy方便多了
------解决方案--------------------
LPBITMAPINFOHEADER d;
LPBITMAPINFOHEADER* pd=&d;
C#中结构体数组指针如何赋值
结构体定义如下
public struct LPBITMAPINFOHEADER
{
public UInt32 biSize;//DWORD=UInt32
public Int32 biWidth;
public Int32 biHeight;
public UInt16 biPlanes;// WORD=UInt16
public UInt16 biBitCount;
public UInt32 biCompression;
public UInt32 biSizeImage;
public Int32 biXPelsPerMeter;
public Int32 biYPelsPerMeter;
public UInt32 biClrUsed;
public UInt32 biClrImportant;
}
我想在类DemoClient定义成员变量时定义这个结构体类型的指针
unsafe static LPBITMAPINFOHEADER* lpbi;
然后怎么给它一个初始地址和一定大小的内存空间。
PS:我是在做C++程序转C#。C++里是这样的
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, FAR *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER;//定义
LPBITMAPINFOHEADER lpbi;//定义
lpbi = (LPBITMAPINFOHEADER)GlobalAlloc(GPTR, 2000 * 2000 * 4 + 5120);//这句赋值是在窗体加载函数里执行的
C#
指针
Structure
C++
------解决方案--------------------
LPBITMAPINFOHEADER lpbi;就可以了。分配内存的事情你不用考虑,CLR帮你去做。
------解决方案--------------------
//Class
[StructLayout(LayoutKind.Sequential)]
public struct Class
{
public int number;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
public Student[] students; // 结构体数组定义
}
// 调用复杂结构体测试
int size = Marshal.SizeOf(typeof(Class)) * 50;
IntPtr pBuff = Marshal.AllocHGlobal(size); //直接分配50个元素的空间,比Marshal.copy方便多了
------解决方案--------------------
LPBITMAPINFOHEADER d;
LPBITMAPINFOHEADER* pd=&d;