天天看点

windows kernel char数组转wchar数组

需求很简单,char数组转wchar数组,

原来的做法:

UNICODE_STRING usFilePath = { 0 };
  ANSI_STRING    asFilePath = { 0 };
 RtlInitAnsiString(&asFilePath, pFilePath);
 RtlAnsiStringToUnicodeString(&usFilePath, &asFilePath, TRUE);
// TODO
usFilePath.Buffer

 RtlFreeUnicodeString(&usFilePath);
           

应用层char数组转wchar数组的方法还是挺多的,但是内核层在网上竟然找不到几个靠谱的方法,没办法,我太菜了

后来使用swprintf_s加%hs格式化字符串:

char* pFilePath = "c:\\1.exe";
wchar_t ws[260] = { 0 };
swprintf_s(ws, 260, L"%hs", pFilePath);
           

最后在msdn上发现官方推荐使用RtlStringCbPrintfW来代替swprintf系列函数

windows kernel char数组转wchar数组
char* str1 = "this is an ansi string";
	WCHAR str2[260] = { 0 };
	status = RtlStringCbPrintfW(str2, 260, L"this is an unicode string from %hs", str1);
	if (!NT_SUCCESS(status))
	{
		KdPrint(("convert ansi to wchar failed.\n status = %x", status));
	}
           

继续阅读