Solved

Trouble with using FindFirstFile in MFC app

Posted on 1998-05-21
9
1,210 Views
Last Modified: 2013-11-20
I have a dialog based application. From the OnInitDialog() I want to build a list of a specific kind of files and update my combo box on the dialog. To get a listing of those kind of files from the hard disk, I call the Win32 functions FindFirstFile and FindNextFile. I had successfully tested these functions from a console app and they seemed to be fine, but my dialog app cannot execute the FindFirstFile and I get an unhandeled exception in NTDLL.DLL for an access violation. Another information that might be helpful is the prototype of FindFirstFile is as:
HANDLE FindFirstFile (LPCTSTR, LPWIN32_FIND_DATA);
My calling sequence is as follows:
char myFile="C:\\*.ppp";  // for example
LPWIN32_FIND_DATA lpFindFileData;
HANDLE myHandle;

myHandle = FindFirstFile (myFile, lpFindFileData);

       Another question I have is if I can use normal C based char string pointers in place of LPCTSTRS?
0
Comment
Question by:syedm
  • 4
  • 3
  • 2
9 Comments
 
LVL 3

Accepted Solution

by:
tma050898 earned 200 total points
Comment Utility
Here's an example of how I've done this in an MFC application.

WIN32_FIND_DATA findData;                                      
HANDLE hFile;
CString strFileMask = "CommSvr*.DLL";

char szCurrentDirectory[CHAR_MAX];
GetCurrentDirectory(sizeof(szCurrentDirectory), szCurrentDirectory);

ZeroMemory(&findData, sizeof(findData));
hFile = FindFirstFile(strFileMask, &findData);
if (INVALID_HANDLE_VALUE != hFile)
{
 do
 {
  if ((findData.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) ||
  (findData.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE))
  {
   TRACE1("*** Found '%s'\n", findData.cFileName);
  }
 } while (FindNextFile(hFile, &findData));
}
FindClose(hFile);

HTH,
Tom

0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
You can do this a little simpler with the CFileFind class.  ie:

    CFileFind finder;
    BOOL bWorking = finder.FindFile("C:\\*.ppp");
    while (bWorking) {
        bWorking = finder.FindNextFile();
        cout << (LPCTSTR) finder.GetFileName() << endl;
    }

this is much simpler than working directly with the API.

BTW:tma .. why do you get the current directory in the code above.  This does not seem to be required (or used).

0
 
LVL 3

Expert Comment

by:tma050898
Comment Utility
RONSLOW,

The code above was code taken from an actual application with a lot of my application-specific code cut out. I just missed the declaration of that variable. It has no bearing on this code now.

Tom


0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
I thought as much .. happens to me all the time when I quote extracts from my working code .. either I miss something I needed or put in something I don't.

Just thought I'd point that out so that syendm would realise this also an not think that this was required for some mysterious reason.


BTW:
> Another question I have is if I can use normal C based
> char string pointers in place of LPCTSTRS?

Yes .. as long as you are working not working with UniCode.  The TCHAR and xxxTSTR types depend on the unicode settings.  When using unicode, these correspond to wide (16-bit) characters.  Otherwise they a 8-bit chars.

However, generally when working with windows, use TCHAR and LPTSTR etc for all your 'C' characters and strings (unless you want to always use 8-bit characters)

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:syedm
Comment Utility
Actually I figured out the second method yu pointed out, namely using the CFindClass class. Also before that I played around with placing FindFirstFile and FindNextFile at different places in the application and it always behaved differently. For instance it never worked in the OnInitDialog(). Next I placed a button on the dialog and moved these to the button handler and it always worked. But I needed this at the time dialog was initialized, so I tried using these API's within the App constructor (the idea was that I will store the names within a linked list or something and look it up when the InitDialog() happens). With this approach I had FindFirstFile working but the FindNextFile failed!!! Lastly I ended up using CFindFile within the App constructor and it seemed to work perfectly.

    I guess the bottom line is that with MFC yu get a lot of functionality but yu also are disadvantaged by the fact that a LOT is happening behind the veils of MFC and if something does not work as yu expected yu end up using a lot of trial/error methods.
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
BTW: CFindFile was my suggestion, not tma's

0
 
LVL 3

Expert Comment

by:tma050898
Comment Utility
syedm,

Did you use the code above in the OnInitDialog and it failed??? I'm just curious because that exact code is located in my OnInitDialog and it has always worked perfectly.

Tom

0
 

Author Comment

by:syedm
Comment Utility
Ronslay, I'm sorry, yu'r right.

Tom,
   Yeah, it was pretty much similar to your code, except that it was not getting the current directory and that it was structured in form of
while() { ... }
rather than
do { .... } while ();
Also the code using CFindFile is in the first form, although after exiting from the while loop it again gets the file name using CFindFile::GetFileName().
This code is also a portion of App constructor, I did not get a chance to try CFindFile in OnInitDialog.
-Mohammad.


0
 
LVL 3

Expert Comment

by:tma050898
Comment Utility
sysedm,

If the CFindFile works, that's fine as long as you find something that works for you.

My only concern was that you indicated that the FindFirstFile wasn't working for you and I know that the code snippet I gave is in fact working. As a matter of fact, it's working in the IBM WorldBook Multimedia Encyclopedia product right now ;) Therefore, if you want to use the FindFirstFile and isn't working in your OnInitDialog or you just want to know why it doesn't work in OnInitDialog, post the code and I'll take a look at it.

Thanks,
Tom
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to know only "File created" with EventLog 3 29
dog bark java program 15 81
Fibonacci challenge 11 82
noX challenge 17 75
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

10 Experts available now in Live!

Get 1:1 Help Now