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 ...
随机推荐
- Javascript倒计时页面跳转
在js中实现页面定时跳转我们要使用setInterval或setTimeOut函数,还可以使用页面的meta实现. 例1: <script type="text/javascript& ...
- C++ 容器及选用总结
目录 ==================================================== 第一章 容器 第二章 Vector和string 第三章 关联容器 第四章 迭代器 第五 ...
- main函数的正确格式
main函数称之为主函数,一个C程序总是从main()函数开始执行的.在关于C语言的网贴和图书中,可以看到main函数的多种格式,这些格式,有的是正确的,有的是不正确的,为了避免错误,现归纳整理如下. ...
- IOS 学习教程
IOS 学习教程http://www.gaixue.com/course/236#### 讲课http://wenku.baidu.com/view/6786064fe518964bcf847c63. ...
- 因程序问题引起的服务器CPU负荷一直保持在90%以上
昨天早上刚到办公室,就接到客户的电话说其某台小型机的CPU负荷一直保持在90以上,告警短信发个不停,一直没有间断过.该服务器是一台IBM的小型机,性能应该还是不错的,出现这样的情况确实不太正常.登陆上 ...
- 编译andriod源码出错:java.lang.UnsupportedClassVersionError: com/google/doclava/Doclava : Unsupported
问题:java.lang.UnsupportedClassVersionError: com/google/doclava/Doclava : Unsupported update-java-alte ...
- EXTJS4.2 chart 柱状图
chart 柱状图 Ext.require('Ext.chart.*'); Ext.require(['Ext.Window', 'Ext.fx.target.Sprite', 'Ext.layout ...
- IOS成长之路-调用照相机和相册功能(转)
转载自:http://blog.csdn.net/like7xiaoben/article/details/8465237 //先设定sourceType为相机,然后判断相机是否可用(ipod)没相机 ...
- eclipse html插件的下载和安装
需求:需要在eclipse里面编辑html和jsp,语法高亮和语法提示,自动补全等. 1.下载GEF(依赖包): http://www.eclipse.org/downloads/download.p ...
- Jquery实现自动提示下拉框
1.引入脚本库: <script type="text/javascript" src="/Jscripts/jquery-1.3.2.js"&g ...