GlobalLock() private method

private GlobalLock ( SafeGlobalAllocHandle h ) : IntPtr
h SafeGlobalAllocHandle
return System.IntPtr
Esempio n. 1
0
        internal static IntPtr CopyHGlobal(IntPtr data)
        {
            IntPtr src    = UnsafeNativeMethods.GlobalLock(data);
            int    size   = UnsafeNativeMethods.GlobalSize(data);
            IntPtr ptr    = Marshal.AllocHGlobal(size);
            IntPtr buffer = UnsafeNativeMethods.GlobalLock(ptr);

            try
            {
                for (int i = 0; i < size; i++)
                {
                    byte val = Marshal.ReadByte(new IntPtr((long)src + i));

                    Marshal.WriteByte(new IntPtr((long)buffer + i), val);
                }
            }
            finally
            {
                if (buffer != IntPtr.Zero)
                {
                    UnsafeNativeMethods.GlobalUnLock(buffer);
                }

                if (src != IntPtr.Zero)
                {
                    UnsafeNativeMethods.GlobalUnLock(src);
                }
            }
            return(ptr);
        }
Esempio n. 2
0
        public static SafeGlobalAllocHandle CopyHGlobal(SafeGlobalAllocHandle data)
        {
            IntPtr  src  = UnsafeNativeMethods.GlobalLock(data);
            UIntPtr size = UnsafeNativeMethods.GlobalSize(data);
            SafeGlobalAllocHandle ptr = UnsafeNativeMethods.GlobalAlloc(0, size);
            IntPtr buffer             = UnsafeNativeMethods.GlobalLock(ptr);

            try
            {
                UnsafeNativeMethods.MoveMemory(buffer, src, size);
            }
            finally
            {
                if (buffer != IntPtr.Zero)
                {
                    UnsafeNativeMethods.GlobalUnlock(ptr);
                }

                if (src != IntPtr.Zero)
                {
                    UnsafeNativeMethods.GlobalUnlock(data);
                }
            }

            return(ptr);
        }
Esempio n. 3
0
        private IntPtr PackageSelectionData(StringBuilder sb, bool addEndFormatDelimiter)
        {
            if (sb == null || sb.ToString().Length == 0 || this.ItemsDraggedOrCutOrCopied.Count == 0)
            {
                return(IntPtr.Zero);
            }

            // Double null at end.
            if (addEndFormatDelimiter)
            {
                if (sb.ToString()[sb.Length - 1] != '\0')
                {
                    sb.Append('\0');
                }
            }

            // We request unmanaged permission to execute the below.
            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();

            _DROPFILES df         = new _DROPFILES();
            int        dwSize     = Marshal.SizeOf(df);
            Int16      wideChar   = 0;
            int        dwChar     = Marshal.SizeOf(wideChar);
            int        structSize = dwSize + ((sb.Length + 1) * dwChar);
            IntPtr     ptr        = Marshal.AllocHGlobal(structSize);

            df.pFiles = dwSize;
            df.fWide  = 1;
            IntPtr data = IntPtr.Zero;

            try
            {
                data = UnsafeNativeMethods.GlobalLock(ptr);
                Marshal.StructureToPtr(df, data, false);
                IntPtr strData = new IntPtr((long)data + dwSize);
                DragDropHelper.CopyStringToHGlobal(sb.ToString(), strData, structSize);
            }
            finally
            {
                if (data != IntPtr.Zero)
                {
                    UnsafeNativeMethods.GlobalUnLock(data);
                }
            }

            return(ptr);
        }
Esempio n. 4
0
        /// <summary>
        /// Returns the data packed after the DROPFILES structure.
        /// </summary>
        /// <param name="dropHandle"></param>
        /// <returns></returns>
        internal static string GetData(IntPtr dropHandle)
        {
            IntPtr data = UnsafeNativeMethods.GlobalLock(dropHandle);

            try
            {
                _DROPFILES df = (_DROPFILES)Marshal.PtrToStructure(data, typeof(_DROPFILES));
                if (df.fWide != 0)
                {
                    IntPtr pdata = new IntPtr((long)data + df.pFiles);
                    return(Marshal.PtrToStringUni(pdata));
                }
            }
            finally
            {
                if (data != null)
                {
                    UnsafeNativeMethods.GlobalUnLock(data);
                }
            }

            return(null);
        }