Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CFileDialog: "My Computer" not shows files and dirs

279 views
Skip to first unread message

Guido Franzke

unread,
Sep 16, 2009, 8:45:09 AM9/16/09
to
Hello NG,

I see a strange behaviour of CFileDialog on some, but not all computers.
When I open a dialog in Windows XP with

static char hFilter[] = "jpg Files (*.jpg,*.jpeg)|*.jpg;*.jpeg|All Files
(*.*)|*.*||";
CFileDialog fdlg(TRUE, "", "",OFN_FILEMUSTEXIST| OFN_HIDEREADONLY,
hFilter, this);
if (fdlg.DoModal() == IDOK) ...

When I press left on "My Computer", the file list don't show files and
directories. But there are e.g drives and network drives. When I press "My
documents" the file and directory list is shown correctly.

Even when I swap the filter definitions to
static char hFilter[] = "All Files (*.*)|*.*|jpg Files
(*.jpg,*.jpeg)|*.jpg;*.jpeg||";
the "My Computer" does not show anything.

Why is it?

Thanks for help,
Guido


AliR

unread,
Sep 16, 2009, 10:17:28 AM9/16/09
to
My Computer only lists drives that are connected to your computer, it does
not contain any files or folders.

Does it display files when you select C drive for example?

AliR.

"Guido Franzke" <guid...@yahoo.de> wrote in message
news:OXfGyusN...@TK2MSFTNGP05.phx.gbl...

Guido Franzke

unread,
Sep 16, 2009, 10:33:12 AM9/16/09
to
The list field is empty. There are no drives, although I have harddisks and
net drives. When I open e.g. in notepad, I can see the same CFileDialog, but
here the My Computer drives are all shown.
Why they are not shown in my CFileDialog? I can only select my drive with
the combobox on top, when I select there, all drives and files/dirs are
shown everywhere (e.g. in C: system)

Do you know why in "My Computer" nothing is shown?

"AliR" <Al...@online.nospam> schrieb im Newsbeitrag
news:uAHrwitN...@TK2MSFTNGP05.phx.gbl...

AliR

unread,
Sep 16, 2009, 10:42:38 AM9/16/09
to
Your call to CFileDialog looks fine, I presonally wouldn't use a static
variable for the filter string, but other than that it looks fine.

AliR.


"Guido Franzke" <guid...@yahoo.de> wrote in message

news:ecjTJrtN...@TK2MSFTNGP04.phx.gbl...

Tom Serface

unread,
Sep 16, 2009, 12:30:46 PM9/16/09
to
I agree with Ali, at first glance this looks OK. Have you tried doing it
without the filter to see if that sort of fixes the problem (I.E., you see
everything OK with no filter)? Then you may want to try just assigning it
to a CString instead, or better yet, put it in your resource table and load
it from there so if you ever want to internationalize you can change the
words like "Files and All Files" without any difficulty (that's not the
issue, just a free tip).

I do this sort of thing all the time and I don't have any problems getting
it to work, but I've never done the filter this way so that why I am
suspicious. It would be easy to try doing it with no filter (or the filter
embedded in the call instead of a variable) to see if that causes any
difference.

Tom

"Guido Franzke" <guid...@yahoo.de> wrote in message
news:OXfGyusN...@TK2MSFTNGP05.phx.gbl...

Tom Serface

unread,
Sep 16, 2009, 12:32:11 PM9/16/09
to
You should get the drives and folders.

I thought of another thing. You could debug down into the CFileDialog set
up code and see what is being passed to the action open window call.

Tom

"Guido Franzke" <guid...@yahoo.de> wrote in message

news:ecjTJrtN...@TK2MSFTNGP04.phx.gbl...

Alexander Grigoriev

unread,
Sep 16, 2009, 10:25:23 PM9/16/09
to
Make sure you're not calling CoInitializeEx with argument other than
COINIT_APARTMENTTHREADED.

If COM is initialized on the current thread with COINIT_MULTITHREADED mode,
the shell functions (including the file dialog) won't work.

"Guido Franzke" <guid...@yahoo.de> wrote in message

news:OXfGyusN...@TK2MSFTNGP05.phx.gbl...

Guido Franzke

unread,
Sep 17, 2009, 5:46:49 AM9/17/09
to
Problem solved:
When I use CString for the filter, everything works fine.
CString hFilter = "jpg Files (*.jpg,*.jpeg)|*.jpg;*.jpeg|All Files
(*.*)|*.*||";

