【问题标题】:UNICODE_STRING to std String ConversionUNICODE_STRING 到 std 字符串的转换
【发布时间】:2013-03-11 03:20:29
【问题描述】:

我正在使用pFileObject->FileName来获取内核模式过滤驱动程序中打开的文件名,this返回的文件名格式为UNICODE_STRING强>。我想把它转换成std String。什么方法???如果可能,请提供示例... 下面是代码

NTSTATUS FsFilterDispatchCreate(
    __in PDEVICE_OBJECT DeviceObject,
    __in PIRP           Irp
    )
{
    PFILE_OBJECT pFileObject = IoGetCurrentIrpStackLocation(Irp)->FileObject;

    DbgPrint("%wZ\n", &pFileObject->FileName);

    return FsFilterDispatchPassThrough(DeviceObject, Irp);
}

【问题讨论】:

  • 在设备驱动程序中你永远不应该做的两件事:假设默认的 new 操作符有效并且假设有一个有效的代码页。 不要在驱动程序中使用 std::string。
  • 正如汉斯所说,这个计划是行不通的。内核模式下没有MSVCR90.DLL。
  • @Hans 我不打算在 KM 中使用 std::string 而是在 UM 中使用它

标签: windows driver kernel-mode filter-driver


【解决方案1】:

我同意汉斯的评论。使 std:: 类在 Windows 内核模式下工作即使不是不可能也是极其困难的。默认的 WinDDK 环境是 C 而不是 C++。最好的办法是将 UNICODE_STRING 转换为 ANSI 空终止字符串。 (您可以使用 DbgPrint("%s"...) 等进行打印)。请参见下面的示例。

UNICODE_STRING tmp;

// ...

ANSI_STRING dest;
ULONG unicodeBufferSize = tmp.Length;
// Length of unicode string in bytes must be enough to keep ANSI string
dest.Buffer = (PCHAR)ExAllocatePool(NonPagedPool, unicodeBufferSize+1);
// check for allocation failure... 
dest.Length = 0;
dest.MaximumLength = unicodeBufferSize+1;

RtlUnicodeStringToAnsiString(&dest, &tmp, FALSE);
// check for failure...
dest.Buffer[dest.Length] = 0; // now we get it in dest.Buffer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2020-04-23
    • 2019-03-31
    • 1970-01-01
    • 2014-03-24
    相关资源
    最近更新 更多