驱动第一章字符串
来源:互联网 发布:中国手机网络发展现状 编辑:程序博客网 时间:2024/05/18 18:16
使用字符串结构
传统C语言总定义和使用字符串:
ansi和unicode
ansi
unicode
char *str={"my first string"}; //ansi字符串定义
wchar_t *wstr={L"my first string"}; //unicode字符串定义
//求长度
size_t len = strlen(str); //ansi
size_t wlen = wcslen(wstr); //unicode
//print
printf("%s %ws %d %d",str,wstr,len,wlen);
1 字符串定义
驱动中常用的字符串结构
typedef struct _UNICODE_STRING{
USHORT Length;//字符串长度
USHORT MaximumLength;//缓冲区长度
PWSTR Buffer;//缓冲区
}UNICODE_STRING,*PUNICODE_STRING;
typedef struct _ANSI_STRING{
USHORT Length; //字符串长度
USHOTT MaximumLength;//缓冲区长度
PSTR Buffer;//缓冲区
}ANSI_STRING,*PANSI_STRING;
对UNICODE_STRING的赋值:
UNICODE_STRING str={
sizeof(L"my first string")-sizeof(L"my first string")[0],
sizeof(L"my first string"),
L("my first string")};
在头文件ntdef.h中有一个宏RTL_CONSTANT_STRING,使用这个宏后,我们就可以简单的
定义一个常数字符串如下:
UNICODE_STRING str=RTL_CONSTANT_STRING(L"my first string");
UNICODE_STRING str;
RtlInitUnicodeString(&str,L"my first string");
2 字符串的拷贝
UNICODE_STRING dst;//目标字符串
WCHAR dst_buf[256];
UNICODE_STRING src = RTL_CONSTANT_STRING(L"my source string!");
//分配缓冲区
RtlInitEmptyString(dst,dst_buf,256*sizeof(WCHAR)); //
RtlCopyUnicodeString(&dst,&src); //字符串拷贝
3 字符串的连接
NTSTATUS status;
UNICODE_STRING dst; //目标字符串
WCHAR dst_buf[256];//定义缓冲区
UNICODE_STRING src = RTL_CONSTANT_STRING(L"My source string!");//定义
//初始化目标字符串
RtlInitEmptyString(dst,dst_buf,256*sizeof(WCHAR));
RtlCopyUnicodeString(&dst,&src); //字符串拷贝
status = RtlAppendUnicodeToString(
&dst,
L"my second string!");
if(status != STATUS_SUCCESS)
{
... ...
}
4 字符串的打印
1)字符串和数字的集合
sprintf(),swprintf()不安全.微软推荐RtlStringCbPrintW
// 调用RtlStringCbPrintfW来进行打印
status = RtlStringCbPrintfW(
dst->Buffer,L”file path = %wZ file size = %d /r/n”,
&file_path,file_size);
// 这里调用wcslen没问题,这是因为RtlStringCbPrintfW打印的
// 字符串是以空结束的。
dst->Length = wcslen(dst->Buffer) * sizeof(WCHAR);
2)输出打印
DbgPrint() 打印调试信息
- 驱动第一章字符串
- AGP驱动第一章第一节
- 第一章 设备驱动概述
- 驱动第一章:hello world
- 第一章 s32v234摄像头驱动
- 第一章、左旋转字符串
- 第一章、左旋转字符串
- 第一章、左旋转字符串
- 第一章:左旋转字符串
- 第一章、基础知识(Part2)--字符串
- 第一章 原始字符串, 模块
- python第一章-字符串
- 【笔记】Linux驱动学习第一章
- 第一章 Linux设备驱动概述
- 第一章 驱动开发_驱动开发基本知识
- 第一章 UVC驱动之虚拟视频驱动
- 第一章 字符串转换成整数
- 第一章_左旋转字符串
- 对我的代码评审总结
- Story
- 设计模式之策略模式
- 多堆NIM问题
- 在程序中使用3D Vision--转帖
- 驱动第一章字符串
- 将图片 按比例 压缩为JPG
- erlang——timer
- vim 操作 总结
- 初识苹果操作系统
- Facebook的新实时消息系统:HBase
- linux中C/C++获得微秒级时间
- MyEclipse 8.6 安装 SVN 插件
- Linux下共享库