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. javascript多线程简介

    讲多线程之前,我们先了解一下JS的事件机制 浏览器运行时,脚本必须定期让位给UI进程进行来维持网页的响应,闲置太长时间的脚本可能会被浏览器当成失控脚本,进而造成假死或弹窗 事件触发的设计javascr ...

  2. C# 导出 Excel

    /// <summary> /// 导出Excel /// </summary> public void ExportExcel() { #region 添加引用 Micros ...

  3. event事件:

    onabort: 图像的加载被中断onblur: 元素失去焦点onchange: 域的内容被改变onclick: 当用户点击某个对象时调用的事件句柄ondblclick: 当用户双击某个对象时调用的事 ...

  4. 转发 PHP 资料(一)

    WebShell隐藏思路.webshell磁盘读写动态检测.webshell沙箱动态检测(2)   作为WebShell检测.CMS修复.WebShell攻防研究学习的第二篇文章 本文旨在研究Webs ...

  5. python学习之socket创建html服务器

    #coding:utf-8 import socket def process_request(request): request.recv(1024)#读取接受的字节 request.send(&q ...

  6. LAMP(Ubuntu+apache+mysql+php)+Zend Studio 新手の PHP的开发环境搭建

    因为工作需要,就从c#转型过来研究PHP.可是没想到从一开始就遇上了问题,环境配置方面的问题足足令我头疼了两天.因为博主本人对于linux的接触非常少,所以在解决这个问题的时候也学到了不少东西, 非常 ...

  7. what is the “handover” and "soft handover" in mobile communication system?

    Handover: 切换,属于移动通信网络中的技术术语. 所谓切换,就是指当移动台在通话过程中从一个基站覆盖区移动到另一个基站覆盖区,或者由于外界干扰而造成通话质量下降时,必须改变原有的话音信道而转接 ...

  8. javascript insertBefore 和 appendChild

    js的appendChild()方法 :在一个元素内部追加一个子节点. js的insertBefore()方法:在一个元素内部指定的子节点之前插入子节点. 很明显,appendChild()方法只需要 ...

  9. Battle Over Cities (25)(DFS、连通图)

    It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...

  10. Oracle的AUTOTRACE功能

    ORACLE9i在使用autotrace之前,需要作一些初始设置: 1.用sys用户运行脚本utlxplan.sql创建PLAN_TABLE表 脚本目录:(UNIX:$ORACLE_HOME/rdbm ...