zoukankan      html  css  js  c++  java
  • 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 != 0)
                    {
                        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, 0, 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++中的多态性
    sdk环境下数据库访问之ADO
    ADO数据库访问问题
    PopMenu 弹出式菜单(变灰,禁用,激活)
    控制台窗口界面控制设计
    判断整数序列是不是二元查找树的后序遍历结果
    把二元查找树转变成排序的双向链表
    二叉树平衡因子应用举例
    二元查找树转换为它的镜像
    满二叉树先序、中序和后序之间的转换
  • 原文地址:https://www.cnblogs.com/chucklu/p/4848571.html
Copyright © 2011-2022 走看看