驱动第一章字符串
使用字符串結構
傳統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() 打印調試信息
總結
- 上一篇: oledb 获取所有表的名字和列名
- 下一篇: 如何使用live writer客户端来发