GetOpenFileName() kills my background open streams :(
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 开发者_StackOverflow中文版= 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 "";
}
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()
.
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
(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.
精彩评论