Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: SelectDirectory - Exception error = Invalid file name -%s


This question is answered.


Permlink Replies: 2 - Last Post: Mar 22, 2018 7:57 AM Last Post By: Garrett B
Garrett B

Posts: 77
Registered: 10/2/16
SelectDirectory - Exception error = Invalid file name -%s  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 21, 2018 1:07 PM
Hi

I am using Embarcadero C++ Builder XE.

I am using the SelectDirectory () function, to allow the user to specify a directory.
I am running into an exception error "Invalid file name - %s"

Here's the code that runs when a button is clicked:

   UnicodeString sDir;
   CHAR szFilename[512];
 
   if ( DirectoryExists(sLog, true) == false)
   {
      bResult = SHGetFolderPathA(NULL, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, szFilename);
      sDir = szFilename;
      sLog = sDir;
   } else {
      sDir = sLog;
   }
   if (SelectDirectory(sDir, TSelectDirOpts() << sdAllowCreate << sdPerformCreate << sdPrompt, 0))
   {
      sLog = sDir;
      ...
   }


Note: sLog is defined as an AnsiString, so perhaps this problem relates to UnicodeString / AnsiString manipulation.

Here are the results of clicking the button / selecting a directory 4 times. In only the last case do I get an exception error.

Click 1:
sLog == “C:\Users\me\Desktop”, so
sDir = u“C:\\Users\\me\\Desktop”

SelectDirectory() launches pop-up window, and user selects new directory.
In this case, the user selects a USB drive, so
sDir = u“F:\\”
sLog = “F:\”


Click 2:

sLog == “F:\”, so 
sDir = u“F:\\”

SelectDirectory() launches pop-up window, and user selects new directory.
In this case, the user selects the desktop, so
sDir = u“C:\\Users\\me\\Desktop”
sLog = “C:\Users\me\Desktop”


Click 3 (same as 1st click):

sLog == “C:\Users\me\Desktop”, so
sDir = u“C:\\Users\\me\\Desktop”

SelectDirectory() launches pop-up window, and user selects new directory.
In this case, the user selects a USB drive, so
sDir = u“F:\\”
sLog = “F:\”


Click 4 (same as 3rd click, except removing USB):

Remove USB drive, so drive F no longer defined
sLog == “F:\”, so 
sDir = u“F:\\”

SelectDirectory() does not launch.
Instead, I get the following exception error:
"Invalid file name - %s"

Any help / insights appreciated

Thanks

Garrett

Edited by: Garrett B on Mar 21, 2018 1:08 PM

Edited by: Garrett B on Mar 21, 2018 1:09 PM

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: SelectDirectory - Exception error = Invalid file name -%s [Edit]
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 21, 2018 2:57 PM   in response to: Garrett B in response to: Garrett B
Garrett B wrote:

I am using the SelectDirectory () function, to allow the user to
specify a directory.

You are using the long-deprecated version of SelectDirectory() that
displays a custom VCL TForm in Windows 3.1 style. You should be using
one of the newer Shell versions that displays a native Windows dialog
instead, eg:

if (SelectDirectory(_D(""), WideString(), sDir, TSelectDirExtOpts() <<
sdNewUI << sdNewFolder))
{
    ...
}


Or:

TArray<String> Dirs;
if (SelectDirectory(sDir, Dirs))
{
    szDir = Dirs[0];
    ...
}


if ( DirectoryExists(sLog, true) == false)
{
bResult = SHGetFolderPathA(NULL, CSIDL_DESKTOPDIRECTORY, NULL,
SHGFP_TYPE_CURRENT, szFilename);
sDir = szFilename;
sLog = sDir;
}

Why are you using SHGetFolderPathA() instead of SHGetFolderPathW()?
You are assigning the path to a UnicodeString, so you should retrieve
the path as Unicode to begin with.

Note: sLog is defined as an AnsiString

Why? You really shouldn't be using AnsiString in modern UI code.

so perhaps this problem relates to UnicodeString / AnsiString
manipulation.

No.

Click 4 (same as 3rd click, except removing USB):

Well, that is a big important factor. The problem is not with the
dialog itself, it is the fact that the selected directory doesn't
physically exist anymore. The old Win3.1 dialog doesn't handle that
condition. The native Shell dialogs do.

--
Remy Lebeau (TeamB)
Garrett B

Posts: 77
Registered: 10/2/16
Re: SelectDirectory - Exception error = Invalid file name -%s [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 22, 2018 7:57 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Garrett B wrote:

I am using the SelectDirectory () function, to allow the user to
specify a directory.

You are using the long-deprecated version of SelectDirectory() that
displays a custom VCL TForm in Windows 3.1 style. You should be using
one of the newer Shell versions that displays a native Windows dialog
instead, eg:

if (SelectDirectory(_D(""), WideString(), sDir, TSelectDirExtOpts() <<
sdNewUI << sdNewFolder))
{
    ...
}

Thank you, I tried the one above and it worked well. I do not love the interface, but it gets over the problem.

if ( DirectoryExists(sLog, true) == false)
{
bResult = SHGetFolderPathA(NULL, CSIDL_DESKTOPDIRECTORY, NULL,
SHGFP_TYPE_CURRENT, szFilename);
sDir = szFilename;
sLog = sDir;
}

Why are you using SHGetFolderPathA() instead of SHGetFolderPathW()?
You are assigning the path to a UnicodeString, so you should retrieve
the path as Unicode to begin with.

Indeed, point taken :)


Note: sLog is defined as an AnsiString

Why? You really shouldn't be using AnsiString in modern UI code.

Again, thanks

Click 4 (same as 3rd click, except removing USB):

Well, that is a big important factor. The problem is not with the
dialog itself, it is the fact that the selected directory doesn't
physically exist anymore. The old Win3.1 dialog doesn't handle that
condition. The native Shell dialogs do.

I actually didn't iterate thru' the problem correctly. On that last call to SelectDirectory(), sDir has the address of the user's desktop, as before.
sDir = u“C:\\Users\\me\\Desktop”

That was the odd thing about it.
Anyway, I am not going to dwell on it now.

Thanks Remy

Garrett
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02