void IDataObject.GetData(FORMATETC[] fmt, STGMEDIUM[] m) { STGMEDIUM retMedium = new STGMEDIUM(); if (fmt == null || fmt.Length < 1) { return; } SafeGlobalAllocHandle copy = null; foreach (DataCacheEntry e in this.entries) { if (e.Format.cfFormat == fmt[0].cfFormat /*|| fmt[0].cfFormat == InternalNativeMethods.CF_HDROP*/) { retMedium.tymed = e.Format.tymed; // Caller must delete the memory. copy = DragDropHelper.CopyHGlobal(e.Data); retMedium.unionmember = copy.DangerousGetHandle(); break; } } if (m != null && m.Length > 0) { m[0] = retMedium; if (copy != null) { copy.SetHandleAsInvalid(); } } }
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); }
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; }
/// <summary> /// The method that does the cleanup. /// </summary> /// <param name="disposing"></param> private void Dispose(bool disposing) { if (disposing) { SafeGlobalAllocHandle handle = this.data; if (handle != null) { handle.Dispose(); data = null; } } }
public static string GetSourceProjectPath(Microsoft.VisualStudio.OLE.Interop.IDataObject dataObject) { string projectPath = null; FORMATETC fmtetc = CreateFormatEtc(CF_VSPROJECTCLIPDESCRIPTOR); if (QueryGetData(dataObject, ref fmtetc) == VSConstants.S_OK) { STGMEDIUM stgmedium = DragDropHelper.GetData(dataObject, ref fmtetc); if (stgmedium.tymed == (uint)TYMED.TYMED_HGLOBAL && stgmedium.unionmember != IntPtr.Zero) { // We are releasing the cloned hglobal here. using (SafeGlobalAllocHandle dropInfoHandle = new SafeGlobalAllocHandle(stgmedium.unionmember, true)) { projectPath = GetData(dropInfoHandle); } } } return(projectPath); }
/// <summary> /// Returns the data packed after the DROPFILES structure. /// </summary> /// <param name="dropHandle"></param> /// <returns></returns> public static string GetData(SafeGlobalAllocHandle dropHandle) { IntPtr data = UnsafeNativeMethods.GlobalLock(dropHandle); try { _DROPFILES df = (_DROPFILES)Marshal.PtrToStructure(data, typeof(_DROPFILES)); if (df.fWide) { IntPtr pdata = new IntPtr((long)data + df.pFiles); return(Marshal.PtrToStringUni(pdata)); } } finally { if (data != null) { UnsafeNativeMethods.GlobalUnlock(dropHandle); } } return(null); }
/// <summary> /// The IntPtr is data allocated that should be removed. It is allocated by the ProcessSelectionData method. /// </summary> public DataCacheEntry(FORMATETC fmt, SafeGlobalAllocHandle data, DATADIR dir) { this.format = fmt; this.data = data; this.dataDir = dir; }
internal static extern bool GlobalUnlock(SafeGlobalAllocHandle h);
internal static extern UIntPtr GlobalSize(SafeGlobalAllocHandle h);
internal static extern IntPtr GlobalLock(SafeGlobalAllocHandle h);
/// <summary> /// Returns the data packed after the DROPFILES structure. /// </summary> /// <param name="dropHandle"></param> /// <returns></returns> public static string GetData(SafeGlobalAllocHandle dropHandle) { IntPtr data = UnsafeNativeMethods.GlobalLock(dropHandle); try { _DROPFILES df = (_DROPFILES)Marshal.PtrToStructure(data, typeof(_DROPFILES)); if(df.fWide) { IntPtr pdata = new IntPtr((long)data + df.pFiles); return Marshal.PtrToStringUni(pdata); } } finally { if(data != null) { UnsafeNativeMethods.GlobalUnlock(dropHandle); } } return null; }
public static string GetSourceProjectPath(Microsoft.VisualStudio.OLE.Interop.IDataObject dataObject) { string projectPath = null; FORMATETC fmtetc = CreateFormatEtc(CF_VSPROJECTCLIPDESCRIPTOR); if(QueryGetData(dataObject, ref fmtetc) == VSConstants.S_OK) { STGMEDIUM stgmedium = DragDropHelper.GetData(dataObject, ref fmtetc); if(stgmedium.tymed == (uint)TYMED.TYMED_HGLOBAL && stgmedium.unionmember != IntPtr.Zero) { // We are releasing the cloned hglobal here. using (SafeGlobalAllocHandle dropInfoHandle = new SafeGlobalAllocHandle(stgmedium.unionmember, true)) { projectPath = GetData(dropInfoHandle); } } } return projectPath; }
public void SetData(FORMATETC format, SafeGlobalAllocHandle data) { this.entries.Add(new DataCacheEntry(format, data, DATADIR.DATADIR_SET)); }