예제 #1
0
        internal static byte[] ConvertHGlobalToByteArray(SafeHGlobalHandle hGlobal)
        {
            int length = SafeNativeMethods.GlobalSize(hGlobal).ToInt32();

            if (length <= 0)
            {
                return(null);
            }
            byte[] destination = new byte[length];
            IntPtr source      = SafeNativeMethods.GlobalLock(hGlobal);

            if (IntPtr.Zero == source)
            {
                throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new OutOfMemoryException());
            }
            try
            {
                Marshal.Copy(source, destination, 0, length);
            }
            finally
            {
                SafeNativeMethods.GlobalUnlock(hGlobal);
            }
            return(destination);
        }
        internal static byte[] ConvertHGlobalToByteArray(SafeHGlobalHandle hGlobal)
        {
            // this has to be Int32, even on 64 bit machines since Marshal.Copy takes a 32 bit integer
            Int32 sizeOfByteArray = (SafeNativeMethods.GlobalSize(hGlobal)).ToInt32();

            if (sizeOfByteArray > 0)
            {
                byte[] byteArray = new Byte[sizeOfByteArray];
                IntPtr pBuff     = SafeNativeMethods.GlobalLock(hGlobal);
                if (IntPtr.Zero == pBuff)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new OutOfMemoryException());
                }

                try
                {
                    Marshal.Copy(pBuff, byteArray, 0, sizeOfByteArray);
                }
                finally
                {
                    SafeNativeMethods.GlobalUnlock(hGlobal);
                }
                return(byteArray);
            }
            return(null);
        }