[RESOLVED] Reading Vista directory files with ReadFile()
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] Reading Vista directory files with ReadFile()

  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 11:47 AM. Reason: Added code tags

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Reading Vista directory files with ReadFile()

    Quote Originally Posted by msdn
    Parameters

    hFile [in]

    A handle to the device (for example, a file, file stream, physical disk, volume, console buffer, tape drive, socket, communications resource, mailslot, or pipe).
    Doesn't tell anything about a directory.
    I wander what you expect to read from directory ?

    Kurt

  3. #3
    Join Date
    Feb 2010
    Posts
    3

    Re: Reading Vista directory files with ReadFile()

    CreateFile allows me to open a directory for reading:

    "To open a directory using CreateFile, specify the FILE_FLAG_BACKUP_SEMANTICS flag as part of dwFlagsAndAttributes. Appropriate security checks still apply when this flag is used without SE_BACKUP_NAME and SE_RESTORE_NAME privileges."

    Why would I be able to open it for reading if I can't then go on to read it? I haven't found any documentation that says directories can't be read in this way.

    (Judging by other code I found searching the web, there was a time when this would have worked, though not necessarily recently or in Vista.)

    I'm expecting that directories are represented on disk in a similar way to other file types (albeit with a particular internal record structure) and I'm expecting to see a representation of a directory as a stream of bytes.

    Does anyone know whether it's the case, as my example suggests, that directories cannot be read in this way, and if so what's the reason? Thanks.

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Reading Vista directory files with ReadFile()

    If I'm reading the MSDN correctly, you cannot use ReadFile on a directory handle:

    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    Viggy

  5. #5
    Join Date
    Feb 2010
    Posts
    3

    Re: Reading Vista directory files with ReadFile()

    Thanks, that answers my question ( except for the why part, which I'm not so concerned about ).

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center