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 ...
随机推荐
- DirectSound学习(三)--类、方法、属性翻译
DirectSound.Device :Contains methods and properties used to create buffer objects, manage devices, a ...
- HttpClient SSL示例(转)
原文地址: http://www.cnblogs.com/jerry19890622/p/4291053.html package com.jerry.httpclient; import java. ...
- android 软件开机自启动
安卓的很多功能实现方式都是“Don't call me, I'll call you back!”,开机启动就是其中之一 步骤: 1.首先建立一个BroadcastReceiver, 2.在他的onR ...
- 浏览器兼容CSS代码:按钮文字垂直居中(input button text vertical align)
经过测试的浏览器:IE6, IE7, IE8, IE9, Firefox, Chrome, Safiri, Maxthon 按钮的HTML代码: <input id="btn_comm ...
- 1030. Travel Plan (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...
- 1006. Sign In and Sign Out
#include <stdio.h> #include <algorithm> #include <iostream> #include <string.h& ...
- 关于对db_block_gets的理解与实验
实验 一. 自己手动创建的小表 创建一个区大小为 40k SYS@ORCL>show parameter db_block_size NAME ...
- pdf增加水印
/// <summary> /// 为PDF添加水印或背景图片 /// </summary> /// <param name="strSourceFilePat ...
- 一个好用且方便的FastCgi C++库 - FastCgi++
不知道你是不是曾经发愁过使用FastCgi库来使用C++开发Fastcgi程序繁琐而且会与C++ STL代码产生冲突的地方,或者你还是习惯了cout而不是pringf,那这篇文章就可以了解到一个使用的 ...
- 团体程序设计天梯赛-练习集L1-009. *N个数求和
L1-009. N个数求和 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题的要求很简单,就是求N个数字的和.麻烦的是,这些 ...