GetOpenFileName( )

Posted on 2000-04-21
Medium Priority
Last Modified: 2013-12-03
Im using GetOpenFileName() to pull in one or multiple files from a user.  The problem is how do I go about parsing the "strFile" field in the OPENFILENAME structure, since it contains many embedded strings?

For example if the user choose "hello.txt and "goodbye.txt" in the "c:\words" folder the string would look like this...


if the user just chose "hello.txt" in the "c:\words" folder the string would look like this...


Whats a good algrorithm to use to end up with this...

MyString1 = "c:\words\hello.txt"
MyString2 = "c:\words\goodbye.txt"

Question by:dmaroff
  • 2
LVL 15

Accepted Solution

NickRepin earned 300 total points
ID: 2739651
<<If multiple files were selected, the string is of this form:

   Drive: \Directory Name\0FileName 1\0FileName 2\0FileName n\0\0.

The Directory Name is listed first. Then each file that was selected is listed with a terminating NULL Character, except for the last filename, which is terminated with two NULL characters. The two NULL characters signal the end of the string.
If a single file was selected, the string is of this form:

   Drive: \Directory Name\FileName\0\0.

The Directory Name in this case is not terminated by a NULL character, and the file name is terminated with two NULL characters.

Applications must parse the string returned in the lsptrFile member. In doing so, they should make provisions in the parsing code to have a case where the user can make a single selection (even though the OFN_ALLOWMULTISELECT flag is set) or multiple selections.

NOTE: When using OFN_ALLOWMULTISELECT under Windows 95, you need to use the OFN_EXPLORER flag to get the explorer style dialog and NULL terminated strings. If you don't use OFN_EXPLORER with OFN_ALLOWMULTISELECT, you get the old style dialog and space-delimited strings. >>




//Detect if a single file was selected:
int len=strlen(ofn.lpstrFile);
if(ofn.lpstrFile[len+1]==0) {
   // Single file selected,
   // ofn.lpstrFile contains the full path and file name.
   printf("Single file=%s",ofn.lpstrFile);
else {

// ofn.lpstrFile contains directory

// Enumerate all file names.
while(ofn.lpstrFile[0]) {
   // ofn.lpstrFile contains the file name
   printf("Next file=%s",ofn.lpstrFile);
   // Find next name



Author Comment

ID: 2741240
Ok, Im going to try it out and let you know.


Author Comment

ID: 2743954
Your code worked beautifully.

Thanks a lot,


Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question