15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


CFileDialog directory change & ODBC

M. Smit -- m.smit@Greenock.NL
Monday, September 09, 1996

Environment: Visual C++ 4.0, Windows NT4.0,ODBC Driverpack 3

Hello,

At this moment I am having a problem in combination with a CFileDialog =
and ODBC.

I have opened a recordset and I show the fields in my dialog. One of the =
fields is a file-name and path. I thought it would be nice let users =
pick this file and path by using a CFileDialog, instead of letting them =
type it in.

Whenever the user changes the current directory in the CFileDialog to =
something else. The ODBC-driver reports: Invalid Cursor State, when I =
execute whatever ODBC/CRecordset command afterwards. I tried requerying =
the same recordset before writing any modifications, but this also gives =
the Invalid Cursor State message.

So when you change the current-directory, the ODBC-driver's internal =
state is somehow out of sync and produces this error. I think this =
problem is not directly related to the CFileDialog.

I looked in the documentation, the MSKB.... all with no result. Has =
anyone experienced the same problem before?


Regards,

Mario Smit
Software Engineer
Greenock BV, The Netherlands



Colin Angus Mackay -- colin.angus.mackay@dial.pipex.com
Tuesday, September 10, 1996

> Environment: Visual C++ 4.0, Windows NT4.0,ODBC Driverpack 3

> Hello,

> At this moment I am having a problem in combination with a CFileDialog 
and ODBC.

> Whenever the user changes the current directory in the CFileDialog to 
something else. The ODBC-driver reports: Invalid Cursor State, when I 
execute whatever ODBC/CRecordset command afterwards. I tried requerying the 
same recordset before writing any modifications, but this also gives the 
Invalid Cursor State message.

> So when you change the current-directory, the ODBC-driver's internal 
state is somehow out of sync and produces this error. I think this problem 
is not directly related to the CFileDialog.

> I looked in the documentation, the MSKB.... all with no result. Has 
anyone experienced the same problem before?

I have noticed some problems with the ODBC classes when CFileDialog changes 
the current directory. The problem I was having was that the previous 
CFileDialog may have been used to get a file from a CD-ROM and the system 
reported that it was unable to create the "File Buffer". My workaround was 
to store the current directory, change the CD to the TEMP dir. Then at the 
end of the process, change it back. i.e.

	// Store the Current Directory and change to the temp directory.
	char szCurrentPathBuffer[260];
	::GetCurrentDirectory(260,szCurrentPathBuffer);
	char szTempPathBuffer[260];
	::GetTempPath(260,szTempPathBuffer);
	::SetCurrentDirectory(szTempPathBuffer);

... Do some processing here ...

	// Change back to the old Current Directory
	::SetCurrentDirectory(szCurrentPathBuffer);

I get the impression that the Current Directory is changing mid-way through 
processing in your case. In which case I'd try store the Current Directory 
before opening the CFileDialog and then restore it afterwards.

The reason may be related to the problem I had, in that *.tmp files are 
created by ODBC and they are placed in the Current Directory (hence the 
problem I had when the Current Directory was a CD-ROM). I guess it cannot 
find the temp files that it created as the Current Directory changed, hence 
going out of sync.

Hope all this helps.


Colin Angus Mackay.




Roger Onslow/Newcastle/Computer Systems Australia/
Wednesday, September 11, 1996

[Mini-digest: 3 responses]

Colin Angus Mackay wrote:
>My workaround was to store the current directory,
> change the CD to the TEMP dir.
> Then at the end of the process, change it back. i.e.
>
> // Store the Current Directory and change to the temp directory.
> char szCurrentPathBuffer[260];
> ::GetCurrentDirectory(260,szCurrentPathBuffer);
> char szTempPathBuffer[260];
> ::GetTempPath(260,szTempPathBuffer);
> ::SetCurrentDirectory(szTempPathBuffer);
>
>... Do some processing here ...
>
> // Change back to the old Current Directory
> ::SetCurrentDirectory(szCurrentPathBuffer);

No need for all of this:

Use the m_ofn.lpstrInitialDir member to set the starting dir (rather than CD)
Add the OFN_NOCHANGEDIR bit to the m_ofn.Flags so that the CFieDialog will not 
change working directory when after the DoModal.

eg:
  CFileDialog dlg(TRUE,".bmp",fullname,0,"Bitmap Pictures (*.bmp)|*.bmp|All 
Files(*.*)|*.*||",this);
  dlg.m_ofn.lpstrInitialDir = initdir;
  dlg.m_ofn.lpstrTitle = "Browse for Picture";
  dlg.m_ofn.Flags |=
   OFN_FILEMUSTEXIST |
   OFN_PATHMUSTEXIST |
   OFN_NOCHANGEDIR |
   OFN_READONLY;
  if (dlg.DoModal() != IDOK) return;

You may like to change your code to use this method which lets windows take 
care of things for you.

Hope all this helps.


-----From: Roger Onslow/Newcastle/Computer Systems Australia/AU

Before calling your dialog, add OFN_NOCHANGEDIR bits to the flags as follows 
(from my code):

  CFileDialog dlg(TRUE,".bmp",fullname,0,"Bitmap Pictures (*.bmp)|*.bmp|All 
Files(*.*)|*.*||",this);
  dlg.m_ofn.lpstrInitialDir = initdir; // sets initial directory
  dlg.m_ofn.lpstrTitle = "Browse for Picture"; // sets title of the open dialog
  dlg.m_ofn.Flags |=
   OFN_FILEMUSTEXIST | // only allow existing files
   OFN_PATHMUSTEXIST | // only allow existing directories
>>>>>>   OFN_NOCHANGEDIR |  // !!!! DON'T CHANGE WORKING DIR
   OFN_READONLY;  // don't try to open for write
  if (dlg.DoModal() != IDOK) return;
-----From: "Greg Tighe" 

The easiest way to change back to your original directory after 
displaying a CFileDialog is to add in the OFN_NOCHANGEDIR flag when 
creating the CFileDialog, i.e.

CFileDialog    dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY |
    OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR);


	-Greg Tighe
	Applied Intelligent Systems, Inc.
	Ann Arbor, MI
	gdt@aisinc.com




| Вернуться в корень Архива |