Solved

GetOpenFileName( )

Posted on 2000-04-21
3
575 Views
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...

"c:\words\hello.txt"goodbye.txt""

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

"c:\words\hello.txt"

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

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


Thanks,
-Dan
0
Comment
Question by:dmaroff
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
NickRepin earned 75 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. >>

So,

1) Use OFN_EXPLORER along with OFN_ALLOWMULTISELECT

2)
OPENFILENAME ofn;
GetOpenFileName(...)


//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
printf("Directory=%s",ofn.lpstrFile);

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

}


0
 

Author Comment

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

Thanks,
-Dan
0
 

Author Comment

by:dmaroff
ID: 2743954
Your code worked beautifully.

Thanks a lot,

-Dan
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now