CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    Feb 2010
    Posts
    3

    [RESOLVED] Reading Vista directory files with ReadFile()

    I'm trying to read raw directory data using CreateFile()/ReadFile().
    My CreateFile() call works (i.e. returns a HANDLE that is not INVALID_HANDLE_VALUE) for both an ordinary .txt file and a directory.
    My ReadFile() call works only for the plain file. For the directory case, the first ReadFile() returns 0, i.e. fails, and GetLastError() returns ERROR_INVALID_FUNCTION.
    Given that the CreateFile() succeeded, I don't see that there's a lot that can be wrong with the ReadFile(), though I googled up some comments about the read block size being constrained. In my case it's 64.

    I'm running as myself, and I have admin rights., and all permissions in the directory in question.

    No compiler warnings or anything. I'm running Norton 360 but there's nothing related in the security log.

    Please note, I'm trying to understand why this is not working. I'm not interested in alternative ways of discovering directory contents :-)

    Here's the code. It works as is, but fails as described when I knock the final filename off the pathname.

    Code:
    #include "stdafx.h"
    #include <Windows.h>
    #include <iostream>
    using namespace std;
    #define BufSize 64
    
    int _tmain()
    {
    	HANDLE fh = CreateFile( _T("C:\\Users\\Julian\\Documents\\xx.txt") ,
    		GENERIC_READ,
    		FILE_SHARE_READ,
    		NULL,
    		OPEN_EXISTING,
    		FILE_FLAG_BACKUP_SEMANTICS|FILE_ATTRIBUTE_READONLY,
    		NULL
    		);
    	if( fh != INVALID_HANDLE_VALUE )
    	{
    		char buf[BufSize]; 
    		DWORD nread;
    		DWORD ret = ReadFile(
    			fh,
    			buf,
    			sizeof(buf),
    			&nread,
    			NULL
    		);
    		if( ret == FALSE )
    		{
    			DWORD lastError = GetLastError();
    			cout << "read failed: error " << lastError << endl  ;
    		}
    		else
    			cout << "read ok" << endl  ;
    	}
    	return 0;
    }
    Can anyone help?
    Last edited by Marc G; February 10th, 2010 at 12:47 PM. Reason: Added code tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured