C#中的Marshal
Const.MaxLengthOfBufferd的长度固定为0x2000 也就是8192
private bool SendMessage(int messageType, string ip, string port, int length, byte[] messageBytes)
{
bool result = false;
try
{
if (windowHandle != )
{
var bytes = new byte[Const.MaxLengthOfBuffer];
Array.Copy(messageBytes, bytes, messageBytes.Length); int sizeOfType = Marshal.SizeOf(typeof(StClientData)); //Step1 把数据封装到结构体中
StClientData stData = new StClientData
{
Ip = GlobalConvert.IpAddressToUInt32(IPAddress.Parse(ip))//客户端ip
,Port = Convert.ToInt16(port)//客户端端口
,Length = Convert.ToUInt32(length)
,Buffer = bytes
}; //Step2 给结构体实例分配空间
//public static int SizeOf(Object structure)
//函数说明:Returns the unmanaged size of an object in bytes.
//structure:The object whose size is to be returned.
//Return Value:The size of the specified object in unmanaged code.
int sizeOfStData = Marshal.SizeOf(stData); //public static IntPtr AllocHGlobal(int cb)
//函数说明:Allocates memory from the unmanaged memory of the process by using the specified number of bytes.
//cb:The required number of bytes in memory.
//Return Value:A pointer to the newly allocated memory. This memory must be released using the Marshal.FreeHGlobal method.
IntPtr pointer = Marshal.AllocHGlobal(sizeOfStData); //把结构体实例赋值到非托管内存中
//public static void StructureToPtr(Object structure,IntPtr ptr,bool fDeleteOld)
//函数说明:Marshals data from a managed object to an unmanaged block of memory.
//structure:A managed object that holds the data to be marshaled. This object must be a structure or an instance of a formatted class.
//ptr:A pointer to an unmanaged block of memory, which must be allocated before this method is called.
//fDeleteOld:true to call the Marshal.DestroyStructure method on the ptr parameter before this method copies the data.
// The block must contain valid data.
// Note that passing false when the memory block already contains data can lead to a memory leak.
Marshal.StructureToPtr(stData, pointer, true); CopyData copyData = new CopyData
{
DwData = (IntPtr) messageType,
CbData = Marshal.SizeOf(sizeOfType),
LpData = pointer
}; SendMessage(windowHandle, WmCopydata, , ref copyData); Marshal.FreeHGlobal(pointer); string data = GlobalConvert.ByteArrayToHexString(messageBytes);
CommunicationManager.Instance.SendDebugInfo(new DataSendEventArgs() {Data = data}); result = true;
}
}
catch (Exception ex)
{
ExceptionLog.Instance.WriteLog(ex, LogType.UI);
}
return result;
}
C#中的Marshal的更多相关文章
- json包中的Marshal&Unmarshal 文档译本
Marshal func Marshal(v interface{})([]byte, error) Marshal returns the JSON encoding of v. Marshal返回 ...
- #python基础学习模块:marshal 对象的序列化
#标准库地址:https://docs.python.org/2/library/marshal.html"""有时候,要把内存中一个对象持久化保存磁盘或者序列化二进制流 ...
- C#+ArcEngine中com对象的释放问题
1.问题描述 最近在写C#下AE的开发,在循环获取数据并修改时碰到了两个问题"超出系统资源"和"超出打开游标最大数":在网上看了一些资料,发现都是说在循环中没有 ...
- 在 golang 中使用 Json
序列化 序列化对象将使用 encoding/json 中的 Marshal 函数. 函数原型为:func Marshal(v interface{}) ([]byte, error) 以下是官网给出的 ...
- python marshal 对象序列化和反序列化
有时候,要把内存中的一个对象持久化保存到磁盘上,或者序列化成二进制流通过网络发送到远程主机上.Python中有很多模块提供了序列化与反序列化的功能,如:marshal, pickle, cPickle ...
- p/invoke 碎片-- 对字符串的处理
字符串在内存中的的几种风格 字符串作为参数和返回值 参考 字符串在内存中的几种风格 所谓的风格,也就是字符串在内存中的存在形式.如何存放的,占据内存的大小,还有存放顺序等.在不同的编程语言和不同的平台 ...
- 使用 Visual Studio 分析器找出应用程序瓶颈(转)
使用 Visual Studio 分析器找出应用程序瓶颈 Hari Pulapaka and Boris Vidolov 本文讨论: 以性能瓶颈为目标 应用程序代码分析 比较分析数据 性能报告 本文使 ...
- VB.NET 内存指针和非托管内存的应用
介绍 Visual Basic 从来不像在C或C++里一样灵活的操纵指针和原始内存.然而利用.NET框架中的structures 和 classes,可以做许多类似的事情.它们包括 IntPtr, ...
- 使用 Visual Studio 分析器找出应用程序瓶颈
VS的性能分析工具 性能分析工具的选择 打开一个“性能分析”的会话:Debug->Start Diagnotic Tools Without Debugging(或按Alt+F2),VS2013 ...
随机推荐
- stm32之ADC学习
1.stm32中采用的是逐次逼近型模拟数字方式,那么什么是逐次逼近呢? 逐次逼近的方式类似于二分法,以8位数据为例:当输入一个模拟量的时候,首先取这8位数的一半,即1000 0000,与模拟量比较,大 ...
- Android Studio 导入第三方jar包
1.先将AS切换到Project 2.在app-main-src下建一个libs目录,将jar包拷到里面 3.右击jar,add as Library
- Oracle bug 使用max或min函数into到一个char类型报字符缓冲区太小的错误
这个BUG出现会报错如下: selectto_char(max(RENEWAL_DATE)) intoM_YEAR_MONTH fromt_renewal_schedule; ORA-06502: P ...
- 【BZOJ 1066】[SCOI2007]蜥蜴
Description 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平 ...
- ASP.NET如何获取根目录的方法汇总
编写程序的时候,经常需要用的项目根目录,自己总结如下: 1.取得控制台应用程序的根目录方法 方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径 方法 ...
- Web前端框架学习成本比较及学习方法
就项目中自己用过的前端框架的学习成本比较与学习心得分享 刚工作时间不长只用过这几个框架下面是难易程度比较: 不论哪个web前端框架, 究其本质都是把页面的数据传递给后台服务器语言(如java)进行处理 ...
- 实验五 Java网络编程及安全
北京电子科技学院 实 验 报 告 课程:移动平台应用开发实践 班级:201592 姓名:曾俊宏 学号:20159210 成绩:___________ 指导老师: ...
- java第三课:分支结构、循环结构
分支结构 1.if后面可以不加{},但是不建议,容易产生误解2.写程序的时候先防屌丝测试,再写逻辑,否则会出错3.if:一条分支 if else:两条分支 if...else if...:多条分支(结 ...
- find 与 tar命令连用
find 与 tar命令连用 今天打包日志时,用 -type f -exec tar -cvf log.tar {} \; 发现只打包了最后一个文件,应该是tar的c参数,每次都创建一个新的文件,想了 ...
- 有关js的变量、作用域和内存问题
来自<javascript高级程序设计 第三版:作者Nicholas C. Zakas>的学习笔记(四) js共有5种基本数据类型:Undefined.NULL.Boolean.Numbe ...