Semantics of UNICODE_STRING datatype ...

Hello,

I don’t quite understand the “assignment/copy semantics” of UNICODE_STRING. Is it sufficient to do

UNICODE_STRING x, y;

x = y;

I.e. can I do shallow copy? Or must I do deep copy by allocating memory and copying from the source to the destination?

Thanks, Vasili


Do you Yahoo!?
Yahoo! Tax Center - File online, calculators, forms, and more

This will definitely work if you simply use the shallow copy. However, the
two structures will point to the same memory string buffer i.e. if you free
one string, the other one will contain a pointer to a freed buffer.

RtlCopyUnicodeString is a function you might be interested in.

Mat

-----Original Message-----
From: Galchin Vasili [mailto:xxxxx@yahoo.com]
Sent: Wednesday, April 09, 2003 9:58 PM
To: NT Developers Interest List
Subject: [ntdev] Semantics of UNICODE_STRING datatype …

Hello,
I don’t quite understand the “assignment/copy semantics” of
UNICODE_STRING. Is it sufficient to do
UNICODE_STRING x, y;
x = y;
I.e. can I do shallow copy? Or must I do deep copy by allocating memory and
copying from the source to the destination?
Thanks, Vasili


Do you Yahoo!?
Yahoo! http: Tax
Center - File online, calculators, forms, and more — You are currently
subscribed to ntdev as: xxxxx@guillemot.com To unsubscribe send a blank
email to xxxxx@lists.osr.com</http:>

Both variants are possible. Do not forget to call RtlFreeUnicodeString in the second case.

----- Original Message -----
From: Galchin Vasili
To: NT Developers Interest List
Sent: Thursday, April 10, 2003 5:58 AM
Subject: [ntdev] Semantics of UNICODE_STRING datatype …

Hello,

I don’t quite understand the “assignment/copy semantics” of UNICODE_STRING. Is it sufficient to do

UNICODE_STRING x, y;

x = y;

I.e. can I do shallow copy? Or must I do deep copy by allocating memory and copying from the source to the destination?

Thanks, Vasili


Do you Yahoo!?
Yahoo! Tax Center - File online, calculators, forms, and more — You are currently subscribed to ntdev as: xxxxx@storagecraft.com To unsubscribe send a blank email to xxxxx@lists.osr.com

Take a look at NTSTRSAFE header file in the latest DDK. You may find a safer way of handing those pesky UNICODE strings.


Gary G. Little
Have Computer, Will Travel …
909-698-3191
909-551-2105
http://www.wd-3.com
“Maxim S. Shatskih” wrote in message news:xxxxx@ntdev…
Both variants are possible. Do not forget to call RtlFreeUnicodeString in the second case.

----- Original Message -----
From: Galchin Vasili
To: NT Developers Interest List
Sent: Thursday, April 10, 2003 5:58 AM
Subject: [ntdev] Semantics of UNICODE_STRING datatype …

Hello,

I don’t quite understand the “assignment/copy semantics” of UNICODE_STRING. Is it sufficient to do

UNICODE_STRING x, y;

x = y;

I.e. can I do shallow copy? Or must I do deep copy by allocating memory and copying from the source to the destination?

Thanks, Vasili

----------------------------------------------------------------------------
Do you Yahoo!?
Yahoo! Tax Center - File online, calculators, forms, and more — You are currently subscribed to ntdev as: xxxxx@storagecraft.com To unsubscribe send a blank email to xxxxx@lists.osr.com

Galchin Vasili wrote:

I don’t quite understand the “assignment/copy semantics” of
UNICODE_STRING. Is it sufficient to do

UNICODE_STRING x, y;

x = y;

I think some of the answers you’ve gotten are misleading. The
UNICODE_STRING structure points to a separate memory area (Buffer) that
contains the string data. Doing “x = y” is like copying one char* to
another – the new value points to the same data, but someone who
releases the memory invalidates both pointers.

If you truly want a copy of the data, you can allocate memory to hold
the target, as in this example:

x.Buffer = (PWCHAR) ExAllocatePool(PagedPool, y.Length);
x.MaximumLength = y.Length;
RtlCopyUnicodeString(&x, &y);

The call to RtlCopyUnicodeString copies min(x.MaximumLength, y.Length)
bytes from y.Buffer to x.Buffer, setting x.Length to the number of bytes
actually copied. In this example, that would be the whole source string.

Note that the string data is not null terminated. In general,
UNICODE_STRING structures don’t point to null-terminated strings.
There’s no need for a null terminator since the UNICODE_STRING structure
contains the length.


Walter Oney, Consulting and Training
Basic and Advanced Driver Programming Seminars
Now teaming with John Hyde for USB Device Engineering Seminars
Check out our schedule at http://www.oneysoft.com