需求: 分解字符串, 提取分隔符之间的内容
字符串输入: \\MY-DEV2\MyShareDir\test.txt
分解成3部分: MY-DEV2 + MyShareDir + test.txt
switch(ulCtrlCode) { case IO_CTRL_CODE_UNC_QUERY: DbgPrint("IO_CTRL_CODE_UNC_QUERY\r\n"); sRc = fnUncFileQuery(pInputBuffer, ulinputSize, &UncFileQueryInfo); pIrp->IoStatus.Status = sRc; pIrp->IoStatus.Information = (STATUS_SUCCESS == sRc) ? sizeof(TAG_UNC_FILE_QUERY_INFO) : 0; if (STATUS_SUCCESS == sRc) { if (*pulOutputSize >= sizeof(TAG_UNC_FILE_QUERY_INFO)) { RtlCopyMemory(pOutputBuffer, (PVOID)(&UncFileQueryInfo), sizeof(TAG_UNC_FILE_QUERY_INFO)); } else { pIrp->IoStatus.Status = STATUS_BUFFER_TOO_SMALL; pIrp->IoStatus.Information = 0; } } break; default: DbgPrint("Unknown ulCtrlCode\r\n"); pIrp->IoStatus.Status = STATUS_SUCCESS; pIrp->IoStatus.Information = 0; break; }
NTSTATUS fnUncFileQuery(PVOID pcUncPathName, ULONG ulLenUncPathName, TAG_UNC_FILE_QUERY_INFO * pUncFileQueryInfo) { /// ulLenUncPathName 是字节长度 ULONG dwRc = E_SUCCESS; if (PASSIVE_LEVEL != KeGetCurrentIrql()) { DbgPrint("PASSIVE_LEVEL != KeGetCurrentIrql()\r\n"); return KfRaiseIrql(PASSIVE_LEVEL); } if ( (NULL == pcUncPathName) || (NULL == pUncFileQueryInfo) || (ulLenUncPathName <= 0)) { return STATUS_INVALID_PARAMETER; } /// 从UncPathName中取出 主机名, 共享文件夹名称, 剩下的内容(共享文件中的子文件夹+共享文件名称) dwRc = fnParseUncPathName((wchar_t *)pcUncPathName, pUncFileQueryInfo); if (E_SUCCESS != dwRc) { DbgPrint("fnParseUncPathName(%S) = %d\r\n", pcUncPathName, dwRc); return STATUS_INVALID_PARAMETER; } /// 判断主机名是否是本机 /// 判断共享文件夹 是否为本地的共享文件夹 /// 重组UNC文件全路径名 => 实际的文件名, 写入输出参数 return STATUS_SUCCESS; } DWORD fnParseUncPathName(wchar_t * pszUncPathName, TAG_UNC_FILE_QUERY_INFO * pUncFileQueryInfo) { // \\MY-DEV2\MyShareDir\test.txt ///< \\主机名称\共享文件夹名称\共享子文件夹\共享文件名称 // \\Localhost\MyShareDir\test.txt ///< \\Localhost\共享文件夹名称\共享子文件夹\共享文件名称 // \\192.168.1.60\MyShareDir\test.txt ///< \\IP地址\共享文件夹名称\共享子文件夹\共享文件名称 ULONG dwRc = E_SUCCESS; wchar_t * pszFindBegin = NULL; wchar_t * pszFindEnd = NULL; const wchar_t * pszUncFileNameHead = L"\\\\"; ///< UNC 文件名以 L"\\\\" 起头 const wchar_t * pszUncFileNameSeparator = L"\\"; ///< UNC 文件名 元素间的分隔符 if (PASSIVE_LEVEL != KeGetCurrentIrql()) { DbgPrint("PASSIVE_LEVEL != KeGetCurrentIrql()\r\n"); return KfRaiseIrql(PASSIVE_LEVEL); } RtlZeroMemory(pUncFileQueryInfo, sizeof(TAG_UNC_FILE_QUERY_INFO)); DbgPrint("pszUncPathName = %S\r\n", pszUncPathName); RtlCopyMemory(pUncFileQueryInfo->szUncFilePathName, pszUncPathName, wcslen(pszUncPathName) * sizeof(wchar_t)); /// 查找主机名称 pszFindBegin = wcsstr(pUncFileQueryInfo->szUncFilePathName, pszUncFileNameHead); if ((NULL == pszFindBegin) || (pszFindBegin != pUncFileQueryInfo->szUncFilePathName)) ///< 字符串开头就是引导符号 { return E_PARAM; } pszFindBegin += wcslen(pszUncFileNameHead); ///< 指向引导符号后的字符串 pszFindEnd = wcsstr(pszFindBegin, pszUncFileNameSeparator); if (NULL == pszFindEnd) { return E_PARAM; } RtlCopyMemory(pUncFileQueryInfo->szHostName, pszFindBegin, (pszFindEnd - pszFindBegin) * sizeof(wchar_t)); /// 查找共享文件夹名称 pszFindBegin = pszFindEnd + 1; pszFindEnd = wcsstr(pszFindBegin, pszUncFileNameSeparator); if (NULL == pszFindEnd) { return E_PARAM; } RtlCopyMemory(pUncFileQueryInfo->szShareDir, pszFindBegin, (pszFindEnd - pszFindBegin) * sizeof(wchar_t)); /// 查找 共享文件名, 包含子文件夹 pszFindBegin = pszFindEnd + 1; RtlCopyMemory(pUncFileQueryInfo->szShareFileName, pszFindBegin, wcslen(pszFindBegin) * sizeof(wchar_t)); return dwRc; }