Home All Groups Group Topic Archive Search About

Using GetSaveFileName API with VB6

Author
26 Mar 2009 12:13 AM
MM
In the explanatory notes for GetSaveFileName it is stated:

"sDefFileExt is a pointer to a buffer that contains the default
extension. GetOpenFileName and GetSaveFileName append this extension
to the file name if the user fails to type an extension."

Observe that: "if the user fails to type an extension" proviso.

Problem:
I am using sDefFileExt with GetSaveFileName, but the extension in
sDefFileExt is being appended EVEN IF I type in a different extension.

I've got sDefFileExt = "lay" & vbNullChar & vbNullChar

(I am NOT including the leading dot in sDefFileExt, as per the notes.)

For example, if sDefFileExt = "lay" and I type in "dummy.xyz" in the
GetSaveFileName dialog then the dialog returns "dummy.xyz.lay"

What I'm trying to achieve is:

- If filename is typed in with an extension and the extension matches
sDefFileExt, e.g. xyz123.lay, then if file exists, then warn to
overwrite, else return xyz123.lay


- If NO extension typed, append sDefFileExt. If file exists, then warn
to overwrite, else return e.g. abc456.lay


- if a filename with a DIFFERENT extension is typed, e.g. fgh654.xyz,
if file exists, then warn to overwrite, else return fgh654.xyz


What is *actually* returned in the last example above is
gfh654.xyz.lay, that is, despite my typing an extension that is
different from the one given in sDefFileExt, Windows is appending
sDefFileExt to my extension!

******************************************************************************
This behaviour by Windows makes no sense and is contrary to the
explanatory notes for GetSaveFileName
******************************************************************************

Is there a way of doing what I want with the dialog flags? Or do I
have to code around it? Or am I completely missing something?

NB: I am using OFN_OVERWRITEPROMPT. This PC is running Windows 98SE
and VB6/SP6.

Thanks!

MM

Author
26 Mar 2009 1:10 AM
Karl E. Peterson
MM wrote:
> What is *actually* returned in the last example above is
> gfh654.xyz.lay, that is, despite my typing an extension that is
> different from the one given in sDefFileExt, Windows is appending
> sDefFileExt to my extension!

