How to determine that two Win32 API handles represent the same object?

风格不统一 提交于 2019-12-03 17:23:33

You could use GetFinalPathNameByHandle and compare the file path of both handles. https://msdn.microsoft.com/en-us/library/windows/desktop/aa364962(v=vs.85).aspx

IInspectable

The GetFileInformationByHandle API returns information that can be used to uniquely identify the referenced object:

You can compare the VolumeSerialNumber and FileIndex members returned in the BY_HANDLE_FILE_INFORMATION structure to determine if two paths map to the same target; for example, you can compare two file paths and determine if they map to the same directory.

For example:

bool SameFile( HANDLE h1, HANDLE h2 ) {
    BY_HANDLE_FILE_INFORMATION bhfi1 = { 0 };
    BY_HANDLE_FILE_INFORMATION bhfi2 = { 0 };
    if ( ::GetFileInformationByHandle( h1, &bhfi1 ) &&
         ::GetFileInformationByHandle( h2, &bhfi2 ) ) {
        return ( ( bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh ) &&
                 ( bhfi1.nFileIndexLow  == bhfi2.nFileIndexLow ) &&
                 ( bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber ) );
    }
    return false;
}

New for Windows 10:

CompareObjectHandles - Compares two object handles to determine if they refer to the same underlying kernel object.

https://msdn.microsoft.com/en-us/library/windows/desktop/mt438733(v=vs.85).aspx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!