今天写驱动用到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 ) );
}
}

最新文章

  1. 【WCF】使用“用户名/密码”验证的合理方法
  2. sqlite3的图片的(二进制数据)存取操作
  3. jQuery瀑布流简单示例
  4. ASP.NET MVC增删改查
  5. 点击播放js
  6. windows 10家庭版升级到专业版
  7. css应用四
  8. 开源Math.NET基础数学类库使用(12)C#随机数扩展方法
  9. Cannot read property &#39;component&#39; of undefined 即vue-router 0.x转化为2.x
  10. Android 框架炼成 教你如何写组件间通信框架EventBus
  11. &lt;Android基础&gt;(三) UI开发 Part 3 RecyclerView
  12. MySql concat与字符转义
  13. SSH框架搭建过程详解
  14. Spring入门教程:通过MyEclipse开发第一个Spring项目
  15. [转]SpringMVC单文件上传、多文件上传、文件列表显示、文件下载
  16. mac os 切换网络优先级
  17. VS2013 C++ 动态链接库的生成
  18. Exercise02_07
  19. linux如何启动/停止/重启MySQL
  20. SQL子查询/嵌套查询

热门文章

  1. noip普及组2004 FBI树
  2. JavaScript模块化 --- Commonjs、AMD、CMD、es6 modules
  3. Json-Server模拟数据接口开发
  4. Sublime Text前端开发工具介绍
  5. 写给后端的前端笔记:浮动(float)布局
  6. 关于i++和++i
  7. 单表ORM框架
  8. 运行mvn install时跳过Test
  9. Springmvc_validation 效验器
  10. Entity Framework Core 2.0 数据库迁移