Tag: 编组

将复杂结构编组到c#

我仍然忙着编组一个从c ++到c#的非常复杂的结构。 c ++中的结构如下: typedef struct { DWORD Flags; DWORD TimeCode; DWORD NodeMoving; Matrix NodeRots[NUM_GYROS]; Vector Position; DWORD ContactPoints; float channel[NUM_CHANNELS]; } Frame; 向量: typedef struct { union { struct { float x, y, z; }; float Array[3]; }; } Vector; 矩阵: typedef struct { union { struct { float xx, xy, xz; //This row […]

将Color32 数组快速复制到byte 数组

将Color32[]值array 复制/转换为byte[]缓冲区的快速方法是什么? Color32是Unity 3D的结构,分别包含4 bytes, R, G, B and A respectively 我想要完成的是通过管道将渲染的图像从统一发送到另一个应用程序( Windows Forms )。 目前我正在使用此代码: private static byte[] Color32ArrayToByteArray(Color32[] colors) { int length = 4 * colors.Length; byte[] bytes = new byte[length]; IntPtr ptr = Marshal.AllocHGlobal(length); Marshal.StructureToPtr(colors, ptr, true); Marshal.Copy(ptr, bytes, 0, length); Marshal.FreeHGlobal(ptr); return bytes; } 谢谢,对不起,我是StackOverflow的新手。 Marinescu Alexandru

在C#中尽可能快地将数组复制到struct数组

