【问题标题】:kernel driver: How get correct content of each variable in a struct?内核驱动程序:如何获取结构中每个变量的正确内容?
【发布时间】:2017-08-13 23:37:20
【问题描述】:

我的以下代码涉及 struct 包括指针变量,但我无法检索每个变量的正确内容。

有人可以帮帮我吗?

struct MyData
    {
        ULONG Value[3];
        wchar_t *Str1;
        int Str1Len;
        wchar_t *Str2;
        int Str2Len;
    };

    // In the driver, on method that receives commands i have following:

    struct MyData *pData = (struct MyData*) Irp->AssociatedIrp.SystemBuffer; 

    DbgPrint("0x%x \n 0x%x \n 0x%x \n %s \n %d \n %s \n %d", &pData->Value[0], &pData->Value[1], &pData->Value[2], &pData.Str1, &pData->Str1Len, &pData->Str2, &pData->Str2Len);

【问题讨论】:

  • 您不能只将指针从用户模式传递到内核模式。尝试改用字符数组。

标签: c windows visual-studio driver kernel-mode


【解决方案1】:

DbgPrint 的调用方式与printf 的调用方式非常相似,因此您应该传递值,而不是值的地址。所以

DbgPrint("0x%x \n 0x%x \n 0x%x \n %s \n %d \n %s \n %d", &pData->Value[0], // etc...

应该是

DbgPrint("0x%x \n 0x%x \n 0x%x \n %s \n %d \n %s \n %d", pData->Value[0],  // etc...

【讨论】:

  • 此外,格式说明符并不都匹配数据类型。
  • 您的建议适用于ULONG 类型,但不适用于wchar_t*
  • 例如:如果我发送:\\SystemRoot\\System32\\win32k.sys,则不起作用。在每个变量wchar_t* 上只接收一个“\”
  • 查看我的第一条评论。即使ULONG%x 也不正确,应该是%lx。对宽字符串使用%S(我认为)。
  • 我正在为驱动程序上的每个 wchar_t* 发送 PWideChar (Delphi)。
猜你喜欢
  • 2019-07-14
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
  • 1970-01-01
  • 2020-06-04
  • 1970-01-01
  • 2015-11-14
  • 1970-01-01
相关资源
最近更新 更多