Ring3层 UNICODE_STRING

今天写驱动用到UNICODE_STRING,就在Ring3层抠了一些源代码,学习一下,不多说了上代码了

 1 #pragma once
 2 
 3 #include <windows.h>
 4 #include <iostream>
 5 using namespace std;
 6 #define BUFFER_SIZE 0x400
 7 typedef struct _UNICODE_STRING 
 8 {
 9     USHORT Length;
10     USHORT MaximumLength;
11     PWCHAR Buffer;
12 }UNICODE_STRING,*PUNICODE_STRING;
13 
14 
15 /************************************************************************/
16 /* 初始化                                                               */
17 /************************************************************************/
18 void Sub_1();
19 VOID
20 RtlInitUnicodeString(
21     OUT PUNICODE_STRING DestinationString,
22     IN PCWSTR SourceString OPTIONAL);//微软源代码
23 
24 
25 void Sub_2();
26 void Sub_3();
27 void Sub_4();   
28 VOID
29 RtlCopyUnicodeString(
30     OUT PUNICODE_STRING DestinationString,
31     IN PUNICODE_STRING SourceString OPTIONAL);//微软源代码
32 VOID
33 RtlFreeUnicodeString(
34     IN OUT PUNICODE_STRING UnicodeString);     //微软源代码
  1 #include "UnicodeString(User).h"
  2 int main()
  3 {
  4     Sub_1();
  5     Sub_2();
  6     Sub_3();
  7     Sub_4();
  8     printf("Input AnyKey To Exit\r\n");
  9     getchar();
 10 
 11     return 0;
 12 }
 13 
 14 void Sub_1()
 15 {
 16     //常量指针直接灌上去
 17     UNICODE_STRING v1;
 18     RtlInitUnicodeString(&v1, L"HelloWorld");
 19     printf("%Z\r\n", &v1);
 20 }
 21 //Windows源代码
 22 VOID 
 23 RtlInitUnicodeString(
 24     OUT PUNICODE_STRING DestinationString,
 25     IN PCWSTR SourceString OPTIONAL)
 26 {
 27     USHORT Length = 0;
 28      DestinationString->Length = 0;
 29      DestinationString->Buffer = (PWCHAR)SourceString;
 30      
 31      if (SourceString!=NULL)
 32      {
 33          while (*SourceString++)//源地址头
 34          {
 35              Length += sizeof(*SourceString);
 36              DestinationString->Length = Length;
 37              DestinationString->MaximumLength = Length + sizeof(UNICODE_NULL);
 38          }
 39      }
 40      else
 41      {
 42          DestinationString->Length = 0;
 43          DestinationString->MaximumLength = 0;
 44      }
 45 
 46 }
 47 void Sub_2()
 48 {
 49     //栈区内存通过缓存区灌上去
 50     UNICODE_STRING v1;
 51     WCHAR BufferData[] = L"HelloWorld";
 52     v1.Buffer = BufferData;
 53     v1.Length = wcslen(BufferData) * sizeof(WCHAR);
 54     v1.MaximumLength = (wcslen(BufferData) + 1) * sizeof(WCHAR);
 55     printf("%Z\r\n", &v1);
 56 }
 57 void Sub_3()
 58 {
 59     //堆区内存通过动态申请
 60     UNICODE_STRING v1;
 61     WCHAR BufferData[] = L"HelloWorld";
 62 
 63     v1.Length = wcslen(BufferData) * sizeof(WCHAR);
 64     v1.MaximumLength = (wcslen(BufferData) + 1) * sizeof(WCHAR);
 65     v1.Buffer = (WCHAR*)malloc(v1.MaximumLength);
 66     RtlZeroMemory(v1.Buffer, v1.MaximumLength);
 67     RtlCopyMemory(v1.Buffer, BufferData, v1.Length);
 68 
 69     printf("%Z\r\n", &v1);
 70     if (v1.Buffer != NULL)
 71     {
 72         free(v1.Buffer);
 73         v1.Buffer = NULL;
 74         v1.Length = v1.MaximumLength = 0;
 75     }
 76 }
 77 
 78 void Sub_4()
 79 {
 80 
 81     UNICODE_STRING SourceString;
 82     RtlInitUnicodeString(&SourceString, L"HelloWorld");
 83     UNICODE_STRING DestinationString = { 0 };
 84     
 85     DestinationString.Buffer = (PWSTR)malloc(BUFFER_SIZE);
 86     DestinationString.MaximumLength = BUFFER_SIZE;
 87     
 88     RtlCopyUnicodeString(&DestinationString, &SourceString);
 89     
 90     printf("SourceString:%wZ\n", &SourceString);
 91     printf("DestinationString:%wZ\n", &DestinationString);
 92     
 93     RtlFreeUnicodeString(&DestinationString);
 94 }
 95 
 96 
 97 VOID
 98 RtlCopyUnicodeString(
 99     OUT PUNICODE_STRING DestinationString,
100     IN PUNICODE_STRING SourceString OPTIONAL
101 )
102 {
103     WCHAR *v1, *v2;
104     ULONG SourceStringLength = 0;
105     if (SourceString != NULL)
106     {
107 
108         v1 = DestinationString->Buffer;
109         v2 = SourceString->Buffer;
110         SourceStringLength = SourceString->Length;
111         if ((USHORT)SourceStringLength > DestinationString->MaximumLength) //这个UHORT转换挺重要不然gg
112         {
113             SourceStringLength = DestinationString->MaximumLength;//一个是目标长度小于源目标
114         }
115         
116         DestinationString->Length = (USHORT)SourceStringLength;
117 
118         RtlCopyMemory(v1, v2, SourceStringLength);
119 
120         if (DestinationString->Length < DestinationString->MaximumLength)
121         {
122             v1[SourceStringLength / sizeof(WCHAR)] = UNICODE_NULL;//清空操作
123             //或者v2[SourceStringLength] = UNICODE_NULL;//清空操作
124         }
125     }
126     else
127     {
128         DestinationString->Length = 0;
129         DestinationString->MaximumLength = 0;
130     }
131     return;
132 }
133 
134 VOID
135 RtlFreeUnicodeString(
136     IN OUT PUNICODE_STRING UnicodeString
137     )
138 {
139 
140     if (UnicodeString->Buffer) 
141     {
142         free(UnicodeString->Buffer);
143         memset( UnicodeString, 0, sizeof( *UnicodeString ) );    
144     }
145 }

 

posted @ 2017-08-15 22:47  _Flame  阅读(486)  评论(0编辑  收藏  举报