Solved

openfiledialog vc++ .net

Posted on 2009-05-15
11
672 Views
Last Modified: 2013-12-14
when i build a windows form application as debug, the form works just fine and when i click on the open fiolder button, it shows the openFiledialog when i run the program from wihtin VS 2008 or if i run its .exe. If i build the model  in release mode and run it from within visual studio, the openfiledialog shows up but if i run the .exe under release folder, the openfiledialog doesnt show up. Any suggestions?
0
Comment
Question by:funcoding
  • 5
  • 4
  • 2
11 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24401641
>>>> but if i run the .exe under release folder, the openfiledialog doesnt show up.

Probably it is the wrong working directory. When running from Visual Studio the working directory is the project folder.

You can test that by opening a command window at Start - Run  by typing

   cmd

then change - eventually - drive by

  d:

and directory by

  cd "\path_to_project_folder"

Here type

  Release\your_app

If that works you need to check your code whether you were using relative paths some where and exit if file open error. You either change then the relative path to absolute path or you change the working directory of your  app by calling

       SetCurrentDirectory(full_path_to_project_folder);
0
 
LVL 1

Author Comment

by:funcoding
ID: 24403069
hmm.. yea, that is not what my problem is. When this weird problem started happening, I was making sure that i was looking at the right exe file and i kept the folder opened to make sure that VS was not causing some problem and was creating a new file when i did a clean solution and then rebuilt it. So thats not it.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 24404454
A have to agree with itsme... The only functional difference between running the Release build executable in the IDE and running it by itself is the default environment at the time of execution.  In this case, the most likely confounding element is that the current working directory is different.  Other (less likely) possibilities could relate the the availability of (and version) of any DLLs used by that executable.
Show the code that gets executed when you click the "open folder" button.
0
 
LVL 1

Author Comment

by:funcoding
ID: 24410908
Please see the code below. the debug version works both from within VS and if i run the executable under debug. The problem as i said is for the release build. It only works from within VS and not if i run the exe under Release folder.
Are there any project settings that i may need to look at?
//

//Read File

//

TCHAR szFile[MAX_PATH]=TEXT("\0");

char FilePath[256];

HWND hwndOwner;

OPENFILENAME ofn={0};
 

ofn.lpstrInitialDir=TEXT( "\\");

ofn.lStructSize =sizeof(ofn);

ofn.hwndOwner = hwndOwner;

ofn.lpstrFile = szFile;

ofn.nMaxFile = sizeof(szFile)/sizeof(szFile[0]);

ofn.lpstrFilter =  TEXT("Binary Files (*.bin)\0*.bin\0");

ofn.lpstrTitle = TEXT("Open PLD File");

				 if(GetOpenFileName(&ofn)==NULL){

					 delete Switch_State;

					 delete tx;

					 delete rx;

					 return;

				 }

				 

ifstream ReadText(szFile,ios::in|ios::binary);

ReadText.read((char *) &ButtonState,sizeof ButtonState);

Open in new window

0
 
LVL 49

Expert Comment

by:DanRollins
ID: 24416596
The GetOpenFileName is a bit peculiar in that the system keeps track of something on a per-program basis -- it remembers the default directory to display.  It's one of the quirks of the OS.    That might explain what's going on.
What you need to do is diagnose the problem.  Since it won't fail in the debugger, you need to display the information at runtime.   Just add this at line 22 of the example:
     ::MessageBox(NULL, szFile,"Selected item was...", MB_OK);
Now run the program both ways and report what you see.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:funcoding
ID: 24476992
Dan,
Thanks for suggestion. I wasn't thinking of debugging it like that. It certainly helps to see where the code is getting stuck at. Let me tell the results before i tell what my observation is.
running from witih VS:
 clicking on save button brings up the dialog box where i can select the file to open and right afer that dialog box pops up that shows the file i selected with its full file path.

outside of VS, running the .exe:
 cicking on the save dialog does not bring up anything. neither the dialog box,nor the message box.

I added the meesagebox line in the if condition:
if(GetOpenFileName(&ofn)==NULL){
                                         delete Switch_State;
                                         delete tx;
                                         delete rx;
                                         return;
}
and the message box showed up which indicated to me that it didn't pick up the file path, but whats interesting is that it didnt even bring up the dialog box to allow me to select the file. I *think* the problem maybe the function GetOpenFilaName().. suggestions? Actual code is below:

TCHAR szFile1[12]=TEXT("openclicked");

				 TCHAR output1[21]=TEXT("Selected Item was...");

				 ::MessageBox(NULL,szFile1,output1,MB_OK);
 

				 TCHAR szFile[MAX_PATH]=TEXT("\0");

				 HWND hwndOwner;

				 OPENFILENAME sfn={0};

				 

				 sfn.lStructSize =sizeof(sfn);

				 sfn.hwndOwner = hwndOwner;

				 sfn.lpstrFile = szFile;

				 sfn.nMaxFile = sizeof(szFile)/sizeof(szFile[0]);

				 sfn.lpstrFilter = TEXT("bin Files (*.bin)\0*.bin\0");

				 sfn.lpstrTitle = TEXT("Save PLD File As");
 

				 if(GetSaveFileName(&sfn)==NULL){

					 TCHAR szFile1[12]=TEXT("null");

					 TCHAR output1[21]=TEXT("Selected Item was...");

					 ::MessageBox(NULL,szFile1,output1,MB_OK);

					 return;

				 }

Open in new window

0
 
LVL 49

Accepted Solution

by:
DanRollins earned 250 total points
ID: 24478444
If the dialog box does not even appear then there was an error.  As described here:
    GetOpenFileName Function
    http://msdn.microsoft.com/en-us/library/ms646927.aspx
...you should then call...
    CommDlgExtendedError Function
    http://msdn.microsoft.com/en-us/library/ms646916(VS.85).aspx
...to obtain the error code.  That will help to diagnose the problem.  My guess is that you have not fully initialized the OPENFILENAME structure.  A common mistrake (and the fix) are described here:
   http://www.experts-exchange.com/Programming/Languages/CPP/Q_20550300.html
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24480963
>>>> sfn.lpstrFilter = TEXT("bin Files (*.bin)\0*.bin\0");
Better do

  sfn.lpstrFilter = TEXT("bin Files (*.bin)\0*.bin\0\0 ");  // note the trailing space char

The filter must be terminated by a double zero and to avoid being cut you need the extra space.
0
 
LVL 1

Author Comment

by:funcoding
ID: 24482886
the links were helpful. The problem was indeed in my initialization. I had:
HWND hwndOwner;
initializing it as:
HWND hwndOwner=NULL;
fixes the problem. In the first case, the dialog box doesn't show up and in second, it does. Thanks much!!

Last question i had on this topic was making this dialog modal. I can click on open or save as many times as i want and that will open as many dialog windows. I can't seem to figure out how to make this dialog modal. Usually, we call these dialog boxes by first declaring them as a dialog, but here we had OPENFILENAME and we called the dialog box from GetOpenFileName(). suggestions?
0
 
LVL 49

Assisted Solution

by:DanRollins
DanRollins earned 250 total points
ID: 24487740
Make sure that ofn.hwndOwner is set to the HWND of your application window.  If you set it to NULL, it will make the dialog a child of the desktop.
0
 
LVL 1

Author Comment

by:funcoding
ID: 24682074
HWND was the problem. It was set to NULL initially, so as was suggested, I set it to the hwnd of the appl.
THanks!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now