Opening a directory with NtCreateFile

This is a discussion on Opening a directory with NtCreateFile within the Windows Programming forums, part of the Platform Specific Boards category; I want to get an handle to a directory and query it,via NtCreateFile() and NtQueryDirectoryFile() : Code: #include <cstdlib> #include ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    69

    Opening a directory with NtCreateFile

    I want to get an handle to a directory and query it,via NtCreateFile() and NtQueryDirectoryFile() :

    Code:
    #include <cstdlib>
    #include <windows.h>
    #include <cstdio>
    
    // you also need some #defines from Windows DDK,like 
    // IO_STATUS_BLOCK,FILE_INFORMATION_CLASS etc.
    
    // this is my definition of OBJECT_ATTRIBUTES,perhaps it's incorrect??
    
    
     typedef struct _OBJECT_ATTRIBUTES {
        ULONG uLength;
        HANDLE  RootDirectory;
        PUNICODE_STRING  ObjectName;
        ULONG  Attributes;
        PSECURITY_DESCRIPTOR  SecurityDescriptor;
        PSECURITY_QUALITY_OF_SERVICE SecurityQualityOfService;  
     } OBJECT_ATTRIBUTES,*POBJECT_ATTRIBUTES;
    
    
    
    int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){
    
    
     typedef int (WINAPI * NtQueryDirFunc)(HANDLE,HANDLE,PVOID,PVOID,PIO_STATUS_BLOCK,PVOID,ULONG,FILE_INFORMATION_CLASS,BOOL,PUNICODE_STRING,BOOL);
     NtQueryDirFunc NtQueryDirectoryFile = (NtQueryDirFunc)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQueryDirectoryFile");
    
     PFILE_DIRECTORY_INFORMATION pDirInfo = (PFILE_DIRECTORY_INFORMATION)calloc(1,sizeof(FILE_DIRECTORY_INFORMATION) + MAX_PATH);
    
     typedef int (WINAPI * NtCreateFileFunc)(PHANDLE,DWORD,POBJECT_ATTRIBUTES,PVOID,PVOID,ULONG,ULONG,ULONG,ULONG,PVOID,ULONG);
     NtCreateFileFunc ntcreatefile = (NtCreateFileFunc)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtCreateFile");
    
    	typedef DWORD (WINAPI *PfRtlAnsiStringToUnicodeString)(PUNICODE_STRING, PANSI_STRING, BOOL);
    	typedef DWORD (WINAPI *PfRtlUnicodeStringToAnsiString)(PANSI_STRING, PUNICODE_STRING, BOOL);
    	typedef DWORD (WINAPI *PfRtlCompareUnicodeString)(PUNICODE_STRING, PUNICODE_STRING, BOOL);
    
    	PfRtlAnsiStringToUnicodeString MyRtlAnsiStringToUnicodeString;
    	PfRtlUnicodeStringToAnsiString MyRtlUnicodeStringToAnsiString;
    	PfRtlCompareUnicodeString MyRtlCompareUnicodeString;
    
     MyRtlAnsiStringToUnicodeString = (PfRtlAnsiStringToUnicodeString)GetProcAddress(LoadLibrary("ntdll.dll"),"RtlAnsiStringToUnicodeString");
     MyRtlUnicodeStringToAnsiString = (PfRtlUnicodeStringToAnsiString)GetProcAddress(LoadLibrary("ntdll.dll"),"RtlUnicodeStringToAnsiString");
     MyRtlCompareUnicodeString = (PfRtlCompareUnicodeString)GetProcAddress(LoadLibrary("ntdll.dll"), "RtlCompareUnicodeString");
    
     OBJECT_ATTRIBUTES oa;
     UNICODE_STRING us;
     ANSI_STRING as;
    
     const char szDir[] = "c:\\windows\\";  
    
     as.Buffer = (char *)malloc(strlen(szDir) + 1);
     strcpy(as.Buffer,szDir);
     as.Length = as.MaximumLength = us.MaximumLength = us.Length = strlen(szDir);
    
      // convert directory name from ANSI to UNICODE
     MyRtlAnsiStringToUnicodeString(&us, &as, TRUE);  
    
     MessageBoxW(NULL,us.Buffer,us.Buffer,MB_OK);  // show directory name
    
     oa.uLength = sizeof(oa);
     oa.RootDirectory = NULL;
     oa.ObjectName = &us;
     oa.Attributes = OBJ_KERNEL_HANDLE ;
     oa.SecurityDescriptor = NULL;
     oa.SecurityQualityOfService = NULL;
    
     ULONG info;
    
     PIO_STATUS_BLOCK pIO = (PIO_STATUS_BLOCK)malloc(sizeof(IO_STATUS_BLOCK));
     pIO->Information = &info;
    
     HANDLE hDir;
    
     char c[20];
     sprintf(c,"%u", ntcreatefile(&hDir,FILE_TRAVERSE | FILE_LIST_DIRECTORY,&oa,pIO,NULL,0,0,FILE_OPEN,FILE_DIRECTORY_FILE,NULL,0) == STATUS_SUCCESS);
     MessageBox(NULL,c,"",MB_OK);
    
     sprintf(c,"%u",STATUS_SUCCESS == NtQueryDirectoryFile(hDir,NULL,NULL,NULL,pIO,pDirInfo,sizeof(FILE_DIRECTORY_INFORMATION) + MAX_PATH,FileDirectoryInformation,FALSE,NULL,TRUE));
     
     MessageBox(NULL,c,"",MB_OK);
    
     CloseHandle(hDir);
    
     return 0;
    }
    The output is always 0...why?? perhaps the definition of OBJECT_ATTRIBUTES was wrong?? why does the call to NtCreateFile fails?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    Perhaps calling getLastError() would help.
    Like checking most of your function call results as opposed to none of them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    AFAIK NtCreateFile is a low level function to be used mainly in system level code. Using it in user mode probably wont work.

    Use the documented CreateFile to do whatever you are trying to do

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    what are you trying to do with it? If you want to get a list of the files in a directory then use FindFirstFile() and FindNextFile() (CreateFile will not work on directories either). MS-Windows programs can't read directories like they do in *nix.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    69
    Quote Originally Posted by Ancient Dragon
    what are you trying to do with it? If you want to get a list of the files in a directory then use FindFirstFile() and FindNextFile() (CreateFile will not work on directories either). MS-Windows programs can't read directories like they do in *nix.
    it's possible,instead
    you have to use CreateFile with FILE_FLAG_BACKUP_SEMANTICS if SE_BACKUP_NAME and SE_RESTORE_NAME privileges are enabled on your process,otherwise enable them first and then use createfile like above

    ok thanks,i've already solved this problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 01:54 AM
  2. Opening all files in a directory
    By TKMan in forum C Programming
    Replies: 2
    Last Post: 11-11-2005, 12:27 PM
  3. Opening file in directory of the running program
    By Hankyaku in forum C++ Programming
    Replies: 6
    Last Post: 10-11-2003, 06:41 AM
  4. Opening files with a directory browser?
    By Labelizm in forum Windows Programming
    Replies: 2
    Last Post: 07-22-2002, 09:40 AM
  5. Opening a file in a database or different directory
    By bc120 in forum C Programming
    Replies: 1
    Last Post: 01-07-2002, 12:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21