我正在使用Unity 4.5,将图像作为字节数组(每个字节代表一个通道,每个像素占用4个字节(rgba)并在使用此循环将数组转换为Color32数组的纹理上显示它们: img = new Color32[byteArray.Length / nChannels]; //nChannels being 4 for (int i=0; i< img.Length; i++) { img[i].r = byteArray[i*nChannels]; img[i].g = byteArray[i*nChannels+1]; img[i].b = byteArray[i*nChannels+2]; img[i].a = byteArray[i*nChannels+3]; } 然后,使用以下方法将其应用于纹理: tex.SetPixels32(img); 但是,这会显着降低应用程序的速度(此循环在每一帧上执行),我想知道是否有其他方法可以加快复制过程。 我找到了一些人(使用Marshal.Copy函数快速复制Color32 []数组到byte []数组 )以执行反向过程( Color32到字节数组),但我无法使其工作将字节数组复制到Color32数组。 有人知道更快的方式吗? 先感谢您!

在C#中将字符串数组编组为char **

我正在调用C DLL函数,需要提供以下C结构: typedef struct { char *mTableId; char **mFieldNames; int mNumFields; char *mFilter; char *mSort; int mOffset; int mMaxRecords; char *mTargetRecordFilter; int mSurroundingRecordsCount; int *mOwnerIds; int mNumOwnerIds; gsi_bool mCacheFlag; } SAKESearchForRecordsInput; 问题在于char ** mFieldNames; 我试过像这样自动编组: [MarshalAs(UnmanagedType.LPArray,ArraySubType = UnmanagedType.LPTStr,SizeConst = 9)] public String [] mFieldNames; 这样我在Marshal.SizeOf()中得到一个错误 – 无法计算正确的大小。 然后我决定手动处理指针。 它实际上只是指向C字符串数组的指针。 这是我的代码导致的 System.AccessViolationException:尝试读取或写入受保护的内存。 这通常表明其他内存已损坏。 所以我搞砸了指针。 代码对我来说似乎没问题,bug在哪里? C#: […]

返回从非托管代码到托管代码的指针

我有一个非托管的DLL导出以下函数: SomeData* test(); 我们假设SomeData为: typedef struct _Data Data; struct _Data{ int a; int b; } 现在我想用C#代码调用这个函数。 我开始定义自定义编组所需的C#Struture,如下所示: [StructLayout(LayoutKind.Sequential)] public class SomeData { public Int32 a; public Int32 b; } 现在,我宣布托管函数: [DllImport(“DynamicLibrary.dll”, CharSet=CharSet.Auto)] [return: MarshalAs(UnmanagedType.LPStruct)] public static extern SomeData test(); 在主要function我有: IntPtr ptr = test(); 这样做,我得到MarchalDirectiveException:“无法封送’返回值’:无效的托管/非托管类型组合(Int / UInt必须与SysInt或SysUInt配对)。” 我没有为C#中的SomeData分配内存,因为我希望这个内存在C函数中分配,我会使用Marshal.Copy将它传递给托管内存。 有任何想法吗? 谢谢 ———————— JaredPar ANSWER之后的编辑——————– 事实上,我在将代码复制到我的问题时犯了一个错误。 我使用的真实托管签名是: […]

使用MarshalByRefObject与序列化相比有多贵?

在我的Azure Web角色代码中,我有一个派生自System.Security.Principal.IIdentity的CustomIdentity类。 在某些时候,.NET运行时尝试序列化该类,并且序列化将不起作用 。 试图解决我搜索了很多并找到了这个答案,并试图从MarshalByRefObjectinheritance我的类。 现在,一旦我的CustomIdentity类从MarshalByRefObjectinheritance,就不再有序列化尝试了,我的代码也可以工作。 但是,我想知道使用MarshalByRefObject类的性能影响。 我的代码就像这样运行。 首先,请求进入IIS并传递给身份validation代码,该代码创建CustomIdentity实例并将该实例附加到HTTP上下文。 然后一段时间后,相同的HTTP上下文被传递给ASP.NET处理程序,该处理程序最多访问一次CustomIdentity实例。 CustomIdentity对象在请求期间存在,然后被销毁。 现在通过序列化,我的CustomIdentity将序列化为流,然后从该流反序列化为新对象。 使用MarshalByRefObject ,没有序列化,但是创建了代理,并且访问将通过RPC封送到实际对象所在的位置。 在这种情况下使用MarshalByRefObject会有多贵? 哪个MarshalByRefObject或序列化 – 会更昂贵?

通用BitConverter式方法?

我最近遇到过一种情况,我需要创建一个通用方法来从字节数组中读取数据类型。 我创建了以下类: public class DataStream { public int Offset { get; set; } public byte[] Data { get; set; } public T Read() where T : struct { unsafe { int dataLen = Marshal.SizeOf( typeof( T ) ); IntPtr dataBlock = Marshal.AllocHGlobal( dataLen ); Marshal.Copy( Data, Offset, dataBlock, dataLen ); T type = *( ( […]

将C ++结构编组到C#的最有效方法是什么?

我即将开始阅读大量的二进制文件,每个文件有1000个或更多记录。 不断添加新文件,因此我正在编写Windows服务来监视目录并在收到新文件时对其进行处理。 这些文件是用c ++程序创建的。 我在c#中重新创建了结构定义,可以很好地读取数据,但是我担心我这样做会最终导致我的应用程序死机。 using (BinaryReader br = new BinaryReader(File.Open(“myfile.bin”, FileMode.Open))) { long pos = 0L; long length = br.BaseStream.Length; CPP_STRUCT_DEF record; byte[] buffer = new byte[Marshal.SizeOf(typeof(CPP_STRUCT_DEF))]; GCHandle pin; while (pos < length) { buffer = br.ReadBytes(buffer.Length); pin = GCHandle.Alloc(buffer, GCHandleType.Pinned); record = (CPP_STRUCT_DEF)Marshal.PtrToStructure(pin.AddrOfPinnedObject(), typeof(CPP_STRUCT_DEF)); pin.Free(); pos += buffer.Length; /* Do stuff with my […]

使用C#中的位字段进行编组

是否可以将包含位字段的C风格结构编组到C#结构中,或者您是否必须将其编组为基本类型然后执行位掩码? 例如,我想从这样的C风格结构编组: struct rgb16 { unsigned int R : 4; unsigned int G : 5; unsigned int B : 4; } 并将其编组为以下内容: [StructLayout(LayoutKind.Sequential)] public struct Rgb16 { public byte R; public byte G; public byte B; }

如果GetFields()不保证顺序,LayoutKind.Sequential如何工作

我需要在声明顺序的保证顺序中获取fieldinfo。 现在我正在使用属性来指定顺序。 有更自动的方式吗? 有没有人知道LayoutKind.Sequential如何工作,以及我是否可以应用它的技术。 我没有看到LayoutKind.Sequential如何工作,除非有一些预编译器代码添加属性。