YAHOOOOO
YAHOOOOO

Reputation: 957

Get file path from NT/Device Name

I want to convert device path to a file path.

I want to get process name by process id, so I am using this code

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

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

But it gives path as \Device\hardDiskVolume1\windows\system32\taskmgr.exe

But I want this as a plain filename C:\windows\system32\taskmgr.exe

Upvotes: 1

Views: 3888

Answers (2)

Marcus Carey
Marcus Carey

Reputation: 33

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

Upvotes: 1

Ashutosh Mehra
Ashutosh Mehra

Reputation: 2602

There's an article in Dr. Dobb's (NT Handle-to-Path Conversion by Jim Conyngham) that describes a way of getting from a handle to DOS path name: See the listing of GetFileNameFromHandleNT().

In your case, since you already have the device path, you don't need the initial parts of that code that does the handle-to-memory-map-to-get-device-path work.

Upvotes: 1

Related Questions