Tag: 编组

C#Marshal.Copy Intptr到16位托管无符号整数数组

为什么C#Marshal.Copy例程没有任何重载从非托管内存指针复制到16位托管无符号整数数组? 例如: Copy(IntPtr, Byte[], Int32, Int32) Copies data from an unmanaged memory pointer to a managed 8-bit unsigned integer array. Copy(IntPtr, Char[], Int32, Int32) Copies data from an unmanaged memory pointer to a managed character array. Copy(IntPtr, Double[], Int32, Int32) Copies data from an unmanaged memory pointer to a managed double-precision floating-point number array. Copy(IntPtr, […]

如何将带有unsigned char *的struct从C#传递给C ++?

我有一些带有struct描述的C ++ dll和一些方法: struct MSG_STRUCT { unsigned long dataSize; unsigned char* data; } 并且function例如: unsigned long ReadMsg( unsigned long msgId, MSG_STRUCT* readMsg) { readMsg->dataSize = someDataSize; readMsg->data = someData; } 所以我想从C#中调用这个函数: [StructLayout(LayoutKind.Sequential)] struct MSG_STRUCT { UInt32 dataSize; byte[] data; } [DllImport(“mydll.dll”)] public static Int32 ReadMsg( UInt32 msgId, ref MSG_STRUCT readMsg); 所以我试着调用C#函数: var readMsg = new […]

Marshal.PtrToStructure()和结构DEVMODE中的char数组有问题

我在使用Marshal.PtrToStructure()从指向DEVMODE类型结构的指针中提取数据时遇到问题。 以下是DEVMODE结构上MSDN条目的链接。 我对此结构的C#实现如下: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct DEVMODE { public const int CCHDEVICENAME = 32; public const int CCHFORMNAME = 32; public unsafe fixed char dmDeviceName [CCHDEVICENAME]; public Int16 dmSpecVersion; public Int16 dmDriverVersion; public Int16 dmSize; public Int16 dmDriverExtra; public DM_FIELD_TYPE dmFields; public Int16 dmOrientation; public Int16 dmPaperSize; public Int16 dmPaperLength; public Int16 […]

将字符串封送到非托管内存,将它们传递给c ++并再次返回c#

我在c#中创建非托管内存块,并用结构中的数据填充它。 我遍历结构列表并执行以下操作: Marshal.StructureToPtr(structTemp, currentMemoryPosition, false); currentMemPosition = new IntPtr(currentMemPosition.ToInt64() + structSize); 该结构包含引用类型:“string”。 我已经研究了BOL的StructureToPtr方法,它说: “All other reference types (for example, strings and arrays) are marshaled to copies” 究竟是什么意思? 这是否意味着对该字符串的引用仍将在内存中,尽管结构的实例将超出范围? 上面的非托管内存块,我传递给使用它的c ++方法。 当在c ++部分上完成作业时,我再次遍历内存中的结构(在c#中)和: Marshal.DestroyStructure(currentMemPosition, typeof(struct)); 对我来说最重要的问题是: Whether I can: 1) Create structs with strings inside 2) Marshal them to unmanaged mamory 3) Make use of them […]

C ++回调到C#函数期间的NullReferenceException

开发商! 我有一个非常奇怪的问题。 我的项目有用C ++编写的DLL和用C#编写的GUI。 我已经实现了一些互操作性的回调。 我计划在某些情况下C ++ DLL会调用C#代码。 它有效…但不长,我不明白为什么。 在C#部分的注释中标出了问题 这里是简化示例的完整代码: C ++ DLL: #include #define WIN32_LEAN_AND_MEAN #include BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } extern “C” { typedef void (*WriteSymbolCallback) (char Symbol); WriteSymbolCallback Test; […]

与Windows7 Display API通信

