Hi,
This may be a dumb question, but I am new to driver development.
Does the user have to allocate the memory for the destination string while
calling RtlInitAnsiString(). If it is done automatically, then do you have
to call some function to free it?
Thanks
Mayank
Just a general rule of thumb …
If the RtlXxxx function has a TRUE/FALSE regarding memory allocation, the
function will allocate the memory, and you then have to free it. Only one
function has that parameter, so generally speaking you do you own memory
management for Unicode and ansii strings.
–
Gary G. Little
xxxxx@broadstor.com
xxxxx@inland.net
“Mayank Ajmera” wrote in message news:xxxxx@ntdev…
>
> Hi,
> This may be a dumb question, but I am new to driver development.
> Does the user have to allocate the memory for the destination string while
> calling RtlInitAnsiString(). If it is done automatically, then do you have
> to call some function to free it?
>
> Thanks
> Mayank
>
>
It’s not dumb. The RtlxxxString functions make sense once you’ve used them
for a while, but they are definitely not the same as the standard string
handling routines used to manage char* strings.
As Gary Little said in another message, then answer is yes, you own
allocation for this particular routine, there is not automatic allocation,
and there is consequently no matching RtlFreeXXXString to call.
The longer answer is that this is *not* a strcpy operation. All
RtlInitXXXString does is set the length member to the number of *bytes*
taken up by the string, and sets the maximum length member to the number of
bytes taken up by the string plus the trailing null, then sets the buffer
pointer to the address of the string. So it has simply wrapped the
XXX_STRING struct around the string you passed it.
Just be aware, most RtlXXXString routines are not this benign.
Hope this is helpful.
Phil
“Mayank Ajmera” @lists.osr.com on 02/28/2002 07:28:52
PM
Please respond to “NT Developers Interest List”
Sent by: xxxxx@lists.osr.com
To: “NT Developers Interest List”
cc:
Subject: [ntdev] Question about RtlInitAnsiString()
Hi,
This may be a dumb question, but I am new to driver development.
Does the user have to allocate the memory for the destination string while
calling RtlInitAnsiString(). If it is done automatically, then do you have
to call some function to free it?
Thanks
Mayank
—
You are currently subscribed to ntdev as: xxxxx@seagate.com
To unsubscribe send a blank email to %%email.unsub%%
At 03.28 01/03/2002, you wrote:
This may be a dumb question, but I am new to driver development.
Does the user have to allocate the memory for the destination string while
calling RtlInitAnsiString(). If it is done automatically, then do you have
to call some function to free it?
The source string will be used as buffer, no buffer will be allocated, the
function will merely calculate the Length and MaximumLength fields
accordingly. From ReactOS (http:</http:> - check it out, it’s
really worth it) implementation of NTDLL.DLL:
VOID
STDCALL
RtlInitAnsiString (
IN OUT PANSI_STRING DestinationString,
IN PCSZ SourceString
)
{
ULONG DestSize;
if(SourceString==NULL)
{
DestinationString->Length = 0;
DestinationString->MaximumLength = 0;
}
else
{
DestSize = strlen ((const char *)SourceString);
DestinationString->Length = DestSize;
DestinationString->MaximumLength = DestSize + 1;
}
DestinationString->Buffer = (PCHAR)SourceString;
}
This means that if you pass a string literal as source string, you must not
pass the obtained ANSI_STRING as an OUT parameter, or you’ll get an access
violation fault, because almost all compilers will put string literals in a
read-only data section