GetFileInformationByHandleEx返回什么卷序列号?

时间:2015-02-24 16:46:07

标签: windows winapi filesystems ntfs

我正在使用GetFileInformationByHandleEx来获取FILE_ID_INFO structure它包含VolumeSerialNumber字段。对于位于我的逻辑卷C上的某个文件:它同时是32B7232F vol C:显示卷序列号是14B8-B987。同样返回Win32_Volume和Win32_LogicalDisk的WMI请求。为什么音量s / n不同?


一些代码:

//
// Internal file system file id. This FILE_ID_INFORMATION is defined in ntifs.h for kernel mode and the same structure with name
// FILE_ID_INFO in winnt.h for user mode, so we should implement our own. FILE_ID_128 is common in winnt.h  and ntifs.h.
//
typedef struct _MYFILE_ID_INFORMATION {
    ULONGLONG VolumeSerialNumber; // Protected file volume serial number
    FILE_ID_128 FileId;           // File id on volume
} MYFILE_ID_INFORMATION, *PMYFILE_ID_INFORMATION;

_Must_inspect_result_
HRESULT
WINAPI
GetFileId(
_In_ LPCWSTR filePath,
_Out_ PMYFILE_ID_INFORMATION info
)
/*++

Routine Description:

    Retrieves internal file system file id for a given file.

Arguments:

    filePath - The path and name of the file to create.

    info - pointer to a caller-allocated variable that receives the address of internal file system file id structure.

Return Value:

    The final status of this operation.

--*/
{
    HANDLE hFile;
    HRESULT result = S_OK;

    hFile = CreateFileW(filePath,
        GENERIC_READ,
        FILE_SHARE_WRITE | FILE_SHARE_READ,
        NULL,
        OPEN_EXISTING,
        (DWORD)NULL,
        (HANDLE)NULL);

    if (hFile == INVALID_HANDLE_VALUE) {
        result = HRESULT_FROM_WIN32(GetLastError());
    }

    __try
    {
        BOOL retVal = GetFileInformationByHandleEx(hFile, FileIdInfo, (LPVOID)info, sizeof(MYFILE_ID_INFORMATION));
        if (!retVal) {
            result = HRESULT_FROM_WIN32(GetLastError());
        }
    }
    __finally
    {
        CloseHandle(hFile);
    }

    return result;
}

0 个答案:

没有答案