Tag: marshalling

内存中.NET值类型的布局

我有以下.NET值类型: [StructLayout(LayoutKind.Sequential)] public struct Date { public UInt16 V; } [StructLayout(LayoutKind.Sequential)] public struct StringPair { public String A; public String B; public String C; public Date D; public double V; } 我有代码将指向值类型的指针传递给非托管代码,以及通过调用System.Runtime.InteropServices.Marshal.OffsetOf发现的偏移量。 非托管代码填充Date和double值。 为StringPair结构报告的偏移正是我所期望的:0,8,16,24,32 我在测试函数中有以下代码: FieldInfo[] fields = typeof(StringPair).GetFields(BindingFlags.Instance|BindingFlags.Public); for ( int i = 0; i > field {0} @ offset {1}”, fields[i].Name, offset)); } […]

.net中的转换:本机Utf-8 托管字符串

我创建了这两个方法来将Native utf-8字符串(char *)转换为托管字符串,反之亦然。 以下代码完成了这项工作: public IntPtr NativeUtf8FromString(string managedString) { byte[] buffer = Encoding.UTF8.GetBytes(managedString); // not null terminated Array.Resize(ref buffer, buffer.Length + 1); buffer[buffer.Length – 1] = 0; // terminating 0 IntPtr nativeUtf8 = Marshal.AllocHGlobal(buffer.Length); Marshal.Copy(buffer, 0, nativeUtf8, buffer.Length); return nativeUtf8; } string StringFromNativeUtf8(IntPtr nativeUtf8) { int size = 0; byte[] buffer = {}; do { […]

这些C ++结构的C#等价物是什么

typedef union _Value { signed char c; unsigned char b; signed short s; unsigned short w; signed long l; unsigned long u; float f; double *d; char *p; } Value; typedef struct _Field { WORD nFieldId; BYTE bValueType; Value Value; } Field; typedef struct _Packet { WORD nMessageType; WORD nSecurityType; BYTE bExchangeId; BYTE bMarketCenter; int […]

正确的方法来组织SIZE_T *?

我有以下C ++函数定义,我试图通过托管代码通过PInvoke调用: bool FooBar(SIZE_T* arg1); 我的管理声明如下: [DllImport(“mydll”, SetLastError=true, CharSet=CharSet.Unicode)] private static extern bool FooBar(ref uint arg1); 有些人可能会注意到我最终做的同样的错误。 这不是64位便携式。 SIZE_T的大小可变(32-64位),指针也是如此。 在托管大小上,指针正确转换为64位,但uint没有,并且您最终可以在arg1的高位中使用垃圾。 这是一个特别持久的错误,因为垃圾通常只是零:( 我已经开始工作的唯一解决方案是以下管理声明: [DllImport(“mydll”, SetLastError=true, CharSet=CharSet.Unicode)] private static extern bool FooBar(ref IntPtr arg1); 这当然有效,因为IntPtr可以正确地改变它的大小。 在我的代码中,我只是将IntPtr视为一个整数,它可以工作,虽然它看起来像一个丑陋的黑客。 在我看来,应该有一些方法来正确指定,也许使用UnmanagedType.SysUInt,但我一直无法提出任何其他工作解决方案。

如何编组一个可变大小的结构数组? C#和C ++互操作帮助

我有以下C ++结构 struct InnerStruct { int A; int B; }; struct OuterStruct { int numberStructs; InnerStruct* innerStructs; }; 还有一个C ++函数 OuterStruct getStructs(); 我如何将其编组为C#? C#定义的位置 struct OuterStruct { InnerStruct[] innerStructs; };

在编组带有字符串的struct时出现PInvoke错误

我有一个C ++结构 struct UnmanagedStruct { char* s; // Other members }; 和一个C#结构 struct ManagedStruct { [MarshalAs(UnmanagedType.LPStr)] string s; // Other members } C ++库暴露 extern “C” UnmanagedStruct __declspec(dllexport) foo( char* input ); 而它是导入的 [DllImport(“SomeDLL.dll”, CharSet = CharSet.Ansi)] static extern ManagedStruct foo( string input ); 但是,当我调用此函数时,我得到了 MarshalDirectiveException未处理 方法的类型签名与PInvoke不兼容。 问题是,如果我从结构中删除char * s和字符串s,则此函数调用有效。

“无效的托管/非托管类型组合”是什么意思?

我有以下结构: [StructLayout(LayoutKind.Auto,Pack=0)] private unsafe struct BIRDSYSTEMCONFIG { public byte bySystemStatus; public byte byError; public byte byNumDevices; public byte byNumServers; public byte byXmtrNum; public ushort wXtalSpeed; public double dMeasurementRate; public byte byChassisNum; public byte byNumChassisDevices; public byte byFirstDeviceNum; public ushort wSoftwareRev; public fixed byte byFlockStatus[127]; } 基于C ++结构: typedef struct tagBIRDSYSTEMCONFIG { BYTE bySystemStatus; // current […]

Marshal包含可变长度数组的C结构

我想将一个带有可变长度数组的C结构编组回C#,但到目前为止,我无法获得比指向结构表示和浮点指针更好的结果。 未管理的代表: typedef float smpl_t; typedef struct { uint_t length; /**< length of buffer */ smpl_t *data; /**< data vector of length ::fvec_t.length */ } fvec_t; 管理代表: [StructLayout(LayoutKind.Sequential)] public unsafe struct fvec_t1 { public uint length; public float* data; } [DllImport(“libaubio-4.dll”, EntryPoint = “new_fvec”, PreserveSig = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public static […]

在c#中编组c结构

我有一个’C”DLL’可以将结构作为输入,我必须从我的c#程序中调用该DLL 以下是我在c中的示例结构,我必须在我的c#代码中编组这些结构 我在做对还是错? 实际C结构: typedef struct procedure { char code[8]; }procedure; typedef struct datefield { char date[10]; }datefield; typedef struct p45_clsgs { procedure p45_clsg; datefield p45_clsgdte; }p45_clsgs; C#: [StructLayout(LayoutKind.Sequential), Serializable] struct procedure { //char code[]; [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 8)] public string code; } [StructLayout(LayoutKind.Sequential), Serializable] struct datefield { //char date[10]; [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 10)] public […]

从const char *复制到字节数组C ++ / c #interop Marshal :: Copy

我正在尝试将C ++中的图像发送到C#,并使用C ++管理的interop(marshaling)。 image->getStream()从字符串返回一个const char* 。 我的Marshal::Copyfunction有exception。 mscorlib.dll中发生了未处理的’System.AccessViolationException’类型exception 附加信息:尝试读取或写入受保护的内存。 这通常表明其他内存已损坏。 我是否正在为从const char*到字节数组的副本做正确的事情? 我的dll是在VS2010中使用ASCII字符集编译的。 array^ OsgViewer::getLastImage() { array^ byteArray; m_ImageQueue->lock(); int index = m_ImageQueue->getCurrentImageIndex(); std::shared_ptr image = m_ImageQueue->getImage(static_cast(index)); if( image && image->isValid() == true) { int wLen = image->getStreamSize(); char* wStream = const_cast(image->getStream()); byteArray = gcnew array(wLen); // convert native pointer to System::IntPtr with C-Style cast […]