dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.36k stars 967 forks source link

SaveFileDialog cannot be opened #7975

Closed John-Qiao closed 1 year ago

John-Qiao commented 1 year ago

.NET version

8.0.100-alpha.1.22514.14 + PR: 7933 private dlls

Did it work in .NET Framework?

No

Did it work in any of the earlier releases of .NET Core or .NET 5+?

Yes, this is a regression issue, it occurs in latest main branch 8.0 sdk + PR: 7933 private dlls.

Issue description

The save file dialog cannot be opened successfully.

.NET 8.0 + PR: 7933 private dlls. Actual

Details log:

System.Runtime.InteropServices.MarshalDirectiveException
  HResult=0x80131535
  Message=Method's type signature is not PInvoke compatible.
  Source=System.Windows.Forms.Primitives
  StackTrace:
   at Windows.Win32.PInvoke.GetSaveFileName(OPENFILENAME* param0)
   at System.Windows.Forms.SaveFileDialog.RunFileDialog(OPENFILENAME* ofn)
   at System.Windows.Forms.FileDialog.RunDialogOld(HWND owner)
   at System.Windows.Forms.FileDialog.RunDialog(IntPtr hWndOwner)
   at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)
   at System.Windows.Forms.CommonDialog.ShowDialog()
   at TestApp.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\v-peqiao\Desktop\TestApp\TestApp\Form1.cs:line 13
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.ButtonBase.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(HWND hWnd, WM msg, WPARAM wparam, LPARAM lparam)

.NET 8.0 Expected

Steps to reproduce

  1. Open the TestApp.zip project in VS.
  2. Build and run it.
  3. Click the 'Open' button in form and observe the result.
RussKie commented 1 year ago

@lonitra @JeremyKuhne please investigate.

elachlan commented 1 year ago

@John-Qiao is this on windows-x64?

elachlan commented 1 year ago

@lonitra @JeremyKuhne I think the struct is missing [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] from the old pinvoke definition. https://github.com/dotnet/winforms/blob/62edc1ab7b601bee58199fdea2580a54cf5cd25b/src/System.Windows.Forms.Primitives/src/Windows/Win32/UI/Controls/Dialogs/OPENFILENAME.cs#L7-L8

JeremyKuhne commented 1 year ago

That shouldn't matter as those are the defaults. I'm about to take a look.

JeremyKuhne commented 1 year ago

Uh, never mind. ANSI is the default.

John-Qiao commented 1 year ago

@elachlan yes, it is on windows-x64.

John-Qiao commented 1 year ago

Verified with .NET SDK 8.0.100-alpha.1.22526.1 build from main branch, this issue is fixed that the save file dialog can be opened successfully now.