开发者

Converting a UNICODE_STRING to ANSI or vice versa in C

开发者 https://www.devze.com 2023-01-11 13:55 出处:网络
I have a UNICODE_STRING that I would like to compare to a null-terminated ANSI string to check if they are the same.I\'m using C.I would like to avoid including winternl.h for RtlInitUnicodeString.

I have a UNICODE_STRING that I would like to compare to a null-terminated ANSI string to check if they are the same. I'm using C. I would like to avoid including winternl.h for RtlInitUnicodeString.

What is the preferred method doing this?

Or, alternatively, is there any problem with me using MultiByteToWideChar() to convert the ANSI string to a wide-character representation and then开发者_如何学运维 comparing that to the UNICODE_STRING.buffer (with the understanding that the buffer might not be null-terminated)?


WideCharToMultiByte seems the more logical route. It can handle strings that aren't zero-terminated and produces a terminated one. And it tries to do something meaningful with codepoints that don't have a character in the system code page. Then just strcmp().


I would just convert the ANSI string using MultiByteToWideChar(). The CompareString() function takes length parameters for each string, so no worries about the missing null-terminator.

Just be careful about which parameters take or return bytes verses characters, and there should be no problems using these functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消