(Although the example in msdn says "static char". Hm.)

Thanks for your tips,
Guido

Tom Serface

unread,
Sep 17, 2009, 10:27:42 AM9/17/09
to
That's strange, but glad you got it to work :o)

Tom

"Guido Franzke" <guid...@yahoo.de> wrote in message

news:%23O4$yv3NKH...@TK2MSFTNGP04.phx.gbl...

AliR

unread,
Sep 17, 2009, 10:28:09 AM9/17/09
to
That's interesting. Which version of VS are you using? I tried your code
as is on 2003 and got good results.

AliR.


"Guido Franzke" <guid...@yahoo.de> wrote in message
news:%23O4$yv3NKH...@TK2MSFTNGP04.phx.gbl...

Giovanni Dicanio

unread,
Sep 17, 2009, 11:44:09 AM9/17/09
to

Tom Serface ha scritto:

> That's strange, but glad you got it to work :o)

That's really strange... the only difference that I can spot between

static char hFilter[] = "jpg Files (*.jpg,*.jpeg)|*.jpg;*.jpeg|All Files
(*.*)|*.*||";

and

CString hFilter = "jpg Files (*.jpg,*.jpeg)|*.jpg;*.jpeg|All Files
(*.*)|*.*||";

is that on Unicode builds CString implicitly converts the ANSI string to
Unicode using CP_ACP (i.e. system default code page).

BTW: to the OP: the 'h' prefix for a char string or CString instance is
very unusual (the 'h' is used for handles, e.g. hFile, hDC, hWnd...)
better not using any prefix than a confusing one, or using some
meaningufl prefix (like 'sz' for raw C strings, or 'str' for CString).

Giovanni

Tom Serface

unread,
Sep 17, 2009, 12:33:38 PM9/17/09
to
Yeah, the variable is just a name, but you're right, according to many
conventions the name is misnamed.

