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); }
// Split the series of null terminated strings back up into an array of strings. static string[] UtilGetFilesFromPROJITEMDrop(IntPtr hDropInfo) { string[] result = null; IntPtr data = Microsoft.VisualStudio.Package.DataObject.GlobalLock(hDropInfo); try { _DROPFILES df = (_DROPFILES)Marshal.PtrToStructure(data, typeof(_DROPFILES)); if (df.fWide != 0) {// unicode? IntPtr pdata = new IntPtr((long)data + df.pFiles); string s = Marshal.PtrToStringUni(pdata); ArrayList list = new ArrayList(); int pos = 0; int i = 0; int len = s.Length; for (; i < len; i++) { if (s[i] == '\0') { if (i == len - 1 || s[i + 1] == '\0') { break; } list.Add(s.Substring(pos, i - 1)); pos = i + 1; } } if (i > pos) { list.Add(s.Substring(pos, i)); } result = (String[])list.ToArray(typeof(string)); } } catch { } DataObject.GlobalUnLock(data); return(result); }
/// <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); }
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; }
DataObject PackageSelectionDataObject(bool cutHighlightItems){ CleanupSelectionDataObject(false, false, false); IVsUIHierarchyWindow w = this.projectMgr.GetIVsUIHierarchyWindow(VsConstants.Guid_SolutionExplorer); IVsSolution solution = this.GetService(typeof(IVsSolution)) as IVsSolution; IVsMonitorSelection ms = this.GetService(typeof(IVsMonitorSelection)) as IVsMonitorSelection; IntPtr psel; IVsMultiItemSelect itemSelect; IntPtr psc; uint vsitemid; StringBuilder sb = new StringBuilder(); ms.GetCurrentSelection(out psel, out vsitemid, out itemSelect, out psc); IVsHierarchy sel = (IVsHierarchy)Marshal.GetTypedObjectForIUnknown(psel, typeof(IVsHierarchy)); ISelectionContainer sc = (ISelectionContainer)Marshal.GetTypedObjectForIUnknown(psc, typeof(ISelectionContainer)); const uint GSI_fOmitHierPtrs = 0x00000001; if ((sel != (IVsHierarchy)this) || (vsitemid == VsConstants.VSITEMID_ROOT) || (vsitemid == VsConstants.VSITEMID_NIL)) throw new InvalidOperationException(); if ((vsitemid == VsConstants.VSITEMID_SELECTION) && (itemSelect != null)){ int singleHierarchy; uint pcItems; itemSelect.GetSelectionInfo(out pcItems, out singleHierarchy); if (singleHierarchy != 0) // "!BOOL" == "!= 0" ? throw new InvalidOperationException(); this.itemsDragged = new ArrayList(); VSITEMSELECTION[] items = new VSITEMSELECTION[pcItems]; itemSelect.GetSelectedItems(GSI_fOmitHierPtrs, pcItems, items); for (uint i = 0; i < pcItems; i++){ if (items[i].itemid == VsConstants.VSITEMID_ROOT){ this.itemsDragged.Clear();// abort break; } this.itemsDragged.Add(items[i].pHier); string projref; solution.GetProjrefOfItem((IVsHierarchy)this, items[i].itemid, out projref); if ((projref == null) || (projref.Length == 0)){ this.itemsDragged.Clear(); // abort break; } sb.Append(projref); sb.Append('\0'); // separated by nulls. } } else if (vsitemid != VsConstants.VSITEMID_ROOT){ this.itemsDragged = new ArrayList(); this.itemsDragged.Add(this.projectMgr.NodeFromItemId(vsitemid)); string projref; solution.GetProjrefOfItem((IVsHierarchy)this, vsitemid, out projref); sb.Append(projref); } if (sb.ToString() == "" || this.itemsDragged.Count == 0) return null; sb.Append('\0'); // double null at end. _DROPFILES df = new _DROPFILES(); int dwSize = Marshal.SizeOf(df); Int16 wideChar = 0; int dwChar = Marshal.SizeOf(wideChar); IntPtr ptr = Marshal.AllocHGlobal(dwSize + ((sb.Length + 1) * dwChar)); df.pFiles = dwSize; df.fWide = 1; IntPtr data = DataObject.GlobalLock(ptr); Marshal.StructureToPtr(df, data, false); IntPtr strData = new IntPtr((long)data + dwSize); DataObject.CopyStringToHGlobal(sb.ToString(), strData); DataObject.GlobalUnLock(data); DataObject dobj = new DataObject(); FORMATETC fmt = DragDropHelper.CreateFormatEtc(); dobj.SetData(fmt, ptr); if (cutHighlightItems){ bool first = true; foreach (HierarchyNode node in this.itemsDragged){ w.ExpandItem((IVsUIHierarchy)this.projectMgr, node.hierarchyId, first ? EXPANDFLAGS.EXPF_CutHighlightItem : EXPANDFLAGS.EXPF_AddCutHighlightItem); first = false; } } return dobj; }