Exemplo n.º 1
0
        public static int Read(IntPtr dh, byte[] buffer, int offset, int count)
        {
            // Allocate an unmanaged memory buffer
            var bufPtr = Marshal.AllocHGlobal(count);
            // Allow GpgMe to fill the unmanaged buffer
            var read = GpgMeWrapper.gpgme_data_read(dh, bufPtr, count);

            // Copy the unmanaged buffer into the managed buffer
            Marshal.Copy(bufPtr, buffer, offset, count);
            // And free the unmanaged buffer
            Marshal.FreeHGlobal(bufPtr);
            return(read);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Read a GpgME data buffer into a C# byte buffer.
        /// </summary>
        public static byte[] ReadBuffer(IntPtr dh, int bufferSize = 4096)
        {
            var allData = new List <byte>();
            int read;

            do
            {
                // Allocate an unmanaged memory buffer
                var bufPtr = Marshal.AllocHGlobal(bufferSize);
                // Allow GpgMe to fill the unmanaged buffer
                read = GpgMeWrapper.gpgme_data_read(dh, bufPtr, bufferSize);

                // Copy the unmanaged buffer into a managed buffer
                var buffer = new byte[bufferSize];
                Marshal.Copy(bufPtr, buffer, 0, bufferSize);
                // And free the unmanaged buffer
                Marshal.FreeHGlobal(bufPtr);
                allData.AddRange(buffer.Take(read));
            } while (read == bufferSize);

            // All those copies are pretty slow, but for now, we'll just have to live with that.
            return(allData.ToArray());
        }