Link to home
Start Free TrialLog in
Avatar of MyQ2004
MyQ2004

asked on

Call GetOpenFileName() from C#

I tried a sample code provided in Microsoft .NET on how to make a call to
Win32 GetOpenFileName. It crashed if I call the function, select a file and
close the dialog and repeat the process several times.. I got the following
error :

An unhandled exception of type 'System.NullReferenceException' occurred in
system.windows.forms.dll

Additional information: Object reference not set to an instance of an
object.

Below is the code :

  [ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Auto )]
  public class OpenFileName
  {
   public int  structSize = 0;
   public IntPtr dlgOwner = IntPtr.Zero;
   public IntPtr instance = IntPtr.Zero;

   public String filter = null;
   public String customFilter = null;
   public int  maxCustFilter = 0;
   public int  filterIndex = 0;

   public String file = null;
   public int  maxFile = 0;

   public String fileTitle = null;
   public int  maxFileTitle = 0;

   public String initialDir = null;

   public String title = null;

   public int  flags = 0;
   public short fileOffset = 0;
   public short fileExtension = 0;

   public String defExt = null;

   public IntPtr custData = IntPtr.Zero;
   public IntPtr hook = IntPtr.Zero;

   public String templateName = null;

   public IntPtr reservedPtr = IntPtr.Zero;
   public int  reservedInt = 0;
   public int  flagsEx = 0;
  }

  public class LibWrap
  {
   [ DllImport( "Comdlg32.dll", CharSet=CharSet.Auto )]
   public static extern bool GetOpenFileNameW([ In, Out ] OpenFileName
ofn );
  }

  private void Test()
  {
   OpenFileName ofn = new OpenFileName();

   ofn.structSize = Marshal.SizeOf( ofn );
   ofn.filter = "*.xml" ;
   ofn.file = new String( new char[ 256 ]);
   ofn.maxFile = ofn.file.Length;
   ofn.fileTitle = new String( new char[ 64 ]);
   ofn.maxFileTitle = ofn.fileTitle.Length;
   ofn.initialDir = "C:\\";
   ofn.title = "Open file";
   ofn.defExt = "xml";
   ofn.flags = 0x00000100;
   ofn.file = "*.xml";
   ofn.dlgOwner = this.Handle;

   if( LibWrap.GetOpenFileNameW( ofn ))
   {
   }
  }

Any Idea ?
Avatar of TheAvenger
TheAvenger
Flag of Switzerland image

Why are you using GetOpenFileName()? Can't you use a OpenFileDialog?
Avatar of MyQ2004
MyQ2004

ASKER

OpenFileDialog is not able to return me current folder/path under the following scenario :

I set the property ValidateNames to false.  I didn't select any file when
the dialog box pop-ups.  When I click the "Open" button, the dialog box was
closed. The problem is filedlg.FileName doesn't not contain anything. I
expect it to have something like this c:\Test\*.lvl (with Win32
OpenFileDialog, it will). The protected member of OpenFileDialog namely
fileNames and FileNameInternal do have the value but as it is a protected
member, the value is inaccessible. ) With OpenFileName Win32 API, I am able
to get the value.

That's the reason why I opted for GetOpenFileName() API.
ASKER CERTIFIED SOLUTION
Avatar of TheAvenger
TheAvenger
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MyQ2004

ASKER

oopss..It works. I think I didn't get the BindingFlags right. Thank you so much.
No, the problem was in the typeof. Now it's typeof(FileDialog), not typeof(OpenFileDialog)