Yeah, that's the way Windows works.  Try opening a new document in Word, and Save as
"Doc1.xyz" -- see what you get.  :-(

> ******************************************************************************
> This behaviour by Windows makes no sense and is contrary to the
> explanatory notes for GetSaveFileName
> ******************************************************************************

Maybe it'll help to realize it was written when *every* file was an 8.3 critter, and
that dots weren't valid filename characters?

> Is there a way of doing what I want with the dialog flags? Or do I
> have to code around it? Or am I completely missing something?

I think you have to code around it.  And who's to say, really, that the user doesn't
want a file with two dots in it?
--
..NET: It's About Trust!
http://vfred.mvps.org
Are all your drivers up to date? click for free checkup

Author
26 Mar 2009 8:28 AM
MM
Show quote Hide quote
On Wed, 25 Mar 2009 18:10:03 -0700, "Karl E. Peterson" <k***@mvps.org>
wrote:

>MM wrote:
>> What is *actually* returned in the last example above is
>> gfh654.xyz.lay, that is, despite my typing an extension that is
>> different from the one given in sDefFileExt, Windows is appending
>> sDefFileExt to my extension!
>
>Yeah, that's the way Windows works.  Try opening a new document in Word, and Save as
>"Doc1.xyz" -- see what you get.  :-(
>
>> ******************************************************************************
>> This behaviour by Windows makes no sense and is contrary to the
>> explanatory notes for GetSaveFileName
>> ******************************************************************************
>
>Maybe it'll help to realize it was written when *every* file was an 8.3 critter, and
>that dots weren't valid filename characters?
>
>> Is there a way of doing what I want with the dialog flags? Or do I
>> have to code around it? Or am I completely missing something?
>
>I think you have to code around it.  And who's to say, really, that the user doesn't
>want a file with two dots in it?

Fine, but why do the docs say the def extension will be appended IF
the user didn't type one? Are the docs wrong or is this a bug?

MM
Author
26 Mar 2009 11:59 AM
Rick Raisley
Show quote Hide quote
"MM" <kylix***@yahoo.co.uk> wrote in message
news:60fms49t2sc8dko1livpvlmokmd8dodbqc@4ax.com...
> On Wed, 25 Mar 2009 18:10:03 -0700, "Karl E. Peterson" <k***@mvps.org>
> wrote:
>
>>MM wrote:
>>> What is *actually* returned in the last example above is
>>> gfh654.xyz.lay, that is, despite my typing an extension that is
>>> different from the one given in sDefFileExt, Windows is appending
>>> sDefFileExt to my extension!
>>
>>Yeah, that's the way Windows works.  Try opening a new document in Word,
>>and Save as
>>"Doc1.xyz" -- see what you get.  :-(
>>
>>> ******************************************************************************
>>> This behaviour by Windows makes no sense and is contrary to the
>>> explanatory notes for GetSaveFileName
>>> ******************************************************************************
>>
>>Maybe it'll help to realize it was written when *every* file was an 8.3
>>critter, and
>>that dots weren't valid filename characters?
>>
>>> Is there a way of doing what I want with the dialog flags? Or do I
>>> have to code around it? Or am I completely missing something?
>>
>>I think you have to code around it.  And who's to say, really, that the
>>user doesn't
>>want a file with two dots in it?
>
> Fine, but why do the docs say the def extension will be appended IF
> the user didn't type one? Are the docs wrong or is this a bug?
>

I consider this proper operation. Normally, to save any document, you select
a FILE TYPE, which usually defines an extension. Windows, then, is saying
that if you do not add the proper extension, for that file type, it will add
it for you. Any other extension can easily, and is, interpreted as part of
the file name. Like when saving an Excel XLS spreadsheet:

Save as MyFile.xls > MyFile.xls
Save as MyFile > MyFile.xls
Save as My.Document > My.Document.xls
Save as My.Doc > My.Doc.xls

I consider these all proper, and expected, operation.

If you don't use the proper extension for the file type being saved, the
program (or maybe it's Windows, don't know) adds the standard extension for
you.

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net
Author
26 Mar 2009 5:13 PM
MM
On Thu, 26 Mar 2009 07:59:30 -0400, "Rick Raisley"
<heavymetal-A-T-bellsouth-D-O-Tnet> wrote:

Show quoteHide quote
>"MM" <kylix***@yahoo.co.uk> wrote in message
>news:60fms49t2sc8dko1livpvlmokmd8dodbqc@4ax.com...
>> On Wed, 25 Mar 2009 18:10:03 -0700, "Karl E. Peterson" <k***@mvps.org>
>> wrote:
>>
>>>MM wrote:
>>>> What is *actually* returned in the last example above is
>>>> gfh654.xyz.lay, that is, despite my typing an extension that is
>>>> different from the one given in sDefFileExt, Windows is appending
>>>> sDefFileExt to my extension!
>>>
>>>Yeah, that's the way Windows works.  Try opening a new document in Word,
>>>and Save as
>>>"Doc1.xyz" -- see what you get.  :-(
>>>
>>>> ******************************************************************************
>>>> This behaviour by Windows makes no sense and is contrary to the
>>>> explanatory notes for GetSaveFileName
>>>> ******************************************************************************
>>>
>>>Maybe it'll help to realize it was written when *every* file was an 8.3
>>>critter, and
>>>that dots weren't valid filename characters?
>>>
>>>> Is there a way of doing what I want with the dialog flags? Or do I
>>>> have to code around it? Or am I completely missing something?
>>>
>>>I think you have to code around it.  And who's to say, really, that the
>>>user doesn't
>>>want a file with two dots in it?
>>
>> Fine, but why do the docs say the def extension will be appended IF
>> the user didn't type one? Are the docs wrong or is this a bug?
>>
>
>I consider this proper operation. Normally, to save any document, you select
>a FILE TYPE, which usually defines an extension. Windows, then, is saying
>that if you do not add the proper extension, for that file type, it will add
>it for you. Any other extension can easily, and is, interpreted as part of
>the file name. Like when saving an Excel XLS spreadsheet:
>
>Save as MyFile.xls > MyFile.xls
>Save as MyFile > MyFile.xls
>Save as My.Document > My.Document.xls
>Save as My.Doc > My.Doc.xls
>
>I consider these all proper, and expected, operation.
>
>If you don't use the proper extension for the file type being saved, the
>program (or maybe it's Windows, don't know) adds the standard extension for
>you.

Fine, but that's not what the docs say! From the horse's mouth (MSDN):
"GetOpenFileName and GetSaveFileName append this extension to the file
name if the user fails to type an extension."

If I type MyFile.xyz I don't expect Windows to add the def extension,
e.g. MyFile.xyz.lay, but it is. This is plain wrong. The OFN member
is, after all, called s....DEF.....FileExt!! That is, it's intended as
the DEFault for when nothing was specified.

MM
Author
26 Mar 2009 8:05 PM
Karl E. Peterson
MM wrote:
Show quoteHide quote
>>> What is *actually* returned in the last example above is
>>> gfh654.xyz.lay, that is, despite my typing an extension that is
>>> different from the one given in sDefFileExt, Windows is appending
>>> sDefFileExt to my extension!
>>
>>Yeah, that's the way Windows works.  Try opening a new document in Word, and Save
>>as "Doc1.xyz" -- see what you get.  :-(
>>
>>> ******************************************************************************
>>> This behaviour by Windows makes no sense and is contrary to the
>>> explanatory notes for GetSaveFileName
>>> ******************************************************************************
>>
>>Maybe it'll help to realize it was written when *every* file was an 8.3 critter,
>>and that dots weren't valid filename characters?
>>
>>> Is there a way of doing what I want with the dialog flags? Or do I
>>> have to code around it? Or am I completely missing something?
>>
>>I think you have to code around it.  And who's to say, really, that the user
>>doesn't want a file with two dots in it?
>
> Fine, but why do the docs say the def extension will be appended IF
> the user didn't type one? Are the docs wrong or is this a bug?

I thought I already answered that?  I'm almost sure it's because the docs were
written in the Win16 days, and Microsoft is really lame about updating that level of
detail.  You got an old copy of Appleman lying around?  I could probably dig out the
exact text, by loading up an old Win16 SDK, but I imagine you could too.

As I said, "back in the day" files only had *one* dot in them.  Perhaps it actually
worked as you're currently interpreting it, back then?  I don't recall.  It's a
plausible explanation for the confusion, though.
--
..NET: It's About Trust!
http://vfred.mvps.org
Author
26 Mar 2009 9:21 PM
MM
Show quote Hide quote
On Thu, 26 Mar 2009 13:05:58 -0700, "Karl E. Peterson" <k***@mvps.org>
wrote:

>MM wrote:
>>>> What is *actually* returned in the last example above is
>>>> gfh654.xyz.lay, that is, despite my typing an extension that is
>>>> different from the one given in sDefFileExt, Windows is appending
>>>> sDefFileExt to my extension!
>>>
>>>Yeah, that's the way Windows works.  Try opening a new document in Word, and Save
>>>as "Doc1.xyz" -- see what you get.  :-(
>>>
>>>> ******************************************************************************
>>>> This behaviour by Windows makes no sense and is contrary to the
>>>> explanatory notes for GetSaveFileName
>>>> ******************************************************************************
>>>
>>>Maybe it'll help to realize it was written when *every* file was an 8.3 critter,
>>>and that dots weren't valid filename characters?
>>>
>>>> Is there a way of doing what I want with the dialog flags? Or do I
>>>> have to code around it? Or am I completely missing something?
>>>
>>>I think you have to code around it.  And who's to say, really, that the user
>>>doesn't want a file with two dots in it?
>>
>> Fine, but why do the docs say the def extension will be appended IF
>> the user didn't type one? Are the docs wrong or is this a bug?
>
>I thought I already answered that?  I'm almost sure it's because the docs were
>written in the Win16 days, and Microsoft is really lame about updating that level of
>detail.  You got an old copy of Appleman lying around?

Appleman doesn't cover GetSaveFileName. I've just checked the .hlp
file ("Visual Basic Programmer's Guide to the Win32 API")

The explanation for OPENFILENAME Structure on MSDN
(http://tinyurl.com/636mv7) should be fairly up to date, since it
mentions NT, 2000 and XP, and it still says "GetOpenFileName and
GetSaveFileName append this extension to the file name if the user
fails to type an extension."

>  I could probably dig out the
>exact text, by loading up an old Win16 SDK, but I imagine you could too.

I've found a promising-looking file in my CD-ROM archive named
psdk-x86.exe, dated March 2000, so I'll take a look at that.

>As I said, "back in the day" files only had *one* dot in them.  Perhaps it actually
>worked as you're currently interpreting it, back then?  I don't recall.  It's a
>plausible explanation for the confusion, though.

I wouldn't get too hooked up on multiple dots! Wanting TWO dots is
perverse. Expecting Windows to honour its pledge to append the
extension IF the user didn't provide one is not.

MM
Author
26 Mar 2009 11:06 PM
Karl E. Peterson
MM wrote:
>>I thought I already answered that?  I'm almost sure it's because the docs were
>>written in the Win16 days, and Microsoft is really lame about updating that level
>>of detail.  You got an old copy of Appleman lying around?
>
> Appleman doesn't cover GetSaveFileName. I've just checked the .hlp
> file ("Visual Basic Programmer's Guide to the Win32 API")

Oh well.  You want to make it worth it for me to find an old Win16 SDK, so I can
show you the wording hasn't changed (much)?

> The explanation for OPENFILENAME Structure on MSDN
> (http://tinyurl.com/636mv7) should be fairly up to date,

Operative word there being "should" huh?  <shrug>

> since it
> mentions NT, 2000 and XP, and it still says "GetOpenFileName and
> GetSaveFileName append this extension to the file name if the user
> fails to type an extension."

Look, you can argue all you want, but that doesn't change the *fact* that Microsoft
rarely goes over their docs at this level of detail.

>>As I said, "back in the day" files only had *one* dot in them.  Perhaps it
>>actually
>>worked as you're currently interpreting it, back then?  I don't recall.  It's a
>>plausible explanation for the confusion, though.
>
> I wouldn't get too hooked up on multiple dots! Wanting TWO dots is
> perverse.

Heh, I use that style routinely, now.  For example, if you need to email someone an
EXE or REG file, you can do:

   whatever.exe.xyz
   export.reg.txt
   viral.vbs.txt

And so on.  <g>

> Expecting Windows to honour its pledge to append the
> extension IF the user didn't provide one is not.

I'd say your issue is that the definition of "extension" has changed over the years,
but you haven't.
--
..NET: It's About Trust!
http://vfred.mvps.org
Author
26 Mar 2009 4:43 AM
mayayana
If you want the option not to append the default
then include an option for All files *.*. Generally
the way it's done is to include a dropdown option for
all file types that the program saves in, plus the All
Files option.
Author
26 Mar 2009 8:36 AM
MM
On Wed, 25 Mar 2009 23:43:58 -0500, "mayayana" <mayayaX***@rcXXn.com>
wrote:

>  If you want the option not to append the default
>then include an option for All files *.*. Generally
>the way it's done is to include a dropdown option for
>all file types that the program saves in, plus the All
>Files option.

Nope. Didn't work. I just whacked in All files *.* and did Save As
dummy.qwe

It has saved the file as dummy.qwe.lay

Here's my revised call to wrapper around GetSaveFileName:

s = ShowSaveAsDialog("Save Layout File", "Layout Files (*.lay)" &
Chr$(0) & "*.lay" & Chr$(0) & "All Files (*.*)" & Chr$(0) & "*.*" &
Chr$(0), AppPath)

Flags:  OFName.flags = OFN_HIDEREADONLY Or OFN_OVERWRITEPROMPT

MM
Author
26 Mar 2009 3:37 PM
mayayana
Interesting. I just did a little test and found that
it worked the way you describe. I agree that it doesn't
make much sense. There's no point in having an All
Files option if it's going to save with a default extension.
Worse, if your def. extension is more than 3 characters
it will be clipped. According to the docs it's supposed to
work that way. You just have to do it yourself if you want
it to work properly.

(Note: You're supposed to have a double null at the end,
but it doesn't sound like that's stopping your code from
working.)

  I haven't dealt with this for awhile, but looking at my own
code where I've provided several save options plus an
"All files" option, I see that I apparently once knew about
this aberration. :)   What I did was to use:

   .lpstrDefExt = vbNullString

in the OPENFILENAME structure.

  That prevents any extension being added. I then deal
with the result in the save function by checking
..NFilterIndex to find out which filter was selected.

So it would be something like:

If OFN.NFilterIndex = 1 then
  '-- add ".lay" if it wasn't entered by the person saving the file.
End if
'-- If index is 2 then it's all files, so save as entered.



Show quoteHide quote
> Nope. Didn't work. I just whacked in All files *.* and did Save As
> dummy.qwe
>
> It has saved the file as dummy.qwe.lay
>
> Here's my revised call to wrapper around GetSaveFileName:
>
> s = ShowSaveAsDialog("Save Layout File", "Layout Files (*.lay)" &
> Chr$(0) & "*.lay" & Chr$(0) & "All Files (*.*)" & Chr$(0) & "*.*" &
> Chr$(0), AppPath)
>
> Flags:  OFName.flags = OFN_HIDEREADONLY Or OFN_OVERWRITEPROMPT
>
> MM
Author
26 Mar 2009 5:19 PM
MM
On Thu, 26 Mar 2009 10:37:11 -0500, "mayayana" <mayayaX***@rcXXn.com>
wrote:

>   Interesting. I just did a little test and found that
>it worked the way you describe. I agree that it doesn't
>make much sense. There's no point in having an All
>Files option if it's going to save with a default extension.
>Worse, if your def. extension is more than 3 characters
>it will be clipped. According to the docs it's supposed to
>work that way. You just have to do it yourself if you want
>it to work properly.

I'm about to surf to http://ccrp.mvps.org/ to see whether they did
anything.

>
> (Note: You're supposed to have a double null at the end,
>but it doesn't sound like that's stopping your code from
>working.)

Yeah, I've tried it with and without the null(s), but it doesn't seem
to make any difference.

MM
Author
26 Mar 2009 10:55 PM
David Youngblood
"MM" <kylix***@yahoo.co.uk> wrote...
> On Wed, 25 Mar 2009, "mayayana" <mayayaX***@rcXXn.com> wrote:
>
>>  If you want the option not to append the default
>>then include an option for All files *.*. Generally
>>the way it's done is to include a dropdown option for
>>all file types that the program saves in, plus the All
>>Files option.
>
> Nope. Didn't work. I just whacked in All files *.* and did Save As
> dummy.qwe
>
> It has saved the file as dummy.qwe.lay

My understanding is that if the file name contains a registered file
extension then the DefaultExe is not added. If there is not a recognized
extension, then any dot is assumed to be a part of the file name and the
DefaultExe is added.

David
Author
26 Mar 2009 11:14 PM
Karl E. Peterson
David Youngblood wrote:
> My understanding is that if the file name contains a registered file
> extension then the DefaultExe is not added. If there is not a recognized
> extension, then any dot is assumed to be a part of the file name and the
> DefaultExe is added.

Open Word and save the default new document as Doc1.txt - see what happens.
--
..NET: It's About Trust!
http://vfred.mvps.org
Author
26 Mar 2009 11:43 PM
Kevin Provance
"Karl E. Peterson" <k***@mvps.org> wrote in message
news:%23ftvbimrJHA.4460@TK2MSFTNGP06.phx.gbl...
|
| Open Word and save the default new document as Doc1.txt - see what
happens.
| --

That annoying paperclip opened up, pointed at me and then fell to the
virtual floor, laughting its metal ass off.

Odd.
Author
27 Mar 2009 1:28 AM
David Youngblood
"Karl E. Peterson" <k***@mvps.org> wrote in message...
> David Youngblood wrote:
>> My understanding is that if the file name contains a registered file
>> extension then the DefaultExe is not added. If there is not a recognized
>> extension, then any dot is assumed to be a part of the file name and the
>> DefaultExe is added.
>
> Open Word and save the default new document as Doc1.txt - see what
> happens.

Yes, but Word does not use the common dialog, so rules can differ. Try
selecting Plain Text (*.txt) file type and trying that test again with
various file extensions. Word will allow you to save text as any registered
file type (all that I tested anyway), only if the extension is not
recognized will the txt extension be added. I'm using Word 2002, other
versions may work differently.

David
Author
27 Mar 2009 1:41 AM
Karl E. Peterson
David Youngblood wrote:
Show quoteHide quote
> "Karl E. Peterson" <k***@mvps.org> wrote in message...
>> David Youngblood wrote:
>>> My understanding is that if the file name contains a registered file
>>> extension then the DefaultExe is not added. If there is not a recognized
>>> extension, then any dot is assumed to be a part of the file name and the
>>> DefaultExe is added.
>>
>> Open Word and save the default new document as Doc1.txt - see what
>> happens.
>
> Yes, but Word does not use the common dialog, so rules can differ. Try
> selecting Plain Text (*.txt) file type and trying that test again with
> various file extensions. Word will allow you to save text as any registered
> file type (all that I tested anyway), only if the extension is not
> recognized will the txt extension be added. I'm using Word 2002, other
> versions may work differently.

D'oh!  Yep, you're right.  "Nevermind..."  :-)
--
..NET: It's About Trust!
http://vfred.mvps.org
Author
26 Mar 2009 11:36 PM
Bill McCarthy
Show quote Hide quote
"David Youngblood" <d**@flash.net> wrote in message
news:u%237yvXmrJHA.1236@TK2MSFTNGP02.phx.gbl...
> "MM" <kylix***@yahoo.co.uk> wrote...
>> On Wed, 25 Mar 2009, "mayayana" <mayayaX***@rcXXn.com> wrote:
>>
>>>  If you want the option not to append the default
>>>then include an option for All files *.*. Generally
>>>the way it's done is to include a dropdown option for
>>>all file types that the program saves in, plus the All
>>>Files option.
>>
>> Nope. Didn't work. I just whacked in All files *.* and did Save As
>> dummy.qwe
>>
>> It has saved the file as dummy.qwe.lay
>
> My understanding is that if the file name contains a registered file
> extension then the DefaultExe is not added. If there is not a recognized
> extension, then any dot is assumed to be a part of the file name and the
> DefaultExe is added.
>

Yep that's what I see here.
Author
26 Mar 2009 11:59 PM
Karl E. Peterson
Bill McCarthy wrote:
>> My understanding is that if the file name contains a registered file
>> extension then the DefaultExe is not added. If there is not a recognized
>> extension, then any dot is assumed to be a part of the file name and the
>> DefaultExe is added.
>
> Yep that's what I see here.

Open Word and save the default new document as Doc1.txt - see what happens.
--
..NET: It's About Trust!
http://vfred.mvps.org

Bookmark and Share