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的更多相关文章

  1. json包中的Marshal&Unmarshal 文档译本

    Marshal func Marshal(v interface{})([]byte, error) Marshal returns the JSON encoding of v. Marshal返回 ...

  2. #python基础学习模块:marshal 对象的序列化

    #标准库地址:https://docs.python.org/2/library/marshal.html"""有时候,要把内存中一个对象持久化保存磁盘或者序列化二进制流 ...

  3. C#+ArcEngine中com对象的释放问题

    1.问题描述 最近在写C#下AE的开发,在循环获取数据并修改时碰到了两个问题"超出系统资源"和"超出打开游标最大数":在网上看了一些资料,发现都是说在循环中没有 ...

  4. 在 golang 中使用 Json

    序列化 序列化对象将使用 encoding/json 中的 Marshal 函数. 函数原型为:func Marshal(v interface{}) ([]byte, error) 以下是官网给出的 ...

  5. python marshal 对象序列化和反序列化

    有时候,要把内存中的一个对象持久化保存到磁盘上,或者序列化成二进制流通过网络发送到远程主机上.Python中有很多模块提供了序列化与反序列化的功能,如:marshal, pickle, cPickle ...

  6. p/invoke 碎片-- 对字符串的处理

    字符串在内存中的的几种风格 字符串作为参数和返回值 参考 字符串在内存中的几种风格 所谓的风格,也就是字符串在内存中的存在形式.如何存放的,占据内存的大小,还有存放顺序等.在不同的编程语言和不同的平台 ...

  7. 使用 Visual Studio 分析器找出应用程序瓶颈(转)

    使用 Visual Studio 分析器找出应用程序瓶颈 Hari Pulapaka and Boris Vidolov 本文讨论: 以性能瓶颈为目标 应用程序代码分析 比较分析数据 性能报告 本文使 ...

  8. VB.NET 内存指针和非托管内存的应用

    介绍 Visual Basic 从来不像在C或C++里一样灵活的操纵指针和原始内存.然而利用.NET框架中的structures 和 classes,可以做许多类似的事情.它们包括 IntPtr,   ...

  9. 使用 Visual Studio 分析器找出应用程序瓶颈

    VS的性能分析工具 性能分析工具的选择 打开一个“性能分析”的会话:Debug->Start Diagnotic Tools Without Debugging(或按Alt+F2),VS2013 ...

随机推荐

  1. 【CLR VIA C#】读书笔记

    工作几年了才看,记录下笔记备忘. 章节 笔记 1.CLR的执行模型 公共语言运行时(Common Language Runtime,CLR) 源代码-->编译器检查语法和分析源代码-->托 ...

  2. 【原】Oracle查询指定表里的触发器

    select * from all_triggers WHERE table_name='表名'

  3. ics OverbyteIcsHttpProt

    else begin { V7.05 begin } if (FRequestType = httpPOST) or (FRequestType = httpPUT) then begin {$IFD ...

  4. Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化

    链接:http://codeforces.com/contest/665/problem/E 题意:求规模为1e6数组中,连续子串xor值大于等于k值的子串数: 思路:xor为和模2的性质,所以先预处 ...

  5. 【BZOJ 2242】[SDOI2011]计算器

    Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...

  6. Jetty 与 Tomcat 比较,及性能分析

    主流java的web容器,主要是Tomcat, jboss, jetty, resin.由于以前我们主要用的是jboss4.0.5,但jbosse用的servlet容器是tomcat5.5,所以只进行 ...

  7. swift基础--数组、字典

    (1)初始化 (2)新增.修改.删除 (3)清空 (4)遍历 var array1 = ["x","y","z"] var array2:[ ...

  8. 第二好用的时间日期选择插件(jscal)

    这个是第二好用的了,支持鼠标滚动选择时间.功能很强大,文档:http://www.dynarch.com/jscal/ 效果图: <!DOCTYPE html PUBLIC            ...

  9. UOJ Round #8 赴京赶考 解题报告

    算法零 $n,m \le 100, q \le 10$ 的话,直接给网格中的每一个格点都建一个点,然后该怎么最短路就怎么最短路,该怎么并查集+BFS就怎么并查集+BFS. 复杂度 $O(qnm)$,可 ...

  10. [转载]jquery获取元素索引值index()方法:

    jquery的index()方法 搜索匹配的元素,并返回相应元素的索引值,从0开始计数. 如果不给 .index() 方法传递参数,那么返回值就是这个jQuery对象集合中第一个元素相对于其同辈元素的 ...