我还没有找到一个体面的(任何真正的)pinvoke包装新win7 CCD api。 api可以从这里找到: http : //msdn.microsoft.com/en-us/library/windows/hardware/hh406259(v = vs.85).aspx 我花了几个小时来转换结构/方法调用,以便我可以在C#中使用它。 这是第一个“草案”: using System; using System.Runtime.InteropServices; namespace CONVERTING_CCD { /// /// This class takes care of wrapping “Connecting and Configuring Displays(CCD) Win32 API” /// Author Erti-Chris Eelmaa || easter199 at hotmail dot com /// public class CCDWrapper { [StructLayout(LayoutKind.Sequential)] public struct LUID { public uint LowPart; […]

从原生到托管执行自定义编组(ICustomMarshaler)时如何正确释放内存?

我的主要应用程序是在C#中,我现在正在尝试编写一个DLL(用于输入输出),它应该是可移植的(在C或C ++中用于Windows和Linux),并且可以从其他C ++应用程序和我的主要C#应用程序访问。 虽然在我的DLL中我使用C ++来使我的生活更轻松,但我在界面中使用C风格的方法,即我的标题中有外部的“C”{/ *方法* /}。 我认为这比使用C ++ / CLI包装更好(希望这是正确的选择,因为我已经足够了,但我很高兴听到你对这个问题的建议)。 现在在我的本机DLL中,我有结构,其中包含其他几个结构和数组。 我通过以下示例简化(很多): typedef struct AStructNative{ size_t length, void* bstructs; }AStructNative; typedef struct BStructNative{ int16_t a; int16_t b; }BStructNative; 和C#同行: public class AStruct { BStruct[] array; } public struct BStruct { short a; short b; } 我使用ICustomMarshaler , AStruct (1)可以传递给本机DLL(写入时)或(2)可以从DLL获取(回读时)。 我认为我在案例(1)中取得了成功(至少它有效)。 现在,我不清楚案例(2)的正确程序。 在本机DLL上,我有类似于: void ReadAStruct(AStructNative […]

来自C ++的Pinvoke结构转换

以下是一些经过validation的C ++: typedef struct { PVOID buffer; UINT32 length; } DATA_BUFFER; typedef struct { DATA_BUFFER TxBuf [1]; DATA_BUFFER RxBuf [1]; } JVM_COMM_BUFFER; UINT32 SendAndRecv( IN JHI_HANDLE handle, IN CHAR* AppId, INOUT JVM_COMM_BUFFER* pComm ); 以下是我尝试将其移植到C#: [StructLayout(LayoutKind.Sequential)] public struct DATA_BUFFER { public byte[] buffer; public uint length; } [StructLayout(LayoutKind.Sequential)] public struct JVM_COMM_BUFFER { public DATA_BUFFER TxBuf; […]

编组C#结构

我试图序列化以下c#结构: [Serializable] [StructLayout(LayoutKind.Sequential, Size = 70, CharSet = CharSet.Ansi)] public struct USSDContinueModel { [MarshalAs(UnmanagedType.U4)] public uint Command_Length; [MarshalAs(UnmanagedType.U4)] public uint Command_ID; [MarshalAs(UnmanagedType.U4)] public uint Command_Status; [MarshalAs(UnmanagedType.U4)] public uint Sender_ID; [MarshalAs(UnmanagedType.U4)] public uint Receiver_ID; [MarshalAs(UnmanagedType.U1)] public uint Ussd_Version; [MarshalAs(UnmanagedType.U1)] public uint Ussd_Op_Type; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)] public string MsIsdn; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)] public string Service_Code; […]

MarshalAsAttribute字符串数组

我试图从Beckhoff-PLC读取报警结构到ac#class。 首先,我必须在c#中使用完全相同的结构,它目前看起来像这样: [StructLayout(LayoutKind.Sequential, Pack = 1)] public class Alarm { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 81)] public string text; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)] public string objectName; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] public string[] instancePath = new string[6]; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)] public string timeStamp; public int priority; [MarshalAs(UnmanagedType.I1)] public bool acknowledge; [MarshalAs(UnmanagedType.I1)] public bool disabled; [MarshalAs(UnmanagedType.I1)] public bool […]