tags:

views:

70

answers:

2

i seen this code but i found some confusion so plzz suggest what is memPtr in this code? when i run that code an error is occured memPtr doesnot exist in the current context.

public class WinAPI 
{ 
    [DllImport("ntdll.dll", SetLastError = true)] 
    public static extern IntPtr NtQueryInformationFile(IntPtr fileHandle, ref IO_STATUS_BLOCK IoStatusBlock, IntPtr pInfoBlock, uint length, FILE_INFORMATION_CLASS fileInformation); 

    public struct IO_STATUS_BLOCK 
    { 
        uint status; 
        ulong information; 
    } 
    public struct _FILE_INTERNAL_INFORMATION { 
      public ulong  IndexNumber; 
    }  

    // Abbreviated, there are more values than shown 
    public enum FILE_INFORMATION_CLASS 
    { 
        FileDirectoryInformation = 1,     // 1 
        FileFullDirectoryInformation,     // 2 
        FileBothDirectoryInformation,     // 3 
        FileBasicInformation,         // 4 
        FileStandardInformation,      // 5 
        FileInternalInformation      // 6 
    } 

    [DllImport("kernel32.dll", SetLastError = true)] 
    public static extern bool GetFileInformationByHandle(IntPtr hFile,out BY_HANDLE_FILE_INFORMATION lpFileInformation); 

    public struct BY_HANDLE_FILE_INFORMATION 
    { 
        public uint FileAttributes; 
        public FILETIME CreationTime; 
        public FILETIME LastAccessTime; 
        public FILETIME LastWriteTime; 
        public uint VolumeSerialNumber; 
        public uint FileSizeHigh; 
        public uint FileSizeLow; 
        public uint NumberOfLinks; 
        public uint FileIndexHigh; 
        public uint FileIndexLow; 
    } 
} 

public class Test 
{ 
   public ulong ApproachA() 
   { 
            WinAPI.IO_STATUS_BLOCK iostatus=new WinAPI.IO_STATUS_BLOCK(); 

            WinAPI._FILE_INTERNAL_INFORMATION objectIDInfo = new WinAPI._FILE_INTERNAL_INFORMATION(); 

            int structSize = Marshal.SizeOf(objectIDInfo); 

            FileInfo fi=new FileInfo(@"C:\Temp\testfile.txt"); 
            FileStream fs=fi.Open(FileMode.Open,FileAccess.Read,FileShare.ReadWrite); 

            IntPtr res=WinAPI.NtQueryInformationFile(fs.Handle, ref iostatus, memPtr, (uint)structSize, WinAPI.FILE_INFORMATION_CLASS.FileInternalInformation); 

            objectIDInfo = (WinAPI._FILE_INTERNAL_INFORMATION)Marshal.PtrToStructure(memPtr, typeof(WinAPI._FILE_INTERNAL_INFORMATION)); 

            fs.Close(); 

            Marshal.FreeHGlobal(memPtr);    

            return objectIDInfo.IndexNumber; 

   } 

   public ulong ApproachB() 
   { 
           WinAPI.BY_HANDLE_FILE_INFORMATION objectFileInfo=new WinAPI.BY_HANDLE_FILE_INFORMATION(); 

            FileInfo fi=new FileInfo(@"C:\Temp\testfile.txt"); 
            FileStream fs=fi.Open(FileMode.Open,FileAccess.Read,FileShare.ReadWrite); 

            WinAPI.GetFileInformationByHandle(fs.Handle, out objectFileInfo); 

            fs.Close(); 

            ulong fileIndex = ((ulong)objectFileInfo.FileIndexHigh << 32) + (ulong)objectFileInfo.FileIndexLow; 

            return fileIndex;    
   } 
} 
A: 

http://msdn.microsoft.com/en-us/library/ms804359.aspx

the third argument is a pointer to a buffer that you allocate, to hold the return value. in this case, based on the value of the 5-th parameter it's a pointer to a FILE_INTERNAL_INFORMATION structure

http://msdn.microsoft.com/en-us/library/ms791535.aspx

it is then deferenced on the next line into objectIDInfo.

Jimmy
but what i use instead of memPtr becaz this is not in current context
Ashwin Upadhyay
+1  A: 

You need something like this (not tested):

public ulong ApproachA()
{ 
    WinAPI.IO_STATUS_BLOCK iostatus=new WinAPI.IO_STATUS_BLOCK(); 

    // allocate space for _FILE_INTERNAL_INFORMATION
    IntPtr memPtr =
        Marshal.AllocHGlobal(Marshal.SizeOf(
            typeof(WinAPI._FILE_INTERNAL_INFORMATION)));

    // note the size
    int structSize = Marshal.SizeOf(WinAPI._FILE_INTERNAL_INFORMATION); 

    FileInfo fi=new FileInfo(@"C:\Temp\testfile.txt"); 

    FileStream fs = fi.Open(FileMode.Open,FileAccess.Read,FileShare.ReadWrite); 

    // pass the address of the _FILE_INTERNAL_INFORMATION block
    // and this function will fill it in
    IntPtr res 
        = WinAPI.NtQueryInformationFile(fs.Handle, ref iostatus, 
        memPtr, (uint)structSize,
        WinAPI.FILE_INFORMATION_CLASS.FileInternalInformation); 

    // dereference and cast it to the correct type
    // so our managed code can get the data out of it
    WinAPI._FILE_INTERNAL_INFORMATION objectIDInfo =
        (WinAPI._FILE_INTERNAL_INFORMATION)Marshal.PtrToStructure(memPtr,
        typeof(WinAPI._FILE_INTERNAL_INFORMATION)); 

    fs.Close(); 

    // make a local copy of just the thing we want
    long IndexNumber = objectIDInfo.IndexNumber;

    // free what we allocated
    Marshal.FreeHGlobal(memPtr);    

    return IndexNumber; 
} 
egrunin