views:

65

answers:

3

Its a little strange. Ok so I am working with OGRE game engine which has a "SceneManager" class which keeps some files streams open in background. If i use those streams just BEFORE using GetOpenFileName() those streams work fine, but if I try to use those streams AFTER GetOpenFileName() those strams are found to be closed. Can someone throw some light why GetOpenFileName() is killing my background streams?

String Submerge::showFileDialog(char* filters, bool savedialog, char* title)
// need to tweak flags for open/save
{
OPENFILENAME ofn ;
char szFile[255] ;
HWND hwnd = NULL;
//getOgre()->getAutoCreatedWindow()->getCustomAttribute("WINDOW", &hwnd);

ZeroMemory( &ofn , sizeof(ofn) );
ofn.hwndOwner = hwnd;
ofn.lStructSize = sizeof ( ofn );
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof( szFile );
ofn.lpstrFilter = filters ? filters : "All files\0*.*\0";
ofn.nFilterIndex =1;
ofn.lpstrFileTitle = NULL ;
ofn.nMaxFileTitle = 0 ;
ofn.lpstrInitialDir=NULL ;
if(title!=NULL)
 ofn.lpstrTitle=title;
//ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST ;

MeshLoadTest(); // this is where i use background file streams
bool success = false;
if(savedialog)
 success = GetSaveFileName( &ofn );
else
 success = GetOpenFileName( &ofn );
MeshLoadTest(); // this is where i use background file streams

if(!success)
 return "";
String str;
str.append(ofn.lpstrFile);
return str;
return "";
}
+1  A: 

Note that GetOpenFileName() can and will change the current directory of your whole process. This might be interfering with whatever else you have going on.

There is an option called OFN_NOCHANGEDIR, but according to the documentation, it's ineffective:

Restores the current directory to its original value if the user changed the directory while searching for files. Windows NT 4.0/2000/XP: This flag is ineffective for GetOpenFileName.

You should check the current directory before and after making this call; if it changes then this may be your problem. In that case, add code to save and restore the current directory around the call to GetOpenFileName().

Greg Hewgill
hmmm that sounds interesting, how to stop this behaviour then?
Jitin Sameer
I've updated my answer with more info.
Greg Hewgill
Perfecto, that makes sense. I am using winXP and yep the directory does change as the files i browse are always in a different directory. I am a little noob in win32 programming, so only if you can tell me how to record and later restore working directory, please :)
Jitin Sameer
Look up the `GetCurrentDirectory` and `SetCurrentDirectory` functions.
Greg Hewgill
A: 

(This is actually an answer to the other answer, where the source of the problem was identified in the change of the current directory)

To save the current directory:

#define ARRSIZE(arr) (sizeof(arr)/sizeof(*(arr)))

//...

TCHAR curDir[MAX_PATH];
DWORD ret;
ret=GetCurrentDirectory(ARRSIZE(curDir),curDir);
if(ret==0)
{
    // The function falied for some reason (see GetLastError), handle the error
}
else if(ret>ARRSIZE(curDir))
{
    // The function failed because the buffer is too small, implementation of a function that uses dynamic allocation left to the reader
}
else
{
    // Now the current path is in curDir
}

To restore the path, simply do

if(!SetCurrentDirectory(curDir))
{
    // The function failed, handle the error
}

.

TIP: use the TCHARS and the generic text mappings functions instead of the chars from the beginning of the application: this will avoid you a lot of troubles in future, when your application will need to be compatible with Unicode paths.

Matteo Italia
hey that was a lil tricky, thanks for the explanation :)
Jitin Sameer
You'll get used to it if you'll have to use a lot of Windows APIs that output strings... but it's not WinAPI's fault, but C's: the C strings can be really hideous. :-S
Matteo Italia
A: 

Thanks guys and I made another discovery, I used OFN_NOCHANGEDIR and the problem was actually solved (WinXP SP3), maybe they need to update MSDN docs once in a while :P

Jitin Sameer
Do not rely on undocumented features: they may change in any moment in the future, and your application will suddenly stop working in any future version of Windows. I'd go with the save/restore directory solution.
Matteo Italia
Okey, i'll revert to save/restore approach soon...
Jitin Sameer
Well done! :-) (random text to reach 15 characters)
Matteo Italia