这段代码中的 memPtr 是什么?

发布于 2024-08-20 15:23:58 字数 2991 浏览 5 评论 0原文

我看到了这段代码,但我发现有些混乱,所以请建议 这段代码中的 memPtr 是什么?当我运行该代码时,发生错误 memPtr 在当前上下文中不存在。

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;    
   } 
} 

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;    
   } 
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许一世地老天荒 2024-08-27 15:23:58

你需要这样的东西(未经测试):

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; 
} 

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; 
} 
|煩躁 2024-08-27 15:23:58

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

第三个参数是指向您分配的缓冲区的指针,用于保存返回值。
在这种情况下,根据第 5 个参数的值,它是一个指向 FILE_INTERNAL_INFORMATION 结构的指针

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

然后在下一行将其引用到 objectIDInfo 中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文