需求很简单,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系列函数
![[图片]](https://i-blog.csdnimg.cn/blog_migrate/c422df6ee112bea9248aba9df847e467.png)
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));
}
本文探讨了在不同层面上将char数组转换为wchar数组的多种方法,包括使用RtlAnsiStringToUnicodeString、swprintf_s以及MSDN推荐的RtlStringCbPrintfW函数。这些方法对于跨字符集编程具有重要意义。
881

被折叠的 条评论
为什么被折叠?



