public static extern IntPtr SHBrowseForFolder([In] UnsafeNativeMethods.BROWSEINFO lpbi);
/// <summary> /// When overridden in a derived class, specifies a common dialog box. /// </summary> /// <param name="hwndOwner">A value that represents the window handle of the owner window for the common dialog box.</param> /// <returns>true if the dialog box was successfully run; otherwise, false.</returns> protected override bool RunDialog(IntPtr hwndOwner) { IntPtr root = IntPtr.Zero; bool flag = false; UnsafeNativeMethods.Shell32.SHGetSpecialFolderLocation(hwndOwner, (int)Environment.SpecialFolder.Desktop, ref root); int browseInfoFlags = NativeMethods.BIF_NEWDIALOGSTYLE; if (!_showNewFolderButton) { browseInfoFlags += NativeMethods.BIF_NONEWFOLDERBUTTON; } IntPtr pidl = IntPtr.Zero; IntPtr hglobal = IntPtr.Zero; IntPtr pszPath = IntPtr.Zero; try { hglobal = Marshal.AllocHGlobal((int)(260 * Marshal.SystemDefaultCharSize)); pszPath = Marshal.AllocHGlobal((int)(260 * Marshal.SystemDefaultCharSize)); _callback = new UnsafeNativeMethods.BrowseCallbackProc(OnBrowseCallbackProc); UnsafeNativeMethods.BROWSEINFO lpbi = new UnsafeNativeMethods.BROWSEINFO(); lpbi.pidlRoot = root; lpbi.hwndOwner = hwndOwner; lpbi.pszDisplayName = hglobal; lpbi.lpszTitle = _descriptionText; lpbi.ulFlags = browseInfoFlags; lpbi.lpfn = _callback; lpbi.lParam = IntPtr.Zero; lpbi.iImage = 0; pidl = UnsafeNativeMethods.Shell32.SHBrowseForFolder(lpbi); if (pidl != IntPtr.Zero) { UnsafeNativeMethods.Shell32.SHGetPathFromIDList(pidl, pszPath); _selectedPathNeedsCheck = true; _selectedPath = Marshal.PtrToStringAuto(pszPath); flag = true; } } finally { UnsafeNativeMethods.IMalloc sHMalloc = GetSHMalloc(); sHMalloc.Free(root); if (pidl != IntPtr.Zero) sHMalloc.Free(pidl); if (pszPath != IntPtr.Zero) Marshal.FreeHGlobal(pszPath); if (hglobal != IntPtr.Zero) Marshal.FreeHGlobal(hglobal); _callback = null; } return flag; }