blob.cs

来自「蓝牙传输控件,用于蓝牙文件上传和下载。芯片只要选用crs」· CS 代码 · 共 102 行

CS
102
字号
using System;
using System.Runtime.InteropServices;
using bluetoothX;


namespace bluetoothX
{
    [StructLayout(LayoutKind.Sequential, Size=8)]
    internal struct BLOB
    {
        public int cbSize;
        public IntPtr pBlobData;

        internal BLOB(int size, IntPtr data)
        {
            cbSize = size;
            pBlobData = data;
        }
    }
    
	internal class oBLOB : IDisposable
	{
		private byte[] data = new byte[8];

		private GCHandle m_blobhandle;
		private BTHNS_BLOB m_blob;

		public int cbSize
		{
			get
			{
				return BitConverter.ToInt32(data, 0);
			}
            set
            {
                BitConverter.GetBytes(value).CopyTo(data, 0);
            }
		}

		public object pBlobData
		{
			get
			{
				return m_blob;
			}
            set
            {
                if (value is BTHNS_BLOB)
                {
                    m_blob = (BTHNS_BLOB)value;
                    m_blobhandle = GCHandle.Alloc(((BTHNS_BLOB)value).ToByteArray(), GCHandleType.Pinned);

                    //write to the byte array
                    BitConverter.GetBytes(m_blobhandle.AddrOfPinnedObject().ToInt32()).CopyTo(data, 4);
                }
                else
                {
                    m_blobhandle = GCHandle.Alloc(value, GCHandleType.Pinned);

                    //write to the byte array
                    BitConverter.GetBytes(m_blobhandle.AddrOfPinnedObject().ToInt32()).CopyTo(data, 4);
                }
            }
		}

		public byte[] ToByteArray()
		{
			return data;
		}

		#region IDisposable Members

		protected void Dispose(bool disposing)
		{
			if(m_blobhandle.IsAllocated)
			{
				m_blobhandle.Free();
			}

			if(disposing)
			{
				m_blob = null;
				data = null;
			}
		}

		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		~oBLOB()
		{
			Dispose(false);
		}

		#endregion
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?