private IntPtr PackageSelectionData(StringBuilder sb, bool addEndFormatDelimiter)
{
if (sb == null || sb.ToString().Length == 0 || 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();
var df = new _DROPFILES();
var dwSize = Marshal.SizeOf(df);
short wideChar = 0;
var dwChar = Marshal.SizeOf(wideChar);
var structSize = dwSize + (sb.Length + 1)*dwChar;
var ptr = Marshal.AllocHGlobal(structSize);
df.pFiles = dwSize;
df.fWide = 1;
var data = IntPtr.Zero;
try
{
data = UnsafeNativeMethods.GlobalLock(ptr);
Marshal.StructureToPtr(df, data, false);
var strData = new IntPtr((long) data + dwSize);
DragDropHelper.CopyStringToHGlobal(sb.ToString(), strData, structSize);
}
finally
{
if (data != IntPtr.Zero)
UnsafeNativeMethods.GlobalUnLock(data);
}
return ptr;
}