473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Call Win32 GetOpenFileName from C#

SS
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 ?
Nov 16 '05 #1
0 3189

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ZhangZQ | last post by:
Is it possible to dynamicaly to local and call a function in Win32 dll(not a ..net assembly dll) in C# at run time, for example, a C# program popup a dialogbox to let use input which Win32 dll to...
1
by: Alisha | last post by:
I'm trying to open the Common Dialog box to allow the user to navigate to a folder and select a file. The users are accessing the application on a single Citrix server, so the enviroment is the...
3
by: bb | last post by:
I have a windows network device driver written in c++ and a user interface im porting to c#, my problem is i dont seem to be getting notified of the event calls from the driver to the c# app im...
14
by: Rajko | last post by:
GetOpenFileName with flag OFN_ALLOWMULTISELECT will create error message if number of characters exceeds 29700 characters. I does not matter if you alocated enough space. It will use only first...
28
by: Peter Olcott | last post by:
I want to double check my understanding about how the .NET framework works. From what I understand every call to the .NET framework is ultimately translated into one of more API calls, is this...
0
by: riva91 | last post by:
I am migrating an application from excel to access. In excel I used the build in GetOpenFilename function where you can give the parameter Multiselect:=True. In Access I use the GetOpenFilname...
1
by: baskar2007 | last post by:
Hi I want to select the directory path using the win32::gui. Now i am using Win32::GUI::OpenFileName method like this sub LoadFile { $FilesPath = Win32::GUI::GetOpenFileName( -title =>...
2
by: qhimq | last post by:
Hi, I was wondering if anyone encountered the following error with GetOpenFileName(). The open dialog opens, and I hover my mouse over a file. The dialog shows the detailed info of the file...
0
SammyB
by: SammyB | last post by:
I am trying to get back to the way Office 97 allowed multiselect in GetOpenFileName: the order you selected was the order returned. This means that I must keep my own list of files which changes...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.