转自:http://blog.csdn.net/lostspeed/article/details/11738311
封了一个函数, 从 FILE_OBJECT 中 得到 FilePathName
在WinXpSp3下测试通过.
函数定义
[cpp] view plain copy
- BOOLEAN IsValidUnicodeString(PUNICODE_STRING pstr);
[cpp] view plain copy
- BOOLEAN GetFilePathNameFromFileObject(
- FILE_OBJECT * pFileObj,
- UNICODE_STRING * puniFilePathName);
函数实现
[cpp] view plain copy
- BOOLEAN GetFilePathNameFromFileObject(
- FILE_OBJECT * pFileObj,
- UNICODE_STRING * puniFilePathName)
- {
- /// puniFilePathName 已经被 RtlInitUnicodeString 初始化过,
- /// .Buffer 有MAX_PATH宽字符长度
- BOOLEAN bValidFN_FileObj = FALSE;
- BOOLEAN bValidFN_RelatedFileObj = FALSE;
- PFILE_OBJECT pRelatedFileObject = NULL;
- UNICODE_STRING ustrTmp;
- UNICODE_STRING ustrLink; ///< 分隔符号, e.g. L'\'
- if ((NULL == pFileObj) || (NULL == puniFilePathName))
- return FALSE;
- /// 初始化数据
- RtlInitUnicodeString(&ustrTmp, NULL);
- RtlInitUnicodeString(&ustrLink, L"\");
- RtlZeroMemory(puniFilePathName->Buffer, puniFilePathName->MaximumLength);
- puniFilePathName->Length = 0;
- pRelatedFileObject = pFileObj->RelatedFileObject;
- bValidFN_FileObj = IsValidUnicodeString(&pFileObj->FileName);
- bValidFN_RelatedFileObj =
- IsValidUnicodeString(&pRelatedFileObject->FileName);
- /// 盘符
- IoVolumeDeviceToDosName(pFileObj->DeviceObject, &ustrTmp);
- RtlCopyUnicodeString(puniFilePathName, &ustrTmp);
- RtlFreeUnicodeString(&ustrTmp); ///< !
- /// 相对路径
- /// pRelatedFileObject->FileName 也有可能是空的
- /// 相对全路径名称全部在 pFileObj->FileName
- if (bValidFN_RelatedFileObj)
- {
- /// pRelatedFileObject->FileName.Buffer 可能是有效的
- /// 却不是一个可见的宽字符串, 以 L' '开头
- if ((L'\' != pRelatedFileObject->FileName.Buffer[0])
- &&(L' ' != pRelatedFileObject->FileName.Buffer[0]))
- {
- RtlUnicodeStringCat(puniFilePathName, &ustrLink);
- }
- RtlUnicodeStringCat(puniFilePathName, &pRelatedFileObject->FileName);
- }
- /// 文件名, 也有可能是包含相对路径的全路径名称.
- /// e.g. "WindowsSystemxx.yyy"
- if (bValidFN_FileObj)
- {
- if ((L'\' != pFileObj->FileName.Buffer[0])
- && (L' ' != pFileObj->FileName.Buffer[0]))
- {
- RtlUnicodeStringCat(puniFilePathName, &ustrLink);
- }
- RtlUnicodeStringCat(puniFilePathName, &pFileObj->FileName);
- }
- return (bValidFN_FileObj || bValidFN_RelatedFileObj);
- }
[cpp] view plain copy
- BOOLEAN IsValidUnicodeString(PUNICODE_STRING pstr)
- {
- BOOLEAN bRc = FALSE;
- ULONG ulIndex = 0;
- __try
- {
- if (!MmIsAddressValid(pstr))
- return FALSE;
- if ((NULL == pstr->Buffer) || (0 == pstr->Length))
- return FALSE;
- for (ulIndex = 0; ulIndex < pstr->Length; ulIndex++)
- {
- if (!MmIsAddressValid((UCHAR *)pstr->Buffer + ulIndex))
- return FALSE;
- }
- bRc = TRUE;
- }
- __except(EXCEPTION_EXECUTE_HANDLER)
- {
- bRc = FALSE;
- }
- return bRc;
- }
在分派例程中得到 FILE_OBJECT 方法
[cpp] view plain copy
- pIoStack = IoGetCurrentIrpStackLocation(pIrp);
[cpp] view plain copy
- pFileObject = pIoStack->FileObject;
入参的准备
[cpp] view plain copy
- WCHAR cFilePathNameW[MAX_PATH];
- UNICODE_STRING unistrFilePathName;
- RtlZeroMemory(cFilePathNameW, sizeof(cFilePathNameW));
- RtlInitUnicodeString(&unistrFilePathName, cFilePathNameW);
- unistrFilePathName.MaximumLength = sizeof(cFilePathNameW); ///< !
效果图
[cpp] view plain copy
- DisPatchDeviceControl IOCTL 0x22e000
- cFilePathName[0] = C:
- cFilePathName[1] = C:Documents and SettingsAll UsersApplication DataVMware
- cFilePathName[2] = C:Documents and SettingsAll UsersApplication DataVMwareVMware Tools
- cFilePathName[3] = C:Documents and SettingsAll UsersApplication DataVMwareVMware Tools
- cFilePathName[4] = C:WINDOWSsystem32Msimtf.dll
- cFilePathName[5] = C:WINDOWSsystem32NOTEPAD.EXE
- cFilePathName[6] = C:WINDOWSAppPatchsysmain.sdb
- cFilePathName[7] = C:WINDOWSAppPatchsystest.sdb
- cFilePathName[8] = C:WINDOWSsystem32
- cFilePathName[9] = C:WINDOWS
- cFilePathName[10] = C:WINDOWSsystem32NOTEPAD.EXE.Manifest
- cFilePathName[11] = C:WINDOWSsystem32NOTEPAD.EXE.Config
- cFilePathName[12] = C:WINDOWSWinSxSPoliciesx86_Policy.6.0.Microsoft.Windows.Common-Controls_6595b64144ccf1df_zh-CN_f3ffe327
- cFilePathName[13] = C:WINDOWSAssemblyGACPolicy.6.0.Microsoft.Windows.Common-Controls
- cFilePathName[14] = C:WINDOWSWinSxSPoliciesx86_Policy.6.0.Microsoft.Windows.Common-Controls_6595b64144ccf1df_zh-CHS_6bff526c
- cFilePathName[15] = C:WINDOWSWinSxSPoliciesx86_Policy.6.0.Microsoft.Windows.Common-Controls_6595b64144ccf1df_x-ww_5ddad775
- cFilePathName[16] = C:WINDOWSWinSxSPoliciesx86_Policy.6.0.Microsoft.Windows.Common-Controls_6595b64144ccf1df_x-ww_5ddad7756.0.2600.5512.Policy
- cFilePathName[17] = C:WINDOWSWinSxSPoliciesx86_Policy.6.0.Microsoft.Windows.Common-Controls.mui_6595b64144ccf1df_zh-CN_b45a2b14
- cFilePathName[18] = C:WINDOWSAssemblyGACPolicy.6.0.Microsoft.Windows.Common-Controls.mui
- cFilePathName[19] = C:WINDOWSWinSxSPoliciesx86_Policy.6.0.Microsoft.Windows.Common-Controls.mui_6595b64144ccf1df_zh-CHS_2c599a59
- cFilePathName[20] = C:WINDOWSWinSxSManifestsx86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83.Manifest
- cFilePathName[21] = C:WINDOWSPrefetchNOTEPAD.EXE-336351A9.pf
- cFilePathName[22] = C:Documents and SettingsAdministrator
- cFilePathName[23] = C:Documents and SettingsAdministrator桌面
- cFilePathName[24] = C:DOCUME~1
- cFilePathName[25] = C:DOCUME~1ADMINI~1
- cFilePathName[26] = C:DOCUME~1ADMINI~1LOCALS~1
- cFilePathName[27] = C:Documents and SettingsAdministrator桌面abc.txt
- cFilePathName[28] = C:Documents and SettingsAdministrator桌面
- cFilePathName[29] = C:SYSTEM VOLUME INFORMATION
- cFilePathName[30] = C:Documents and SettingsAdministratorRecent
- cFilePathName[31] = C:Documents and SettingsAdministratorRecentabc.txt.lnk
- cFilePathName[32] = C:SYSTEM VOLUME INFORMATION\_RESTORE{288FCF24-DDBA-4A0A-98C0-50E279B93ECC}
- cFilePathName[33] = C:SYSTEM VOLUME INFORMATION\_RESTORE{288FCF24-DDBA-4A0A-98C0-50E279B93ECC}RP4
- cFilePathName[34] = C:WINDOWSAPPPATCH
- cFilePathName[35] = C:WINDOWSWINSXS
- cFilePathName[36] = C:WINDOWSWINSXSX86_MICROSOFT.WINDOWS.COMMON-CONTROLS_6595B64144CCF1DF_6.0.2600.5512_X-WW_35D4CE83
- cFilePathName[37] = C:WINDOWSSYSTEM32NTDLL.DLL
- cFilePathName[38] = C:WINDOWSSYSTEM32KERNEL32.DLL
- cFilePathName[39] = C:WINDOWSSYSTEM32UNICODE.NLS
- cFilePathName[40] = C:WINDOWSSYSTEM32LOCALE.NLS
- cFilePathName[41] = C:WINDOWSSYSTEM32SORTTBLS.NLS
- cFilePathName[42] = C:WINDOWSSYSTEM32COMDLG32.DLL
- cFilePathName[43] = C:WINDOWSSYSTEM32ADVAPI32.DLL
- cFilePathName[44] = C:WINDOWSSYSTEM32RPCRT4.DLL
- cFilePathName[45] = C:WINDOWSSYSTEM32SECUR32.DLL
- cFilePathName[46] = C:WINDOWSWINSXSX86_MICROSOFT.WINDOWS.COMMON-CONTROLS_6595B64144CCF1DF_6.0.2600.5512_X-WW_35D4CE83COMCTL32.DLL
- cFilePathName[47] = C:WINDOWSSYSTEM32MSVCRT.DLL
- cFilePathName[48] = C:WINDOWSSYSTEM32GDI32.DLL
- cFilePathName[49] = C:WINDOWSSYSTEM32USER32.DLL
- cFilePathName[50] = C:WINDOWSSYSTEM32SHLWAPI.DLL
- cFilePathName[51] = C:WINDOWSSYSTEM32SHELL32.DLL
- cFilePathName[52] = C:WINDOWSSYSTEM32WINSPOOL.DRV
- cFilePathName[53] = C:WINDOWSSYSTEM32SHIMENG.DLL
- cFilePathName[54] = C:WINDOWSAPPPATCHACGENRAL.DLL
- cFilePathName[55] = C:WINDOWSSYSTEM32WINMM.DLL
- cFilePathName[56] = C:WINDOWSSYSTEM32OLE32.DLL
- cFilePathName[57] = C:WINDOWSSYSTEM32OLEAUT32.DLL
- cFilePathName[58] = C:WINDOWSSYSTEM32MSACM32.DLL
- cFilePathName[59] = C:WINDOWSSYSTEM32VERSION.DLL
- cFilePathName[60] = C:WINDOWSSYSTEM32USERENV.DLL
- cFilePathName[61] = C:WINDOWSSYSTEM32UXTHEME.DLL
- cFilePathName[62] = C:WINDOWSSYSTEM32CTYPE.NLS
- cFilePathName[63] = C:WINDOWSSYSTEM32IMM32.DLL
- cFilePathName[64] = C:WINDOWSSYSTEM32LPK.DLL
- cFilePathName[65] = C:WINDOWSSYSTEM32USP10.DLL
- cFilePathName[66] = C:WINDOWSWINDOWSSHELL.MANIFEST
- cFilePathName[67] = C:WINDOWSSYSTEM32MSCTF.DLL
- cFilePathName[68] = C:WINDOWSSYSTEM32MSCTFIME.IME
- cFilePathName[69] = C:SYSTEM VOLUME INFORMATION\_RESTORE{288FCF24-DDBA-4A0A-98C0-50E279B93ECC}RP4CHANGE.LOG
- cFilePathName[70] = C:BOOT.INI
- cFilePathName[71] = C:WINDOWSSYSTEM32WIN32K.SYS
- cFilePathName[72] = C:WINDOWSWinSxSx86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83
- cFilePathName[73] = C:Documents and Settings
- cFilePathName[74] = C:Documents and SettingsAdministratorLocal Settings
- cFilePathName[75] = C:Documents and SettingsAdministratorLocal SettingsHistorydesktop.ini
- cFilePathName[76] = C:WINDOWSWindowsShell.Config
- cFilePathName[77] = C:WINDOWSsystem32SHELL32.dll.124.Manifest
- cFilePathName[78] = C:WINDOWSsystem32SHELL32.dll.124.Config
- cFilePathName[79] = C:WINDOWSPrefetch
- cFilePathName[80] = C:WINDOWSsystem32 804
- cFilePathName[81] = C:WINDOWSMUIFallback 804
- cFilePathName[82] = C:WINDOWSsystem32DRIVERSMUI 804
- cFilePathName[83] = C:WINDOWSsystem32DRIVERSACPI.sys
- cFilePathName[84] = C:WINDOWSsystem32DRIVERSmssmbios.sys
- cFilePathName[85] = C:WINDOWSsystem32DRIVERSintelppm.sys
- cFilePathName[86] = C:WINDOWSsystem32DRIVERSipnat.sys
- cFilePathName[87] = C:WINDOWSSystem32DriversHTTP.sys
- cFilePathName[88] = C:WINDOWSsystem32WBEMLogswmiprov.log
- cFilePathName[89] = C:WINDOWSSoftwareDistributionDataStore
- cFilePathName[90] = C:WINDOWSSoftwareDistributionDataStoreDataStore.edb
- cFilePathName[91] = C:WINDOWSSoftwareDistributionDataStoreDataStore.edb
- cFilePathName[92] = C:WINDOWSSoftwareDistributionDataStore
- cFilePathName[93] = C:WINDOWSSoftwareDistribution
- cFilePathName[94] = C:WINDOWSSoftwareDistribution
- cFilePathName[95] = C:WINDOWS
- cFilePathName[96] = C:WINDOWSSoftwareDistributionDataStoreLogsedb.chk
- cFilePathName[97] = C:WINDOWSSoftwareDistributionDataStoreLogsedb.chk
- cFilePathName[98] = C:WINDOWSSoftwareDistributionDataStoreLogs
- cFilePathName[99] = C:WINDOWSsystem32xpsp2res.dll