封送和拆封 [英] marshaling and unmarshaling
本文介绍了封送和拆封的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是csharp的新手,如何将c结构编组为c Sharp.
任何机构都可以发送示例文件
hi ,
i am new to csharp how to marshal the c structures into c sharp.
any body send the document of examples
public byte[] StructureToByte(object objStructure, int iLength)
{
byte[] destination = new byte[iLength];
IntPtr ptr = Marshal.AllocHGlobal(iLength);
Marshal.StructureToPtr(objStructure, ptr, true);
Marshal.Copy(ptr, destination, 0, iLength);
Marshal.FreeHGlobal(ptr);
return destination;
}
StructureToByte转换的目的是什么.
祝你有美好的一天.
问候
Naga
what is the purpose of StructureToByte convertion.
have a nice day.
regards
Naga
推荐答案
Internet上有很多有关编组的文章和教程.例如这个:
http://www.c-sharpcorner.com/UploadFile/GemingLeader/617/ [ ^ ]
封送处理是将托管内存转换为本地内存(或将本地内存转换为托管内存)的过程,以便.NET应用程序可以与本地应用程序交换数据.
您的函数StructureToByte
将托管结构转换为字节数组.然后可以将此字节数组发送到例如本机DLL.您可以将托管结构直接发送到本机DLL,因为它位于托管内存中(无法从本机代码访问).
There are lots of articles and tutorials on internet about marshaling. for example this one:
http://www.c-sharpcorner.com/UploadFile/GemingLeader/617/[^]
Marshaling is the process of translating managed memory into native memory (or native memory to managed memory) so that .NET applications can exchange data with native applications.
Your functionStructureToByte
converts a managed structure into a byte array. This byte array can then be sent to a native DLL for example. You can send the managed structure directly to the native DLL because it resides in managed memory (not accessible from native code).
谢谢您的答复.
我需要编写结构,并编写示例,如果它是写的或错误的,请检查它.
要求:
消息类型long(4)
MessageTag Long(4)
Tcode Char(7)
密码unsignedcahr(16)
保留的字符
结构的解决方案:
[StructLayout(LayoutKind.Sequential,Pack = 1)]
公共结构样本
{
[MarshalAs(UnmanagedType.U4)]
公共uint MessageType;
[MarshalAs(UnmanagedType.U4)]
公共uint MessageTag;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 7)]
公共字符串代码;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 16)]
公用字符串sPassword;
[MarshalAs(UnmanagedType.I1)]
public char cReserved;
}
我的疑问是正确的,还是不是
[MarshalAs(UnmanagedType.U4)]
公共uint MessageType;
[MarshalAs(UnmanagedType.U4)]
公共ulong MessageType;
请发回您的反馈.
谢谢
naga
hi thanks for your reply.
i have need to write the structure and i wrote the sample , if it is write or wrong please check it.
Requirement:
Messagetype long(4)
MessageTag Long(4)
Tcode Char(7)
Password unsignedcahr(16)
Reserved char
Solution of Structure:
[StructLayout(LayoutKind.Sequential, Pack = 1 )]
public struct sample
{
[MarshalAs(UnmanagedType.U4)]
public uint MessageType;
[MarshalAs(UnmanagedType.U4)]
public uint MessageTag;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7)]
public string Code;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string sPassword;
[MarshalAs(UnmanagedType.I1)]
public char cReserved;
}
My doubt is it correct or instead of
[MarshalAs(UnmanagedType.U4)]
public uint MessageType;
[MarshalAs(UnmanagedType.U4)]
public ulong MessageType;
please send your feed back .
thanks
naga
与该问题有关的另一篇文章,
使用C#/ [
Another article related to the question,
Mastering structs in C#/[^]
:)
这篇关于封送和拆封的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文