FindFirstFile, CFile, and CArchive

Within my CDocument's serialization class, I call the below function that tries to open up all files with the .asm extension in a certain directory and read each line one at a time.  Since this is called from my application's serialization function in the CDocument class, this function is only invoked when

The problem I am having is that on the while(asmArchive.ReadString(strOneLine)) line a message box (looks like and AfxMessageBox) pops up indicating that the file I selected by going to File->open can not
opened. This function, however, shouldn't touch the file that I selected by going to File->open.  This function is simply invoked when the user takes that action.

So, can anyone see what I am doing wrong?  I am creating the CFile and CArchive classes correctly?  Is there a problem since a file handle already exists for the file before I create the CFile and CArchive objects? How about the use of FindFirstFile and FindNext file in the attempt to search a directory.

Or is the problem have to do with the fact that I am creating another CArchive object (for reading files in the selected dir) while handling the CArchive object that was created when the user selects File->open?

CStringList* Casm::getASMLabels (CString m_workingDir)
     CString str;
     CString strOneLine;
     char buf[512];

     WIN32_FIND_DATA fd;
     m_workingDir.Format("%s\\*.asm", m_workingDir);
     HANDLE hFind = ::FindFirstFile ( _T(m_workingDir), &fd);

     if ( hFind != INVALID_HANDLE_VALUE ) {
          do {
               if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {    
                    CFile asmFile( (int) hFind );
                    CArchive asmArchive( &asmFile, CArchive::load, 512, buf );

                    // read data in, one line at a time
                    while(asmArchive.ReadString(strOneLine)) {
                                     // DO SOME STUFF
          } while (::FindNextFile (hFind, &fd));
          ::FindClose (hFind);

     return &m_ASMLabelNames;
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Question seems very big. Cant' read. I give up...
Do not use return handle from FindFirstFile() to open file in CFile(). Use file name (cFileName) from WIN32_FIND_DATA structure. Just make sure that file name which you passing into CFile() is fully qualified.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bpwallacAuthor Commented:
Thanks for the tip....that is definitely what I was doing wrong.  I the function is working well now.

I guess I just don't understand exactly what a HANDLE is in Win32.  When I looked at the constructor documentation for CFile,

CFile( int hFile );

I assumed that the argument is a handle to a file.  I guess I was wrong...

Anyways, it is working now...Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

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.