从NT /设备名称获取文件路径

时间:2010-09-03 13:15:32

标签: windows device wdm

我想将设备路径转换为文件路径。

我希望按进程ID获取进程名称,因此我使用此代码

PsLookupProcessByProcessId(processId,&pEProcess);
ObOpenObjectByPointer(pEProcess,
                      OBJ_KERNEL_HANDLE,
                      NULL,
                      0,
                      NULL,
                      KernelMode,
                      &hProcess);
ObDereferenceObject (pEProcess);

nts = ZwQueryInformationProcess (hProcess,27,0,0,&ulSize);

但它将路径视为\Device\hardDiskVolume1\windows\system32\taskmgr.exe

但我希望这是一个简单的文件名C:\windows\system32\taskmgr.exe

2 个答案:

答案 0 :(得分:1)

Dobb博士的一篇文章(Jim Conyngham的NT Handle-to-Path Conversion)描述了一种从句柄到DOS路径名的方法:见GetFileNameFromHandleNT()的{​​{3}}。

在您的情况下,由于您已经拥有设备路径,因此您不需要执行handle-to-memory-map-to-get-device-path工作的代码的初始部分。

答案 1 :(得分:1)

// From device file name to DOS filename
BOOL GetFsFileName( LPCTSTR lpDeviceFileName, CString& fsFileName )
{
    BOOL rc = FALSE;

    TCHAR lpDeviceName[0x1000];
    TCHAR lpDrive[3] = _T("A:");

    // Iterating through the drive letters
    for ( TCHAR actDrive = _T('A'); actDrive <= _T('Z'); actDrive++ )
    {
        lpDrive[0] = actDrive;

        // Query the device for the drive letter
        if ( QueryDosDevice( lpDrive, lpDeviceName, 0x1000 ) != 0 )
        {
            // Network drive?
            if ( _tcsnicmp( _T("\\Device\\LanmanRedirector\\"), lpDeviceName, 25 ) == 0 )
            {
                //Mapped network drive 

                char cDriveLetter;
                DWORD dwParam;

                TCHAR lpSharedName[0x1000];

                if ( _stscanf(  lpDeviceName, 
                                _T("\\Device\\LanmanRedirector\\;%c:%d\\%s"), 
                                &cDriveLetter, 
                                &dwParam, 
                                lpSharedName ) != 3 )
                        continue;

                _tcscpy( lpDeviceName, _T("\\Device\\LanmanRedirector\\") );
                _tcscat( lpDeviceName, lpSharedName );
            }

            // Is this the drive letter we are looking for?
            if ( _tcsnicmp( lpDeviceName, lpDeviceFileName, _tcslen( lpDeviceName ) ) == 0 )
            {
                fsFileName = lpDrive;
                fsFileName += (LPCTSTR)( lpDeviceFileName + _tcslen( lpDeviceName ) );

                rc = TRUE;

                break;
            }
        }
    }

    return rc;
}