EXTERN_C NTSTATUS usStringTest() /*字符串操作*/
{
//字符串初始化方式
//第一种
DECLARE_CONST_UNICODE_STRING(usStr1, L"第1个");
DbgPrintEx(DPFLTR_IHVDRIVER_ID, 0, "usStr1:%wZ", usStr1);
//第二种
UNICODE_STRING usStr2;
RtlInitUnicodeString(&usStr2, L"u第2个");
DbgPrintEx(DPFLTR_IHVDRIVER_ID, 0, "usStr2:%wZ", usStr2);
ANSI_STRING asStr2;
RtlInitAnsiString(&asStr2, "a第2个");
DbgPrintEx(DPFLTR_IHVDRIVER_ID, 0, "asStr2:%Z", asStr2);
//第三种
UNICODE_STRING usStr3;
wchar_t wcStr[] = L"u第3个";
usStr3.Length =(USHORT) wcslen(wcStr) * sizeof(wchar_t);
usStr3.MaximumLength = usStr3.Length;
usStr3.Buffer = wcStr;
DbgPrintEx(DPFLTR_IHVDRIVER_ID, 0, "usStr3:%wZ", usStr3);
ANSI_STRING asStr3;
CHAR acStr[] = "a第3个";
asStr3.Buffer = acStr;
asStr3.Length = (USHORT)strlen(acStr) * sizeof(CHAR);
asStr3.MaximumLength = asStr3.Length;
DbgPrintEx(DPFLTR_IHVDRIVER_ID, 0, "asStr3:%Z", asStr3);
//第四种
UNICODE_STRING usStr4 = RTL_CONSTANT_STRING(L"u第4个");
DbgPrintEx(DPFLTR_IHVDRIVER_ID, 0, "usStr4:%wZ", usStr4);
ANSI_STRING acStr4 = RTL_CONSTANT_STRING("a第4个");
DbgPrintEx(DPFLTR_IHVDRIVER_ID, 0, "acStr4:%Z", acStr4);
//第五种
UNICODE_STRING usStr5 = { 0 };
size_t ulength = wcslen(L"u第5个") * sizeof(WCHAR);
usStr5.Buffer = (PWCH)ExAllocatePoolWithTag(NonPagedPool, ulength, 'JAXX');
if (usStr5.Buffer == NULL)
{
return STATUS_UNSUCCESSFUL;
}
RtlZeroMemory(usStr5.Buffer, ulength);
RtlCopyMemory(usStr5.Buffer, L"u第5个", ulength);
usStr5.Length = (USHORT)ulength;
usStr5.MaximumLength = (USHORT)ulength;
DbgPrintEx(DPFLTR_IHVDRIVER_ID, 0, "%wZ", usStr5);
ExFreePoolWithTag(usStr5.Buffer, 'JAXX');
ANSI_STRING acStr5 = { 0 };
ulength = strlen("a第5个") * sizeof(CHAR);
acStr5.Buffer = (PCHAR)ExAllocatePoolWithTag(NonPagedPool, ulength, 'JAXb');
if (acStr5.Buffer == NULL)
{
return STATUS_UNSUCCESSFUL;
}
RtlZeroMemory(acStr5.Buffer, ulength);
RtlCopyMemory(acStr5.Buffer, "a第5个", ulength);
acStr5.Length = (USHORT)ulength;
acStr5.MaximumLength = (USHORT)ulength;
DbgPrintEx(DPFLTR_IHVDRIVER_ID, 0, "%Z", acStr5);
ExFreePoolWithTag(acStr5.Buffer, 'JAXb');
//复制字符串
WCHAR wsStr6[100] = { 0 };
UNICODE_STRING usStr6 = { 0 };
RtlInitEmptyUnicodeString(&usStr6, wsStr6, sizeof(wsStr6)); //---
RtlCompareUnicodeString(&usStr6, &usStr2, FALSE);
CHAR csStr6[100] = { 0 };
ANSI_STRING asStr6 = { 0 };
RtlInitEmptyAnsiString(&asStr6, csStr6, sizeof(csStr6));
RtlCompareString(&asStr6, &asStr2, FALSE);
//字符串转换为整数
UNICODE_STRING usStr7 = RTL_CONSTANT_STRING(L"890");
ULONG uVale = 0;
RtlUnicodeStringToInteger(&usStr7, 10, &uVale);
//数字转换为字符串
WCHAR wsStr8[100] = { 0 };
UNICODE_STRING usStr8 = { 0 };
RtlInitEmptyUnicodeString(&usStr8, wsStr8, sizeof(wsStr8));
RtlIntegerToUnicodeString(520, 10, &usStr8);
//Unicode与Ansi互换
CHAR csStr9[100] = { 0 };
ANSI_STRING asStr9 = { 0 };
UNICODE_STRING usStr10 = RTL_CONSTANT_STRING(L"Uncode字符串");
RtlInitEmptyAnsiString(&asStr9, csStr9, sizeof(csStr9));
RtlUnicodeStringToAnsiString(&asStr9, &usStr10, false);
DbgPrintEx(DPFLTR_IHVDRIVER_ID, 0, "asStr9:%Z", asStr9);
WCHAR wsStr9[100] = { 0 };
UNICODE_STRING usStr9 = { 0 };
ANSI_STRING asStr10 = RTL_CONSTANT_STRING("Ansi字符串");
RtlInitEmptyUnicodeString(&usStr9, wsStr9, sizeof(wsStr9));
RtlAnsiStringToUnicodeString(&usStr9, &asStr10, false);
DbgPrintEx(DPFLTR_IHVDRIVER_ID, 0, "usStr9:%wZ", usStr9);
//比较
return STATUS_SUCCESS;
}
格式化字符查阅链接:
https://learn.microsoft.com/zh-cn/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions?view=msvc-170