Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1257
  • Last Modified:

Trouble with using FindFirstFile in MFC app

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
syedm
Asked:
syedm
  • 4
  • 3
  • 2
1 Solution
 
tma050898Commented:
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
 
RONSLOWCommented:
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
 
tma050898Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RONSLOWCommented:
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
 
syedmAuthor Commented:
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
 
RONSLOWCommented:
BTW: CFindFile was my suggestion, not tma's

0
 
tma050898Commented:
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
 
syedmAuthor Commented:
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
 
tma050898Commented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now