zoukankan      html  css  js  c++  java
  • Windows IOCTL 获取 Volume 信息 (FSCTL_GET_NTFS_VOLUME_DATA)

    #define UNICODE 1
    #define _UNICODE 1
    
    /* The code of interest is in the subroutine GetDriveGeometry. The
    code in main shows how to interpret the results of the call. */
    
    #include <windows.h>
    #include <winioctl.h>
    #include <stdio.h>
    
    #define wszDrive L"\\.\PhysicalDrive0"
    
    //#define wszVol L"\\?\Volume{32ef8775-ad83-45dd-9617-312e4c36f1ca}"
    #define wszVol L"\\.\M:"
    //#define wszVol L"\\?\Volume{fd47549e-0000-0000-0000-402400000000}"
    
    BOOL GetVolInfo(LPWSTR wszPath, NTFS_VOLUME_DATA_BUFFER *pdg)
    {
        HANDLE hDevice = INVALID_HANDLE_VALUE;  // handle to the drive to be examined 
        BOOL bResult = FALSE;                 // results flag
        DWORD bytesReturned = 0;                     // discard results
    
        hDevice = CreateFileW(wszPath,
            FILE_EXECUTE,
            FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
            NULL,             // default security attributes
            OPEN_EXISTING,    // disposition
            FILE_ATTRIBUTE_NORMAL,                // file attributes
            NULL);            // do not copy file attributes
    
        if (hDevice == INVALID_HANDLE_VALUE)    // cannot open the drive
        {
            printf("Failed to open vol %ls
    ", wszPath);
            return (FALSE);
        }
    
        bResult = DeviceIoControl(hDevice,                       // device to be queried
            FSCTL_GET_NTFS_VOLUME_DATA, // operation to perform
            NULL, 0,                       // no input buffer
            (LPVOID)pdg, sizeof(*pdg),            // output buffer
            &bytesReturned,                         // # bytes returned
            (LPOVERLAPPED)NULL);          // synchronous I/O
    
        CloseHandle(hDevice);
    
        printf("FSCTL_GET_NTFS_VOLUME_DATA bytesReturned %ld
    ",
            bytesReturned);
    
        return (bResult);
    }
    
    int wmain(int argc, wchar_t *argv[])
    {
        NTFS_VOLUME_DATA_BUFFER pdg = { 0 }; // disk drive geometry structure
        BOOL bResult = FALSE;      // generic results flag
        ULONGLONG DiskSize = 0;    // size of the drive, in bytes
    
        bResult = GetVolInfo(wszVol, &pdg);
    
        if (bResult)
        {
            wprintf(L"Volume path      = %ws
    ", wszVol);
            wprintf(L"BytesPerCluster  = %ld
    ", pdg.BytesPerCluster);
        }
        else
        {
            wprintf(L"GetVolInfo %ls failed. Error %ld.
    ", wszVol, GetLastError());
        }
    
        return ((int)bResult);
    }
  • 相关阅读:
    Linux下环境搭建(一)——java、tomcat配置
    Fiddler使用过程中容易忽略的小技巧
    Jenkins环境搭建(6)-修改自动化测试报告的样式
    Jmeter——JSON Extractor后置处理器介绍2
    Jmeter——实现Basic Auth方式登录
    Jmeter——JSON Extractor后置处理器介绍1
    基础拾遗------泛型详解
    利用委托与Lambada创建和调用webapi接口
    quartz.net任务调度:源码及使用文档
    quartz.net插件类库封装(含源码)
  • 原文地址:https://www.cnblogs.com/liujx2019/p/14790398.html
Copyright © 2011-2022 走看看