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

 #pragma once

 #include <windows.h>
#include <iostream>
using namespace std;
#define BUFFER_SIZE 0x400
typedef struct _UNICODE_STRING
{
USHORT Length;
USHORT MaximumLength;
PWCHAR Buffer;
}UNICODE_STRING,*PUNICODE_STRING; /************************************************************************/
/* 初始化 */
/************************************************************************/
void Sub_1();
VOID
RtlInitUnicodeString(
OUT PUNICODE_STRING DestinationString,
IN PCWSTR SourceString OPTIONAL);//微软源代码 void Sub_2();
void Sub_3();
void Sub_4();
VOID
RtlCopyUnicodeString(
OUT PUNICODE_STRING DestinationString,
IN PUNICODE_STRING SourceString OPTIONAL);//微软源代码
VOID
RtlFreeUnicodeString(
IN OUT PUNICODE_STRING UnicodeString); //微软源代码
 #include "UnicodeString(User).h"
int main()
{
Sub_1();
Sub_2();
Sub_3();
Sub_4();
printf("Input AnyKey To Exit\r\n");
getchar(); return ;
} void Sub_1()
{
//常量指针直接灌上去
UNICODE_STRING v1;
RtlInitUnicodeString(&v1, L"HelloWorld");
printf("%Z\r\n", &v1);
}
//Windows源代码
VOID
RtlInitUnicodeString(
OUT PUNICODE_STRING DestinationString,
IN PCWSTR SourceString OPTIONAL)
{
USHORT Length = ;
DestinationString->Length = ;
DestinationString->Buffer = (PWCHAR)SourceString; if (SourceString!=NULL)
{
while (*SourceString++)//源地址头
{
Length += sizeof(*SourceString);
DestinationString->Length = Length;
DestinationString->MaximumLength = Length + sizeof(UNICODE_NULL);
}
}
else
{
DestinationString->Length = ;
DestinationString->MaximumLength = ;
} }
void Sub_2()
{
//栈区内存通过缓存区灌上去
UNICODE_STRING v1;
WCHAR BufferData[] = L"HelloWorld";
v1.Buffer = BufferData;
v1.Length = wcslen(BufferData) * sizeof(WCHAR);
v1.MaximumLength = (wcslen(BufferData) + ) * sizeof(WCHAR);
printf("%Z\r\n", &v1);
}
void Sub_3()
{
//堆区内存通过动态申请
UNICODE_STRING v1;
WCHAR BufferData[] = L"HelloWorld"; v1.Length = wcslen(BufferData) * sizeof(WCHAR);
v1.MaximumLength = (wcslen(BufferData) + ) * sizeof(WCHAR);
v1.Buffer = (WCHAR*)malloc(v1.MaximumLength);
RtlZeroMemory(v1.Buffer, v1.MaximumLength);
RtlCopyMemory(v1.Buffer, BufferData, v1.Length); printf("%Z\r\n", &v1);
if (v1.Buffer != NULL)
{
free(v1.Buffer);
v1.Buffer = NULL;
v1.Length = v1.MaximumLength = ;
}
} void Sub_4()
{ UNICODE_STRING SourceString;
RtlInitUnicodeString(&SourceString, L"HelloWorld");
UNICODE_STRING DestinationString = { }; DestinationString.Buffer = (PWSTR)malloc(BUFFER_SIZE);
DestinationString.MaximumLength = BUFFER_SIZE; RtlCopyUnicodeString(&DestinationString, &SourceString); printf("SourceString:%wZ\n", &SourceString);
printf("DestinationString:%wZ\n", &DestinationString); RtlFreeUnicodeString(&DestinationString);
} VOID
RtlCopyUnicodeString(
OUT PUNICODE_STRING DestinationString,
IN PUNICODE_STRING SourceString OPTIONAL
)
{
WCHAR *v1, *v2;
ULONG SourceStringLength = ;
if (SourceString != NULL)
{ v1 = DestinationString->Buffer;
v2 = SourceString->Buffer;
SourceStringLength = SourceString->Length;
if ((USHORT)SourceStringLength > DestinationString->MaximumLength) //这个UHORT转换挺重要不然gg
{
SourceStringLength = DestinationString->MaximumLength;//一个是目标长度小于源目标
} DestinationString->Length = (USHORT)SourceStringLength; RtlCopyMemory(v1, v2, SourceStringLength); if (DestinationString->Length < DestinationString->MaximumLength)
{
v1[SourceStringLength / sizeof(WCHAR)] = UNICODE_NULL;//清空操作
//或者v2[SourceStringLength] = UNICODE_NULL;//清空操作
}
}
else
{
DestinationString->Length = ;
DestinationString->MaximumLength = ;
}
return;
} VOID
RtlFreeUnicodeString(
IN OUT PUNICODE_STRING UnicodeString
)
{ if (UnicodeString->Buffer)
{
free(UnicodeString->Buffer);
memset( UnicodeString, , sizeof( *UnicodeString ) );
}
}
05-11 01:28