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;
        }
原文地址:https://www.cnblogs.com/chucklu/p/4848571.html