For an uninitialized UNICODE_STRING, is it necessary to call
RtlInitUnicodeString before calling a function like RtlStringFromGUID
(or do functions like that unconditionally overwrite the previous
contents of the structure)?

For an initialized UNICODE_STRING, is it necessary to call
RtlFreeUnicodeString before calling a function like RtlStringFromGUID
(or do functions like that attempt to free the previously allocated
buffer before overwriting the structure)?

(Basically, are the conventions for UNICODE_STRING's and ANSI_STRING's
similar to the conventions for BSTR's in COM?)

Re: RtlInitUnicodeString and RtlFreeUnicodeString conventions by cristalink

cristalink
Mon Nov 13 14:13:35 CST 2006

> (Basically, are the conventions for UNICODE_STRING's and ANSI_STRING's
> similar to the conventions for BSTR's in COM?)

It's all quite inconsistent in kernel mode. You need to read the
documentation on each API method very carefully .

RtlStringFromGUID says it allocates the buffer. It does not say it
deallocates the original buffer first. Therefore, it overwrites all the
members of UNICODE_STRING. Just pass an uninitialized UNICODE_STRING:

UNICODE_STRING ustr;
RtlStringFromGUID( guid, &ustr );
...
RtlFreeUnicodeString( &ustr );


> For an initialized UNICODE_STRING, is it necessary to call
> RtlFreeUnicodeString before calling a function like RtlStringFromGUID
> (or do functions like that attempt to free the previously allocated
> buffer before overwriting the structure)?

Your question is not correct. "Initialized" merely means all the members of
UNICODE_STRING are valid. It doesn't mean the buffer should be freed before
reusing the structure. RtlInitUnicodeString does not allocate memory, it
merely sets the members of UNICODE_STRING.

If you have a UNICODE_STRING that was allocated by RtlStringFromGUID or
similar, you have to call RtlFreeUnicodeString before reusing the structure
or when it's no longer needed.

If you have a UNICODE_STRING that points to a buffer allocated with
ExAllocatePool, you need to free that buffer with ExFreePool.

If you have a UNICODE_STRING that points to a buffer on the stack, you don't
need to do anything.

--
http://www.cristalink.com



<BubbaGump> wrote in message
news:a6fhl2dirptgfu6cuf07usukkcjghinv24@4ax.com...
> For an uninitialized UNICODE_STRING, is it necessary to call
> RtlInitUnicodeString before calling a function like RtlStringFromGUID
> (or do functions like that unconditionally overwrite the previous
> contents of the structure)?
>
> For an initialized UNICODE_STRING, is it necessary to call
> RtlFreeUnicodeString before calling a function like RtlStringFromGUID
> (or do functions like that attempt to free the previously allocated
> buffer before overwriting the structure)?
>
> (Basically, are the conventions for UNICODE_STRING's and ANSI_STRING's
> similar to the conventions for BSTR's in COM?)
>