macro to initialize strings as null-terminated

Hi all,

I am currently using the macros

DECLARE_GLOBAL_CONST_UNICODE_STRING
and
DECLARE_GLOBAL_CONST_STRING

to initialize string literals as constants. Occasionally, I’d like to
use functions that take in a char* or wchar*, so I pass the buffer of
my UNICODE_STRING or STRING into the function. However, although it
seems that these buffers are null-terminated, I am not sure if this is
*guaranteed*.

Without having to create a copy of my strings and appending a
null-termination so that I can pass them into my functions, is there a
way, possibly using different macros, that I can just *initialize*
them automatically as null-terminated? FYI, the strings will never
change. They are just constants.

Thanks,
J

Generally, it is considered a Really Bad Idea to rely on NUL-terminated
strings. If you write something that expects them, and then two years
from now somebody calls them with a pointer to the buffer of a
UNICODE_STRING, you are cooked. So you should ALWAYS expect a
UNICODE_STRING, everywhere, no exceptions.

That said, the C standard requires that the compiler terminate all literal
strings by using the appropriate NUL character, which means U0000 for
Unicode strings. So you don’t need to worry about the constant strings
being terminated. What you really have to worry about is that your driver
will be maintained by unskilled labor, meaning the new hire, or yourself
two years from now. (Been there, done that…if I hadn’t left good
comments in the code I wrote years before, I would have been toast)

Since it takes almost zero effort to create a UNICODE_STRING, there’s no
excuse to not do it. The couple extra instructions won’t matter in the
Big Picture, because somebody is going to have to look at that string, and
the instructions required to create the UNICODE_STRING won’t matter. So
just use the RtlInitUnicodeString function.

I suspect that some clever coding of a macro might handle this:

#define DECLARE_CONST_UNICODE_STRING(name, value) \
const UNICODE_STRING name = { value, sizeof(value) - sizeof(WCHAR),
sizeof(value) - sizeof(WCHAR) }

I don’t know if sizeof(“abc”) is actually defined; if it is, it would be
4, and sizeof(L"abc") would be 8. Try it.

Also, I suspect that not all functions that take a UNICODE_STRING and not
modify it declare the parameter as a
const UNICODE_STRING whatever
which is fairly sloppy coding, but if this is a problem, you can’t put the
word ‘const’ in the macro definition I show above.

Experiment a bit; I’m only answering this because I have an hour of
downtime in what has become an amazingly hectic existence [hint: today
involved a visit to a funeral home, so if you wonder why I didn’t take the
time to figure out sizeof for a string literal, that might give a hint]

joe

Hi all,

I am currently using the macros

DECLARE_GLOBAL_CONST_UNICODE_STRING
and
DECLARE_GLOBAL_CONST_STRING

to initialize string literals as constants. Occasionally, I’d like to
use functions that take in a char* or wchar*, so I pass the buffer of
my UNICODE_STRING or STRING into the function. However, although it
seems that these buffers are null-terminated, I am not sure if this is
*guaranteed*.

Without having to create a copy of my strings and appending a
null-termination so that I can pass them into my functions, is there a
way, possibly using different macros, that I can just *initialize*
them automatically as null-terminated? FYI, the strings will never
change. They are just constants.

Thanks,
J


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Every string declared with these 2 functions must be a const lstring, as such, the compiler will null terminate. Since you have the literal init values already, define them with #define and then use that define for the declarations and then pass the same define for the functions expecting a null terminated string, not unicode_string.buffer (since you may change it in the future not to include the null since that is valid for that structure)

d

debt from my phone


From: JonathonS
Sent: 7/28/2012 1:26 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] macro to initialize strings as null-terminated

Hi all,

I am currently using the macros

DECLARE_GLOBAL_CONST_UNICODE_STRING
and
DECLARE_GLOBAL_CONST_STRING

to initialize string literals as constants. Occasionally, I’d like to
use functions that take in a char* or wchar*, so I pass the buffer of
my UNICODE_STRING or STRING into the function. However, although it
seems that these buffers are null-terminated, I am not sure if this is
*guaranteed*.

Without having to create a copy of my strings and appending a
null-termination so that I can pass them into my functions, is there a
way, possibly using different macros, that I can just *initialize*
them automatically as null-terminated? FYI, the strings will never
change. They are just constants.

Thanks,
J


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Thanks guys for the info. It is very helpful :slight_smile:

J

On Sat, Jul 28, 2012 at 2:14 PM, Doron Holan wrote:
> Every string declared with these 2 functions must be a const lstring, as
> such, the compiler will null terminate. Since you have the literal init
> values already, define them with #define and then use that define for the
> declarations and then pass the same define for the functions expecting a
> null terminated string, not unicode_string.buffer (since you may change it
> in the future not to include the null since that is valid for that
> structure)
>
> d
>
> debt from my phone
> ________________________________
> From: JonathonS
> Sent: 7/28/2012 1:26 PM
> To: Windows System Software Devs Interest List
> Subject: [ntdev] macro to initialize strings as null-terminated
>
> Hi all,
>
> I am currently using the macros
>
> DECLARE_GLOBAL_CONST_UNICODE_STRING
> and
> DECLARE_GLOBAL_CONST_STRING
>
> to initialize string literals as constants. Occasionally, I’d like to
> use functions that take in a char* or wchar*, so I pass the buffer of
> my UNICODE_STRING or STRING into the function. However, although it
> seems that these buffers are null-terminated, I am not sure if this is
> guaranteed.
>
> Without having to create a copy of my strings and appending a
> null-termination so that I can pass them into my functions, is there a
> way, possibly using different macros, that I can just initialize
> them automatically as null-terminated? FYI, the strings will never
> change. They are just constants.
>
> Thanks,
> J
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer