【问题标题】:Get file path from NT/Device Name从 NT/设备名称获取文件路径
【发布时间】:2010-09-03 13:15:31
【问题描述】:

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

我想通过进程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

【问题讨论】:

    标签: windows device wdm


    【解决方案1】:

    Dobb 博士(Jim Conyngham 的NT Handle-to-Path Conversion)中有一篇文章描述了一种从句柄获取 DOS 路径名的方法:请参阅 GetFileNameFromHandleNT() 的 listing。

    在您的情况下,由于您已经拥有设备路径,因此您不需要执行句柄到内存映射到获取设备路径工作的代码的初始部分。

    【讨论】:

      【解决方案2】:
      // 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-01
        • 2011-01-25
        • 2011-04-13
        • 1970-01-01
        • 2017-10-08
        相关资源
        最近更新 更多