/// <include file='doc\OpenFileDialog.uex' path='docs/doc[@for="OpenFileDialog.RunFileDialog"]/*' /> /// <devdoc> /// Displays a file open dialog. /// </devdoc> /// <internalonly/> internal override bool RunFileDialog(NativeMethods.OPENFILENAME_I ofn) { //We have already done the demand in EnsureFileDialogPermission but it doesn't hurt to do it again Debug.WriteLineIf(IntSecurity.SecurityDemand.TraceVerbose, "FileDialogOpenFile Demanded in OpenFileDialog.RunFileDialog"); IntSecurity.FileDialogOpenFile.Demand(); bool result = UnsafeNativeMethods.GetOpenFileName(ofn); if (!result) { // Something may have gone wrong - check for error condition // int errorCode = SafeNativeMethods.CommDlgExtendedError(); switch (errorCode) { case NativeMethods.FNERR_INVALIDFILENAME: throw new InvalidOperationException(SR.GetString(SR.FileDialogInvalidFileName, FileName)); case NativeMethods.FNERR_SUBCLASSFAILURE: throw new InvalidOperationException(SR.GetString(SR.FileDialogSubLassFailure)); case NativeMethods.FNERR_BUFFERTOOSMALL: throw new InvalidOperationException(SR.GetString(SR.FileDialogBufferTooSmall)); } } return(result); }
/// <include file='doc\FileDialog.uex' path='docs/doc[@for="FileDialog.DoFileOk"]/*' /> /// <devdoc> /// Processes the CDN_FILEOK notification. /// </devdoc> private bool DoFileOk(IntPtr lpOFN) { NativeMethods.OPENFILENAME_I ofn = Marshal.PtrToStructure <NativeMethods.OPENFILENAME_I>(lpOFN); int saveOptions = options; int saveFilterIndex = filterIndex; string[] saveFileNames = fileNames; bool ok = false; try { options = options & ~NativeMethods.OFN_READONLY | ofn.Flags & NativeMethods.OFN_READONLY; filterIndex = ofn.nFilterIndex; charBuffer.PutCoTaskMem(ofn.lpstrFile); Thread.MemoryBarrier(); if ((options & NativeMethods.OFN_ALLOWMULTISELECT) == 0) { fileNames = new string[] { charBuffer.GetString() }; } else { fileNames = GetMultiselectFiles(charBuffer); } if (ProcessFileNames()) { CancelEventArgs ceevent = new CancelEventArgs(); if (NativeWindow.WndProcShouldBeDebuggable) { OnFileOk(ceevent); ok = !ceevent.Cancel; } else { try { OnFileOk(ceevent); ok = !ceevent.Cancel; } catch (Exception e) { Application.OnThreadException(e); } } } } finally { if (!ok) { Thread.MemoryBarrier(); fileNames = saveFileNames; options = saveOptions; filterIndex = saveFilterIndex; } } return(ok); }
/// <include file='doc\FileDialog.uex' path='docs/doc[@for="FileDialog.RunDialog"]/*' /> /// <devdoc> /// Implements running of a file dialog. /// </devdoc> /// <internalonly/> protected override bool RunDialog(IntPtr hWndOwner) { NativeMethods.WndProc hookProcPtr = new NativeMethods.WndProc(this.HookProc); NativeMethods.OPENFILENAME_I ofn = new NativeMethods.OPENFILENAME_I(); try { charBuffer = CharBuffer.CreateBuffer(FILEBUFSIZE); if (fileNames != null) { charBuffer.PutString(fileNames[0]); } ofn.lStructSize = Marshal.SizeOf(typeof(NativeMethods.OPENFILENAME_I)); // SECREVIEW : Assert environment so that we can determine // : the platform that we are running on. We only // : use the information to change the style of // : dialog, so it is safe. // IntSecurity.UnrestrictedEnvironment.Assert(); try { // Degrade to the older style dialog if we're not on Win2K. // We do this by setting the struct size to a different value // if (Environment.OSVersion.Platform != System.PlatformID.Win32NT || Environment.OSVersion.Version.Major < 5) { ofn.lStructSize = 0x4C; } } finally { CodeAccessPermission.RevertAssert(); } ofn.hwndOwner = hWndOwner; ofn.hInstance = Instance; ofn.lpstrFilter = MakeFilterString(filter); ofn.nFilterIndex = filterIndex; ofn.lpstrFile = charBuffer.AllocCoTaskMem(); ofn.nMaxFile = FILEBUFSIZE; ofn.lpstrInitialDir = initialDir; ofn.lpstrTitle = title; ofn.Flags = Options | (NativeMethods.OFN_EXPLORER | NativeMethods.OFN_ENABLEHOOK | NativeMethods.OFN_ENABLESIZING); ofn.lpfnHook = hookProcPtr; ofn.FlagsEx = NativeMethods.OFN_USESHELLITEM; if (defaultExt != null && AddExtension) { ofn.lpstrDefExt = defaultExt; } return(RunFileDialog(ofn)); } finally { charBuffer = null; if (ofn.lpstrFile != IntPtr.Zero) { Marshal.FreeCoTaskMem(ofn.lpstrFile); } } }
internal override bool RunFileDialog(NativeMethods.OPENFILENAME_I ofn) { System.Windows.Forms.IntSecurity.FileDialogSaveFile.Demand(); bool saveFileName = System.Windows.Forms.UnsafeNativeMethods.GetSaveFileName(ofn); if (!saveFileName && (SafeNativeMethods.CommDlgExtendedError() == 0x3002)) { throw new InvalidOperationException(System.Windows.Forms.SR.GetString("FileDialogInvalidFileName", new object[] { base.FileName })); } return(saveFileName); }
private protected override bool RunFileDialog(NativeMethods.OPENFILENAME_I ofn) { bool result = UnsafeNativeMethods.GetSaveFileName(ofn); if (!result) { // Something may have gone wrong - check for error condition switch (Comdlg32.CommDlgExtendedError()) { case Comdlg32.FNERR.INVALIDFILENAME: throw new InvalidOperationException(string.Format(SR.FileDialogInvalidFileName, FileName)); } } return(result); }
/// <include file='doc\FileDialog.uex' path='docs/doc[@for="FileDialog.DoFileOk"]/*' /> /// <devdoc> /// Processes the CDN_FILEOK notification. /// </devdoc> private bool DoFileOk(IntPtr lpOFN) { NativeMethods.OPENFILENAME_I ofn = (NativeMethods.OPENFILENAME_I)UnsafeNativeMethods.PtrToStructure(lpOFN, typeof(NativeMethods.OPENFILENAME_I)); int saveOptions = options; int saveFilterIndex = filterIndex; string[] saveFileNames = fileNames; bool ok = false; try { options = options & ~NativeMethods.OFN_READONLY | ofn.Flags & NativeMethods.OFN_READONLY; filterIndex = ofn.nFilterIndex; charBuffer.PutCoTaskMem(ofn.lpstrFile); if ((options & NativeMethods.OFN_ALLOWMULTISELECT) == 0) { fileNames = new string[] { charBuffer.GetString() } } ; else { fileNames = GetMultiselectFiles(charBuffer); } if (ProcessFileNames()) { CancelEventArgs ceevent = new CancelEventArgs(); try { OnFileOk(ceevent); ok = !ceevent.Cancel; } catch (Exception e) { Application.OnThreadException(e); } } } finally { if (!ok) { options = saveOptions; filterIndex = saveFilterIndex; fileNames = saveFileNames; } } return(ok); }
private bool RunDialogOld(IntPtr hWndOwner) { NativeMethods.WndProc hookProcPtr = new NativeMethods.WndProc(this.HookProc); NativeMethods.OPENFILENAME_I ofn = new NativeMethods.OPENFILENAME_I(); try { charBuffer = CharBuffer.CreateBuffer(FILEBUFSIZE); if (fileNames != null) { charBuffer.PutString(fileNames[0]); } ofn.lStructSize = Marshal.SizeOf(typeof(NativeMethods.OPENFILENAME_I)); // Degrade to the older style dialog if we're not on Win2K. // We do this by setting the struct size to a different value // if (Environment.OSVersion.Platform != System.PlatformID.Win32NT || Environment.OSVersion.Version.Major < 5) { ofn.lStructSize = 0x4C; } ofn.hwndOwner = hWndOwner; ofn.hInstance = Instance; ofn.lpstrFilter = MakeFilterString(filter, this.DereferenceLinks); ofn.nFilterIndex = filterIndex; ofn.lpstrFile = charBuffer.AllocCoTaskMem(); ofn.nMaxFile = FILEBUFSIZE; ofn.lpstrInitialDir = initialDir; ofn.lpstrTitle = title; ofn.Flags = Options | (NativeMethods.OFN_EXPLORER | NativeMethods.OFN_ENABLEHOOK | NativeMethods.OFN_ENABLESIZING); ofn.lpfnHook = hookProcPtr; ofn.FlagsEx = NativeMethods.OFN_USESHELLITEM; if (defaultExt != null && AddExtension) { ofn.lpstrDefExt = defaultExt; } //Security checks happen here return(RunFileDialog(ofn)); } finally { charBuffer = null; if (ofn.lpstrFile != IntPtr.Zero) { Marshal.FreeCoTaskMem(ofn.lpstrFile); } } }
/// <include file='doc\SaveFileDialog.uex' path='docs/doc[@for="SaveFileDialog.RunFileDialog"]/*' /> /// <devdoc> /// </devdoc> /// <internalonly/> internal override bool RunFileDialog(NativeMethods.OPENFILENAME_I ofn) { bool result = UnsafeNativeMethods.GetSaveFileName(ofn); if (!result) { // Something may have gone wrong - check for error condition // int errorCode = SafeNativeMethods.CommDlgExtendedError(); switch (errorCode) { case NativeMethods.FNERR_INVALIDFILENAME: throw new InvalidOperationException(string.Format(SR.FileDialogInvalidFileName, FileName)); } } return(result); }
/// <summary> /// Displays a file open dialog. /// </summary> private protected override bool RunFileDialog(NativeMethods.OPENFILENAME_I ofn) { bool result = UnsafeNativeMethods.GetOpenFileName(ofn); if (!result) { // Something may have gone wrong - check for error condition switch (Comdlg32.CommDlgExtendedError()) { case Comdlg32.FNERR.INVALIDFILENAME: throw new InvalidOperationException(string.Format(SR.FileDialogInvalidFileName, FileName)); case Comdlg32.FNERR.SUBCLASSFAILURE: throw new InvalidOperationException(SR.FileDialogSubLassFailure); case Comdlg32.FNERR.BUFFERTOOSMALL: throw new InvalidOperationException(SR.FileDialogBufferTooSmall); } } return(result); }
internal override bool RunFileDialog(NativeMethods.OPENFILENAME_I ofn) { System.Windows.Forms.IntSecurity.FileDialogOpenFile.Demand(); bool openFileName = System.Windows.Forms.UnsafeNativeMethods.GetOpenFileName(ofn); if (!openFileName) { switch (SafeNativeMethods.CommDlgExtendedError()) { case 0x3001: throw new InvalidOperationException(System.Windows.Forms.SR.GetString("FileDialogSubLassFailure")); case 0x3002: throw new InvalidOperationException(System.Windows.Forms.SR.GetString("FileDialogInvalidFileName", new object[] { base.FileName })); case 0x3003: throw new InvalidOperationException(System.Windows.Forms.SR.GetString("FileDialogBufferTooSmall")); } } return(openFileName); }
/// <include file='doc\OpenFileDialog.uex' path='docs/doc[@for="OpenFileDialog.RunFileDialog"]/*' /> /// <devdoc> /// Displays a file open dialog. /// </devdoc> /// <internalonly/> internal override bool RunFileDialog(NativeMethods.OPENFILENAME_I ofn) { bool result = UnsafeNativeMethods.GetOpenFileName(ofn); if (!result) { // Something may have gone wrong - check for error condition // int errorCode = SafeNativeMethods.CommDlgExtendedError(); switch (errorCode) { case NativeMethods.FNERR_INVALIDFILENAME: throw new InvalidOperationException(SR.GetString(SR.FileDialogInvalidFileName, FileName)); case NativeMethods.FNERR_SUBCLASSFAILURE: throw new InvalidOperationException(SR.GetString(SR.FileDialogSubLassFailure)); case NativeMethods.FNERR_BUFFERTOOSMALL: throw new InvalidOperationException(SR.GetString(SR.FileDialogBufferTooSmall)); } } return(result); }
public static extern bool GetSaveFileName([In, Out] NativeMethods.OPENFILENAME_I ofn);
/// <include file='doc\FileDialog.uex' path='docs/doc[@for="FileDialog.RunFileDialog"]/*' /> /// <devdoc> /// Implements the actual call to GetOPENFILENAME_I or GetSaveFileName. /// </devdoc> /// <internalonly/> internal abstract bool RunFileDialog(NativeMethods.OPENFILENAME_I ofn);
protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam) { if (msg == NativeMethods.WM_NOTIFY) { dialogHWnd = UnsafeNativeMethods.GetParent(new HandleRef(null, hWnd)); try { UnsafeNativeMethods.OFNOTIFY notify = (UnsafeNativeMethods.OFNOTIFY)UnsafeNativeMethods.PtrToStructure(lparam, typeof(UnsafeNativeMethods.OFNOTIFY)); switch (notify.hdr_code) { case -601: /* CDN_INITDONE */ MoveToScreenCenter(dialogHWnd); break; case -602: /* CDN_SELCHANGE */ NativeMethods.OPENFILENAME_I ofn = (NativeMethods.OPENFILENAME_I)UnsafeNativeMethods.PtrToStructure(notify.lpOFN, typeof(NativeMethods.OPENFILENAME_I)); // Get the buffer size required to store the selected file names. int sizeNeeded = (int)UnsafeNativeMethods.SendMessage(new HandleRef(this, dialogHWnd), 1124 /*CDM_GETSPEC*/, System.IntPtr.Zero, System.IntPtr.Zero); if (sizeNeeded > ofn.nMaxFile) { // A bigger buffer is required. try { int newBufferSize = sizeNeeded + (FILEBUFSIZE / 4); // Allocate new buffer CharBuffer charBufferTmp = CharBuffer.CreateBuffer(newBufferSize); IntPtr newBuffer = charBufferTmp.AllocCoTaskMem(); // Free old buffer Marshal.FreeCoTaskMem(ofn.lpstrFile); // Substitute buffer ofn.lpstrFile = newBuffer; ofn.nMaxFile = newBufferSize; this.charBuffer = charBufferTmp; Marshal.StructureToPtr(ofn, notify.lpOFN, true); Marshal.StructureToPtr(notify, lparam, true); } catch { // intentionaly not throwing here. } } this.ignoreSecondFileOkNotification = false; break; case -604: /* CDN_SHAREVIOLATION */ // See VS Whidbey 95342. When the selected file is locked for writing, // we get this notification followed by *two* CDN_FILEOK notifications. this.ignoreSecondFileOkNotification = true; // We want to ignore the second CDN_FILEOK this.okNotificationCount = 0; // to avoid a second prompt by PromptFileOverwrite. break; case -606: /* CDN_FILEOK */ if (this.ignoreSecondFileOkNotification) { // We got a CDN_SHAREVIOLATION notification and want to ignore the second CDN_FILEOK notification if (this.okNotificationCount == 0) { this.okNotificationCount = 1; // This one is the first and is all right. } else { // This is the second CDN_FILEOK, so we want to ignore it. this.ignoreSecondFileOkNotification = false; UnsafeNativeMethods.SetWindowLong(new HandleRef(null, hWnd), 0, new HandleRef(null, NativeMethods.InvalidIntPtr)); return(NativeMethods.InvalidIntPtr); } } if (!DoFileOk(notify.lpOFN)) { UnsafeNativeMethods.SetWindowLong(new HandleRef(null, hWnd), 0, new HandleRef(null, NativeMethods.InvalidIntPtr)); return(NativeMethods.InvalidIntPtr); } break; } } catch { if (dialogHWnd != IntPtr.Zero) { UnsafeNativeMethods.EndDialog(new HandleRef(this, dialogHWnd), IntPtr.Zero); } throw; } } return(IntPtr.Zero); }
/// <include file='doc\FileDialog.uex' path='docs/doc[@for="FileDialog.DoFileOk"]/*' /> /// <devdoc> /// Processes the CDN_FILEOK notification. /// </devdoc> private bool DoFileOk(IntPtr lpOFN) { NativeMethods.OPENFILENAME_I ofn = (NativeMethods.OPENFILENAME_I)UnsafeNativeMethods.PtrToStructure(lpOFN, typeof(NativeMethods.OPENFILENAME_I)); int saveOptions = options; int saveFilterIndex = filterIndex; string[] saveFileNames = fileNames; bool saveSecurityCheckFileNames = securityCheckFileNames; bool ok = false; try { options = options & ~NativeMethods.OFN_READONLY | ofn.Flags & NativeMethods.OFN_READONLY; filterIndex = ofn.nFilterIndex; charBuffer.PutCoTaskMem(ofn.lpstrFile); // We are filling in the file names list with secure // data. Any access to this list now will require // a security demand. We set this bit before actually // setting the names; otherwise a thread ---- could // expose them. securityCheckFileNames = true; Thread.MemoryBarrier(); if ((options & NativeMethods.OFN_ALLOWMULTISELECT) == 0) { fileNames = new string[] { charBuffer.GetString() }; } else { fileNames = GetMultiselectFiles(charBuffer); } if (ProcessFileNames()) { CancelEventArgs ceevent = new CancelEventArgs(); if (NativeWindow.WndProcShouldBeDebuggable) { OnFileOk(ceevent); ok = !ceevent.Cancel; } else { try { OnFileOk(ceevent); ok = !ceevent.Cancel; } catch (Exception e) { Application.OnThreadException(e); } } } } finally { if (!ok) { securityCheckFileNames = saveSecurityCheckFileNames; Thread.MemoryBarrier(); fileNames = saveFileNames; options = saveOptions; filterIndex = saveFilterIndex; } } return(ok); }
private bool RunDialogOld(IntPtr hWndOwner) { NativeMethods.WndProc hookProcPtr = new NativeMethods.WndProc(this.HookProc); NativeMethods.OPENFILENAME_I ofn = new NativeMethods.OPENFILENAME_I(); try { charBuffer = CharBuffer.CreateBuffer(FILEBUFSIZE); if (fileNames != null) { charBuffer.PutString(fileNames[0]); } ofn.lStructSize = Marshal.SizeOf(typeof(NativeMethods.OPENFILENAME_I)); // Degrade to the older style dialog if we're not on Win2K. // We do this by setting the struct size to a different value // if (Environment.OSVersion.Platform != System.PlatformID.Win32NT || Environment.OSVersion.Version.Major < 5) { ofn.lStructSize = 0x4C; } ofn.hwndOwner = hWndOwner; ofn.hInstance = Instance; ofn.lpstrFilter = MakeFilterString(filter, this.DereferenceLinks); ofn.nFilterIndex = filterIndex; ofn.lpstrFile = charBuffer.AllocCoTaskMem(); ofn.nMaxFile = FILEBUFSIZE; ofn.lpstrInitialDir = initialDir; ofn.lpstrTitle = title; ofn.Flags = Options | (NativeMethods.OFN_EXPLORER | NativeMethods.OFN_ENABLEHOOK | NativeMethods.OFN_ENABLESIZING); ofn.lpfnHook = hookProcPtr; ofn.FlagsEx = NativeMethods.OFN_USESHELLITEM; if (defaultExt != null && AddExtension) { ofn.lpstrDefExt = defaultExt; } //Security checks happen here return RunFileDialog(ofn); } finally { charBuffer = null; if (ofn.lpstrFile != IntPtr.Zero) { Marshal.FreeCoTaskMem(ofn.lpstrFile); } } }