public static string GetDirectoryViaBrowseDialog(this IVsUIShell2 shell, IntPtr parentWindow, Guid persistenceSlot, string title, string initialDirectory, bool overridePersistedInitialDirectory) { if (shell == null) { throw new ArgumentNullException("shell"); } if (title == null) { throw new ArgumentNullException("title"); } const int MaxDirName = 10000; IntPtr dirNameBuffer = Marshal.AllocCoTaskMem((MaxDirName + 1) * sizeof(char)); try { VSBROWSEINFOW[] browse = new VSBROWSEINFOW[] { new VSBROWSEINFOW { pwzDlgTitle = title, dwFlags = (uint)BrowseInfoFlags.ReturnOnlyFileSystemDirectories, hwndOwner = parentWindow, pwzInitialDir = GetInitialDirectoryToUse(initialDirectory, overridePersistedInitialDirectory, persistenceSlot), nMaxDirName = MaxDirName, lStructSize = (uint)Marshal.SizeOf(typeof(VSBROWSEINFOW)), pwzDirName = dirNameBuffer, dwHelpTopic = 0 } }; string helpTopic = string.Empty; string openButtonLabel = null; string ceilingDir = null; VSNSEBROWSEINFOW[] nseBrowseInfo = null; ErrorHandler.ThrowOnFailure(shell.GetDirectoryViaBrowseDlgEx(browse, helpTopic, openButtonLabel, ceilingDir, nseBrowseInfo)); string folder = Marshal.PtrToStringUni(browse[0].pwzDirName); if (!string.IsNullOrEmpty(folder)) { PersistLastUseDirectory(persistenceSlot, folder); } return(folder); } catch (COMException ex) when(ex.ErrorCode == VSConstants.OLE_E_PROMPTSAVECANCELLED) { } finally { if (dirNameBuffer != IntPtr.Zero) { Marshal.FreeCoTaskMem(dirNameBuffer); dirNameBuffer = IntPtr.Zero; } } return(null); }
private static string GetCommandText(IntPtr structPtr) { if (structPtr == IntPtr.Zero) { return(string.Empty); } OLECMDTEXT olecmdtext = (OLECMDTEXT)Marshal.PtrToStructure(structPtr, typeof(OLECMDTEXT)); if (olecmdtext.cwActual == 0) { return(string.Empty); } IntPtr offset = Marshal.OffsetOf(typeof(OLECMDTEXT), "rgwz"); IntPtr ptr = (IntPtr)((long)structPtr + (long)offset); return(Marshal.PtrToStringUni(ptr, (int)olecmdtext.cwActual - 1)); }
/// <summary> /// Gets content of the system clipboard. /// </summary> /// <param name="dataType">The type of the text data in the clipboard</param> /// <returns>Text content retrieved from the clipboard if available. Otherwise null.</returns> /// <remarks> /// <para> /// This method gets text from the system clipboard. /// If stored text data is a special format (line or rectangle,) /// its data type will be set to <paramref name="dataType"/> parameter. /// </para> /// </remarks> /// <seealso cref="Sgry.Azuki.TextDataType">TextDataType enum</seealso> public string GetClipboardText(out TextDataType dataType) { Int32 rc; // result code bool clipboardOpened = false; IntPtr dataHandle = IntPtr.Zero; IntPtr dataPtr = IntPtr.Zero; uint formatID = UInt32.MaxValue; string data = null; dataType = TextDataType.Normal; try { // open clipboard rc = WinApi.OpenClipboard(IntPtr.Zero); if (rc == 0) { return(null); } clipboardOpened = true; // distinguish type of data in the clipboard if (WinApi.IsClipboardFormatAvailable(_CF_LINEOBJECT) != 0) { formatID = WinApi.CF_UNICODETEXT; dataType = TextDataType.Line; } else if (WinApi.IsClipboardFormatAvailable(_CF_RECTSELECT) != 0) { formatID = WinApi.CF_UNICODETEXT; dataType = TextDataType.Rectangle; } else if (WinApi.IsClipboardFormatAvailable(WinApi.CF_UNICODETEXT) != 0) { formatID = WinApi.CF_UNICODETEXT; } else if (WinApi.IsClipboardFormatAvailable(WinApi.CF_TEXT) != 0) { formatID = WinApi.CF_TEXT; } if (formatID == UInt32.MaxValue) { return(null); // no text data was in clipboard } // get handle of the clipboard data dataHandle = WinApi.GetClipboardData(formatID); if (dataHandle == IntPtr.Zero) { return(null); } // get data pointer by locking the handle dataPtr = Utl.MyGlobalLock(dataHandle); if (dataPtr == IntPtr.Zero) { return(null); } // retrieve data if (formatID == WinApi.CF_TEXT) { data = Utl.MyPtrToStringAnsi(dataPtr); } else { data = Marshal.PtrToStringUni(dataPtr); } } finally { // unlock handle if (dataPtr != IntPtr.Zero) { Utl.MyGlobalUnlock(dataHandle); } if (clipboardOpened) { WinApi.CloseClipboard(); } } return(data); }