UnicodeString基本操作(Ring0)
// 纯粹做个记录,微软源码
1 #include "Unicode_String_Ring0.h" //bp Unicode_String_Ring0!DriverEntry
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegisterPath)
{
NTSTATUS Status = STATUS_SUCCESS;
PDEVICE_OBJECT DeviceObject = NULL; DriverObject->DriverUnload = DriverUnload; Test(); return Status;
} void Test()
{
//初始化
//StringInitTest(); //拷贝操作
//StringCopyTest(); //字符串比较
//StringCompareTest(); //字符串变大写
//StringToUpperTest(); //字符串与整型相互转化
//StringToIntegerTest(); //ANSI_STRING字符串与UNICODE_STRING字符串相互转换
StringConverTest(); } //初始化
void StringInitTest()
{
//Sub_1();//常量初始化
//Sub_2();
Sub_3();//堆
}
void Sub_1()
{
//UNICODE_STRING
//常量初始化
UNICODE_STRING v1;
RtlInitUnicodeString(&v1, L"HelloWorld"); //v1.Buffer = 常量指针
//v1.Length = 20
//v1.MaximumLength = 22 DbgPrint("%wZ\r\n", &v1);//Unicode打印L"" /*
//常量初始化ANSI_STRING
//(1)用RtlInitAnsiString初始化字符串
ANSI_STRING AnsiString;
CHAR * string = "hello";
//初始化ANSI_STRING字符串
RtlInitAnsiString(&AnsiString, string);
DbgPrint("AnsiString:%Z\n", &AnsiString);
*/
}
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); DbgPrint("%wZ\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 = ExAllocatePool(PagedPool, v1.MaximumLength); RtlZeroMemory(v1.Buffer, v1.MaximumLength);
RtlCopyMemory(v1.Buffer, BufferData, v1.Length); DbgPrint("%wZ\r\n", &v1); if (v1.Buffer != NULL)
{
ExFreePool(v1.Buffer);
v1.Buffer = NULL;
v1.Length = v1.MaximumLength = ;
}
} //拷贝操作
void StringCopyTest()
{
UNICODE_STRING SourceString;
RtlInitUnicodeString(&SourceString, L"HelloWorld"); UNICODE_STRING DestinationString = { };
DestinationString.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);
DestinationString.MaximumLength = BUFFER_SIZE; RtlCopyUnicodeString(&DestinationString, &SourceString); DbgPrint("SourceString:%wZ\r\n", &SourceString);
DbgPrint("DestinationString:%wZ\n", &DestinationString); RtlFreeUnicodeString(&DestinationString);
} //字符串比较
void StringCompareTest()
{
//初始化UnicodeString1
UNICODE_STRING UnicodeString1;
RtlInitUnicodeString(&UnicodeString1,L"HELLOWORLD"); //初始化UnicodeString2
UNICODE_STRING UnicodeString2;
//RtlInitUnicodeString(&UnicodeString2, L"Hello");
//RtlInitUnicodeString(&UnicodeString2, L"HELLOWORLD");
RtlInitUnicodeString(&UnicodeString2, L"helloworld"); if (RtlEqualUnicodeString(
&UnicodeString1,
&UnicodeString2,
TRUE
//If TRUE,
//case should be ignored when doing the comparison.
)
)
{
DbgPrint("UnicodeString1 and UnicodeString2 are equal\n");
}
else
{
DbgPrint("UnicodeString1 and UnicodeString2 are NOT equal\n");
} } //字符串变大写
void StringToUpperTest()
{
UNICODE_STRING SourceString;
RtlInitUnicodeString(&SourceString, L"Hello World"); UNICODE_STRING DestinationString;
DestinationString.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);
DestinationString.MaximumLength = BUFFER_SIZE; //变化前
DbgPrint("变化前:%wZ\n", &SourceString);
//变大写
RtlUpcaseUnicodeString(
&DestinationString, //DestinationString
&SourceString, //SourceString
FALSE//Specifies if RtlUpcaseUnicodeString is to allocate the buffer space for the DestinationString.
//If it does, the buffer must be deallocated by calling RtlFreeUnicodeString.
); //变化后
DbgPrint("变化后:%wZ\n", &DestinationString); RtlFreeUnicodeString(&DestinationString);
} //字符串与整型相互转化
void StringToIntegerTest()
{
//(1)字符串转换成数字
UNICODE_STRING UnicodeString1;
RtlInitUnicodeString(&UnicodeString1, L"-100"); ULONG lNumber;
NTSTATUS Status =
RtlUnicodeStringToInteger(//第二个参数Base
&UnicodeString1,
//10,//-100是10进制 //输出-100
//16,//-100是16进制 //输出-256
, //-100是8进制 //输出-64
&lNumber
); if (NT_SUCCESS(Status))
{
DbgPrint("Conver to integer succussfully!\n");
DbgPrint("Result:%d\n", lNumber);
}
else
{
DbgPrint("Conver to integer unsuccessfully!\n");
}
//(2)数字转换成字符串
UNICODE_STRING UnicodeString2 = { };
UnicodeString2.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);
UnicodeString2.MaximumLength = BUFFER_SIZE; Status = RtlIntegerToUnicodeString(//同上 第二参数是Base
,
//10, //输出200
//8, //输出310
, //输出 C8
&UnicodeString2
); /*
HEX C8
DEC 200
OCT 310
*/ if (NT_SUCCESS(Status))
{
DbgPrint("Conver to string succussfully!\n");
DbgPrint("Result:%wZ\n", &UnicodeString2);
}
else
{
DbgPrint("Conver to string unsuccessfully!\n");
} //销毁UnicodeString2
//注意!!UnicodeString1不用销毁
RtlFreeUnicodeString(&UnicodeString2); } //ANSI_STRING字符串与UNICODE_STRING字符串相互
void StringConverTest()
{
//(1)将UNICODE_STRING字符串转换成ANSI_STRING字符串
//初始化UnicodeString1
UNICODE_STRING UnicodeString1;
RtlInitUnicodeString(&UnicodeString1, L"HelloWorld"); ANSI_STRING AnsiString1;
NTSTATUS Status = RtlUnicodeStringToAnsiString(
&AnsiString1,
&UnicodeString1,
TRUE
//TRUE if this routine is to allocate the buffer space for the DestinationString.
//If it does, the buffer must be deallocated by calling RtlFreeAnsiString.
); if (NT_SUCCESS(Status))
{
DbgPrint("Conver succussfully!\n");
DbgPrint("Result:%Z\n", &AnsiString1);
}
else
{
DbgPrint("Conver unsuccessfully!\n");
} //销毁AnsiString1
RtlFreeAnsiString(&AnsiString1); //(2)将ANSI_STRING字符串转换成UNICODE_STRING字符串 ANSI_STRING AnsiString2;
RtlInitString(&AnsiString2, "HelloWorld"); UNICODE_STRING UnicodeString2;
Status = RtlAnsiStringToUnicodeString(
&UnicodeString2,
&AnsiString2,
TRUE
//Specifies if this routine should allocate the buffer space for the destination string.
//If it does, the caller must deallocate the buffer by calling RtlFreeUnicodeString. ); if (NT_SUCCESS(Status))
{
DbgPrint("Conver succussfully!\n");
DbgPrint("Result:%wZ\n", &UnicodeString2);
}
else
{
DbgPrint("Conver unsuccessfully!\n");
} //销毁UnicodeString2
RtlFreeUnicodeString(&UnicodeString2);
} VOID DriverUnload(PDRIVER_OBJECT DriverObject)
{
DbgPrint("DriverUnload()\r\n");
}
#include <ntifs.h> #define BUFFER_SIZE 0x400 void Test(); //初始化操作
void StringInitTest();
void Sub_1();//常量初始化
void Sub_2();
void Sub_3(); //拷贝操作
void StringCopyTest(); //字符串比较
void StringCompareTest(); //字符串变大写
void StringToUpperTest(); //字符串与整型相互转化
void StringToIntegerTest(); //ANSI_STRING字符串与UNICODE_STRING字符串相互
void StringConverTest(); VOID DriverUnload(PDRIVER_OBJECT DriverObject);
Ring3层的话不能直接使用UnicodeString、AnsiString,需要自己定义出来,并且部分相关的函数需要自己实现,得参照微软的源代码,本人正在写,写好了发出来,也是Ring0的这些基本的操作。
老实说字符串操作是很大的一个部分,之前就被一个String的输出到txt文件的问题卡过,还是要好好总结这些基础的东西,感觉要是能有完全的手册就好了MSDN。。。String Manipulation (CRT)。。。。唉看英语还是慢,个人琐碎。
vs自动生成的ReadMe.txt是UTF-8 开头有EF BB BF
UnicodeString基本操作(Ring0)的更多相关文章
- UnicodeString基本操作(Ring3)
// Unicode_String_Ring3.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "Unicode ...
- Key/Value之王Memcached初探:二、Memcached在.Net中的基本操作
一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅仅memcached网站上列出的语言就有: ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
- 三、Redis基本操作——List
小喵的唠叨话:前面我们介绍了Redis的string的数据结构的原理和操作.当时我们提到Redis的键值对不仅仅是字符串.而这次我们就要介绍Redis的第二个数据结构了,List(链表).由于List ...
- 二、Redis基本操作——String(实战篇)
小喵万万没想到,上一篇博客,居然已经被阅读600次了!!!让小喵感觉压力颇大.万一有写错的地方,岂不是会误导很多筒子们.所以,恳请大家,如果看到小喵的博客有什么不对的地方,请尽快指正!谢谢! 小喵的唠 ...
- 一、Redis基本操作——String(原理篇)
小喵的唠叨话:最近京东图书大减价,小喵手痒了就买了本<Redis设计与实现>[1]来看看.这里权当小喵看书的笔记啦.这一系列的模式,主要是先介绍Redis的实现原理(可能很大一部分会直接照 ...
- Linq查询基本操作
摘要:本文介绍Linq查询基本操作(查询关键字) - from 子句 - where 子句 - select子句 - group 子句 - into 子句 - orderby 子句 - join 子句 ...
- C++ map的基本操作和使用
原文地址:http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可 ...
随机推荐
- 20165313 《Java程序设计》第九周学习总结
教材学习总结 1.URL类 :ava.net包中的URL类是对统一资源定位符的抽象,使用URL创建对象的应用程序称作客户端程序,客户端程序的URL对象调用InputStream openStream( ...
- 把目录C:\Python34\PCI_Code\chapter2\加到系统路径中
>>> import sys >>> sys.path.append("C:\Python34\PCI_Code\chapter2")
- CH4907 作诗
题意 4907 作诗 0x49「数据结构进阶」练习 描述 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗之后还要虐OI,于是SHY ...
- 一篇文章看懂java反射机制(反射实例化对象-反射获得构造方法,获得普通方法,获得字段属性)
Class<?> cls = Class.forName("cn.mldn.demo.Person"); // 取得Class对象传入一个包名+类名的字符串就可以得到C ...
- dup等复制文件描述符函数
[root@bogon code]# cat b.c #include<stdio.h> #include<error.h> #include<unistd.h> ...
- Cassandra如何保证数据最终一致性
Cassandra如何保证数据最终一致性:1.逆熵机制(Anti-Entropy)使用默克尔树(Merkle Tree)来确认多个副本数据一致,对于不一致数据,根据时间戳来获取最新数据. 2.读修复机 ...
- showdoc 开源在线api&&技术文档管理工具
showdoc 是一个很不错的api 以及技术文档管理工具 环境准备 doker-copose 文件 version: "3" services: doc: image: regi ...
- C#遍历菜单项
(1)横向遍历 ToolStripMenuItem foreach (ToolStripMenuItem con in this.MainMenuStrip.Items) { ...
- MQ介绍 & 实例
阅读目录 定义 优秀MQ特点 产品比较 实例(简单的实战) 关于消息队列与分布式的那些事 定义: 消息队列(MQ)是一种应用程序对应用程序的通信方法,应用程序通过队列进行通信,而不是通过直接调用彼此来 ...
- WPF 控件总结
内容控件:1.Button:当Button.IsCancel="True"时,点击按钮,对话框关闭.当Button.IsDefault="True",按回车触发 ...