I wondered about the conversion too, but it appears OP is just using ANSI.
I wonder if something got fixed when rewriting the string (maybe some weird
char in the string that wasn't showing on the screen?)

Tom

"Giovanni Dicanio" <giovanniD...@REMOVEMEgmail.com> wrote in message
news:%23G7Y226...@TK2MSFTNGP02.phx.gbl...

Guido Franzke

unread,
Sep 18, 2009, 6:28:54 AM9/18/09
to
Yes that is interesting and strange.
I use Win XP SP 2, VS 2003 (V7.1.3088). Other computers have XP SP2 and up.
As I said before, some computers show My Computer, others not. With same
release (no unicode).
Can it be because of not enough memory? Although I see in task manager 2 GB
RAM, and 1.2 GB still free.

But thanks for your tips on naming variables and using resource strings
instead.
Regards, Guido


Tom Serface

unread,
Sep 18, 2009, 1:40:26 PM9/18/09
to
That would certainly be enough memory to do a file open dialog.

Tom

"Guido Franzke" <guid...@yahoo.de> wrote in message

news:OYU$%23rEOK...@TK2MSFTNGP05.phx.gbl...

Alexander Grigoriev

unread,
Sep 18, 2009, 8:15:39 PM9/18/09
to
Have you checked that your code doesn't call CoInitializeEx(MULTITHREADED)?
Do you use any third party components?

"Guido Franzke" <guid...@yahoo.de> wrote in message

news:OYU$%23rEOK...@TK2MSFTNGP05.phx.gbl...

Joseph M. Newcomer

unread,
Sep 21, 2009, 2:03:30 AM9/21/09
to
See below...

On Wed, 16 Sep 2009 14:45:09 +0200, "Guido Franzke" <guid...@yahoo.de> wrote:

>Hello NG,
>
>I see a strange behaviour of CFileDialog on some, but not all computers.
>When I open a dialog in Windows XP with
>
> static char hFilter[] = "jpg Files (*.jpg,*.jpeg)|*.jpg;*.jpeg|All Files
>(*.*)|*.*||";

****
Do not use a static variable.
Do not use char (obsolete)
Do not put text like this in a source program.

You should load this from a STRINGTABLE entry. Always think "localization". If you put
an English-language string in your program, you have made a design error.

I've not seen this fail. But I've never used a static char. Are you sure the right thing
is being done, or could it be that the second and subsequent calls fail because they are
modifying this string "in place"? I'd say avoid the static char and go with a CString.
*****


> CFileDialog fdlg(TRUE, "", "",OFN_FILEMUSTEXIST| OFN_HIDEREADONLY,
>hFilter, this);

****
_T(""), _T(""), always think Unicode!
joe

****


> if (fdlg.DoModal() == IDOK) ...
>
>When I press left on "My Computer", the file list don't show files and
>directories. But there are e.g drives and network drives. When I press "My
>documents" the file and directory list is shown correctly.
>
>Even when I swap the filter definitions to
> static char hFilter[] = "All Files (*.*)|*.*|jpg Files
>(*.jpg,*.jpeg)|*.jpg;*.jpeg||";
>the "My Computer" does not show anything.
>
>Why is it?
>
>Thanks for help,
>Guido
>

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Joseph M. Newcomer

unread,
Sep 21, 2009, 2:20:39 AM9/21/09
to
Where does it show that? I will add it to my list of erroneous MSDN examples!
joe

Joseph M. Newcomer

unread,
Sep 21, 2009, 2:22:41 AM9/21/09
to
This is what comes of people thinking they have a clue about Hungarian Notation. I have
yet to see anyone really get it right. I avoid the problem by never using it.

I believe that what happens is that the static string is initialized once. Then the
CFileDialog comes in and replaces the | with \0 (which is what the low-level API wants),
and the next time it calls the DoModal, the string is already destroyed. This is what
comes from using 'static' inappropriately.
joe

Joseph M. Newcomer

unread,
Sep 21, 2009, 2:24:50 AM9/21/09
to
I have no idea why people fasten on the "not enough memory" explanation for a lot of
failures caused by erroneous code. Task manager numbers are pretty useless anyway, since
they tell nothing at all about what is going on in your code. They cannot be trusted
except as approximate indicators of what might be happening (e.g., if your program keeps
growing, you probably have a memory leak. Pretty much anything else you try to infer from
the numbers is irrelevant because the numbers are themselves largely irrelevant)
joe

Guido Franzke

unread,
Sep 21, 2009, 5:38:27 AM9/21/09
to
http://msdn.microsoft.com/en-us/library/wh5hz49d(VS.71,loband).aspx

In remarks section:
static char BASED_CODE szFilter[] = "Chart Files (*.xlc)|*.xlc|Worksheet
Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files
(*.*)|*.*||";Guido

"Joseph M. Newcomer"

Giovanni Dicanio

unread,
Sep 21, 2009, 6:02:15 AM9/21/09
to
Guido Franzke ha scritto:

> http://msdn.microsoft.com/en-us/library/wh5hz49d(VS.71,loband).aspx
>
> In remarks section:
> static char BASED_CODE szFilter[] = "Chart Files (*.xlc)|*.xlc|Worksheet
> Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files
> (*.*)|*.*||";Guido

MSDN documentation has some errors and omissions.
Joe maintains a web page with a list of some of them:

http://www.flounder.com/msdn_documentation_errors_and_omissions.htm

Giovanni

Joseph M. Newcomer

unread,
Sep 21, 2009, 10:14:39 AM9/21/09
to
Thanks. I have checked the code, and my original guess, that the input string is modified
in-place, is incorrect. A copy of the string is made. However, there is (a) no reason to
declare it static (b) it must not be a char (c) BASED_CODE is obsolete and should have
been removed from the example (d) putting English text (or language-of-your-choice) as a
literal string in a program is very far from modern Best Practice, and it should be a
CString loaded from the STRINGTABLE. So of the example below:

1. static is questionable
2. char is wrong (not Unicode)
3. BASED_CODE is wrong (obsolete and meaningless)
4. szFilter[] is wrong (it should be a CString)
5. the string literal is wrong because a string literal with native language words should
not be used.

Is there anything RIGHT about this example?

(Yes: the semicolon that terminates the declaration!)
joe

Joseph M. Newcomer

unread,
Sep 21, 2009, 10:16:56 AM9/21/09
to
See below...

On Mon, 21 Sep 2009 02:03:30 -0400, Joseph M. Newcomer <newc...@flounder.com> wrote:

>See below...
>On Wed, 16 Sep 2009 14:45:09 +0200, "Guido Franzke" <guid...@yahoo.de> wrote:
>
>>Hello NG,
>>
>>I see a strange behaviour of CFileDialog on some, but not all computers.
>>When I open a dialog in Windows XP with
>>
>> static char hFilter[] = "jpg Files (*.jpg,*.jpeg)|*.jpg;*.jpeg|All Files
>>(*.*)|*.*||";
>****
>Do not use a static variable.
>Do not use char (obsolete)
>Do not put text like this in a source program.
>
>You should load this from a STRINGTABLE entry. Always think "localization". If you put
>an English-language string in your program, you have made a design error.
>
>I've not seen this fail. But I've never used a static char. Are you sure the right thing
>is being done, or could it be that the second and subsequent calls fail because they are
>modifying this string "in place"? I'd say avoid the static char and go with a CString.
>*****

****
I checked the code this morning and it makes a copy of the input string, so this guess is
incorrect. But nothing else about the variable is right:

static is questionable
char is wrong
hFilter[] is a lousy name (violates proper HN usage)
the use of anything other than a CString is questionable
it is not loaded from the STRINGTABLE

The example from which this is copied has NOTHING RIGHT IN IT! It is an AMAZINGLY poor
example!
joe

Guido Franzke

unread,
Sep 22, 2009, 9:07:07 AM9/22/09
to
Ok,
thank you for your detailed analysis.
Guido

"Joseph M. Newcomer" <newc...@flounder.com>

Guido Franzke

unread,
Sep 23, 2009, 10:08:52 AM9/23/09
to
Ok, after CString, I still have the problem on some computers, not on all.
I think, should be a Windows XP problem.

On the computer not showing the drives in "My Computer", I go to the
directory having jpg files. But they are not shown, even not when I use the
filter "All files *.*". But the directory before having documents, shows all
files correctly.

Now, I type a filename in my jpg-directory by hand (I cannot see the file
list, but I know the correct spelling of my file), and click "Open" in
CFileDialog, the dialog and the programm crashes!
A dump file shows me in wincore.cpp the callback funtion AfxWndProc,
stopping when
return AfxCallWndProc(pWnd, hWnd, nMsg, wParam, lParam);
Unfortunately the dump does not show me the vlues of the variables.

Why does CFileDialog crash?
Guido


Tom Serface

unread,
Sep 23, 2009, 6:29:44 PM9/23/09
to
Does the same thing happen in a debug build? If so you may be able to see
an assert, or break in the code to check the call stack to see where it is
crashing. Sounds like maybe you have a memory issue. Are you trying to
collect multiple files from the open file dialog?

Tom

"Guido Franzke" <guid...@yahoo.de> wrote in message

news:e5T7NeFP...@TK2MSFTNGP04.phx.gbl...

Joseph M. Newcomer

unread,
Sep 23, 2009, 11:04:14 PM9/23/09
to
See below...

On Wed, 23 Sep 2009 16:08:52 +0200, "Guido Franzke" <guid...@yahoo.de> wrote:

>Ok, after CString, I still have the problem on some computers, not on all.
>I think, should be a Windows XP problem.
>
>On the computer not showing the drives in "My Computer", I go to the
>directory having jpg files. But they are not shown, even not when I use the
>filter "All files *.*". But the directory before having documents, shows all
>files correctly.
>
>Now, I type a filename in my jpg-directory by hand (I cannot see the file
>list, but I know the correct spelling of my file), and click "Open" in
>CFileDialog, the dialog and the programm crashes!

****
OK, you used the nonsense phrase "crash" as if it has meaning. You did not say exactly
what the error was.

The most common cause of a failure on a return statement is either an access fault or an
invalid instruction exception caused by a stack clobber, caused by a memory overwrite.
There is a long-known bug with CFileDialog where code compiled with old libraries (say,
under VS6) erroneously assumes a size for the data structure on the stack, but when run in
Vista or XP, the data structure is actually longer and a memory overwrite occurs. Largely,
because people are migrating from VS6 and the older libraries, this has gone away, but it
could be your problem.

What was the call stack?

And can you run it under a debugger, instead of trying to work from a stack dump? And if
you say "But I don't have Visual Studio installed", I really don't care. You can install
WinDbg and copy your pdb file across to the target machine, and get real data. WinDbg
isn't nearly as nice as VS as a debugger, but it is free and easily installed.

Also, it doesn't hurt if you tell us that it occurs, say, only in the release mode and not
in debug mode. And it REALLY helps if you tell us what the service packs are on the
machines on which it fails, and which version of VS you are using. Someone might
recognize something about the bug.
joe


>A dump file shows me in wincore.cpp the callback funtion AfxWndProc,
>stopping when
>return AfxCallWndProc(pWnd, hWnd, nMsg, wParam, lParam);
>Unfortunately the dump does not show me the vlues of the variables.
>
>Why does CFileDialog crash?
>Guido
>

0 new messages