大理寺少卿 发表于 2025-1-20 20:29:23

驱动开发与系统原理UNICODE_STRING的使用

UNICODE_STRING的五种初始化方式和一些相关操作
--------------------------------------------

#include<ntifs.h>
VOID DriverUnload(PDRIVER_OBJECT pDriver) {
UNREFERENCED_PARAMETER(pDriver);//对没引用的参数进行处理

      DbgPrint("Unload success!\n");

    }


    NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegPath) {//驱动对象指针,注册表路径 DriverEntry不能改
      //指明该参数未被使用,避免被编译器警告
      UNREFERENCED_PARAMETER(pRegPath);
      //注册卸载函数,作用是指定用哪个函数来完成卸载
      pDriverObject->DriverUnload = DriverUnload;

      //第一种方法:宏
      DECLARE_CONST_UNICODE_STRING(usStr1,L"hello world1");
      DbgPrint("%wZ", usStr1);

      //第二种方法:API
      UNICODE_STRING usStr2 = { 0 };
      RtlInitUnicodeString(&usStr2, L"hello world2");
      DbgPrint("%wZ", usStr2);

      //第三种方法
      UNICODE_STRING usStr3 = { 0 };
      WCHAR wCstr = L"hello world3";
      usStr3.Buffer = wCstr;
      usStr3.Length = wcslen(wCstr)*sizeof(WCHAR);
      usStr3.MaximumLength = usStr3.Length;
      DbgPrint("%wZ", usStr3);

      //第四种方法
      UNICODE_STRING usStr4 = RTL_CONSTANT_STRING(L"hello world4");
      DbgPrint("%wZ", usStr4);

      //第五种方法
      UNICODE_STRING usStr5 = { 0 };
      ULONG uLength = (wcslen(L"Hello world5"))*sizeof(WCHAR);
      usStr5.Buffer = ExAllocatePoolWithTag(NonPagedPool, uLength, 'JXNU');
      if (usStr5.Buffer==NULL)
      {
      return STATUS_SUCCESS;//为了防止蓝屏,我设置了返回成功,这个可以自己看返回的列表选择
      }
      RtlZeroMemory(usStr5.Buffer, uLength);
      RtlCopyMemory(usStr5.Buffer, L"Hello world5", uLength);
      usStr5.Length = uLength;
      usStr5.MaximumLength = uLength;
      DbgPrint("%wZ", usStr5);
      ExFreePoolWithTag(usStr5.Buffer, 'JXNU');

      //拷贝
      UNICODE_STRING ustr1 = { 0 };
      WCHAR        wcBuffer;
      RtlInitEmptyUnicodeString(&ustr1, &wcBuffer, 256 * sizeof(WCHAR));
      RtlCopyUnicodeString(&ustr1, &usStr2);
      DbgPrint("%wZ", ustr1);

      //拼接
      RtlAppendUnicodeStringToString(&ustr1, &usStr2);
      DbgPrint("%wZ", ustr1);

      //比较
      if (0 == RtlCompareUnicodeString(&usStr3, &usStr4, TRUE))//true 忽略大小写 false 不忽略大小写
      {
      DbgPrint("==");
      }
      else
      {
      DbgPrint("!=");
      }

      return STATUS_SUCCESS;//状态成功0,一定要有返回值
    }
页: [1]
查看完整版本: 驱动开发与系统原理UNICODE_STRING的使用