views:

81

answers:

2

Hello,

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)?

+1  A: 

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.

Jeffrey L Whitledge
A: 

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().

Hans Passant