CopyStringToHGlobal() private method

private CopyStringToHGlobal ( string s, IntPtr data, int bufferSize ) : void
s string
data System.IntPtr
bufferSize int
return void
Ejemplo n.º 1
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);
        }