RtlUnicodeStringToAnsiString problem

I am struggling with RtlUnicodeStringToAnsiString function. This is my code:

// pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
ANSI_STRING ansi_string;
DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);

RtlInitAnsiString(&ansi_string, NULL);
nLength = ansi_string.Length;
DbgPrint(“ANSI string length before conversion: %d\n”, nLength);

RtlUnicodeStringToAnsiString(&ansi_string, &pDevExt->ustrDeviceName.uStr, FALSE);
nLength = ansi_string.Length;
DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
DbgPrint(“ANSI string after conversion:: %s\n”, ansi_string.Buffer);

Debug output is:
Source: \Device\LOOPBACK0
ANSI string length before conversion: 0
ANSI string length after conversion: 17
ANSI string after conversion:: (null)
Destination: (null)

What is wrong in this code? ansi_string length is OK, but buffer is empty.


Do you Yahoo!?
Y! Messenger - Communicate in real time. Download now.

So I’m guessing that RUSTAS returns an error, right? Like dude, where’s your
buffer?

=====================
Mark Roddy


From: Alex Farber [mailto:xxxxx@yahoo.com]
Sent: Tuesday, October 26, 2004 10:06 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] RtlUnicodeStringToAnsiString problem

I am struggling with RtlUnicodeStringToAnsiString function. This is my code:

// pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
ANSI_STRING ansi_string;
DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);

RtlInitAnsiString(&ansi_string, NULL);
nLength = ansi_string.Length;
DbgPrint(“ANSI string length before conversion: %d\n”, nLength);

RtlUnicodeStringToAnsiString(&ansi_string, &pDevExt->ustrDeviceName.uStr,
FALSE);
nLength = ansi_string.Length;
DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
DbgPrint(“ANSI string after conversion:: %s\n”, ansi_string.Buffer);

Debug output is:
Source: \Device\LOOPBACK0
ANSI string length before conversion: 0
ANSI string length after conversion: 17
ANSI string after conversion:: (null)
Destination: (null)

What is wrong in this code? ansi_string length is OK, but buffer is empty.


Do you Yahoo!?
Y!
http:hoo.com> Messenger - Communicate in real time. Download now. — Questions?
First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256 You are currently subscribed to
ntdev as: xxxxx@stratus.com To unsubscribe send a blank email to
xxxxx@lists.osr.com</http:>

Try reading the docs for RtlUnicodeToAnsi…

It says:

So, set the last parameter in your string to “TRUE”, and my guess is that
you’ll get a much better result. Alternatively, supply a “long enough”
pre-determined string in RtlInitAnsiString(), such as
“XXXXXXXXXXXXXXXXXXXXXXXXXXXX” that is long enough to hold your final
string.


Mats

xxxxx@lists.osr.com wrote on 10/26/2004 03:06:00 PM:

I am struggling with RtlUnicodeStringToAnsiString function. This is my
code:

// pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
ANSI_STRING ansi_string;
DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);

RtlInitAnsiString(&ansi_string, NULL);
nLength = ansi_string.Length;
DbgPrint(“ANSI string length before conversion: %d\n”, nLength);

RtlUnicodeStringToAnsiString(&ansi_string, &pDevExt->ustrDeviceName.
uStr, FALSE);
nLength = ansi_string.Length;
DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
DbgPrint(“ANSI string after conversion:: %s\n”, ansi_string.Buffer);
Debug output is:
Source: \Device\LOOPBACK0
ANSI string length before conversion: 0
ANSI string length after conversion: 17
ANSI string after conversion:: (null)
Destination: (null)
What is wrong in this code? ansi_string length is OK, but buffer is
empty.
Do you Yahoo!?
Y! Messenger - Communicate in real time. Download now. —
Questions? First check the Kernel Driver FAQ at http://www.
osronline.com/article.cfm?id=256 You are currently subscribed to
ntdev as: xxxxx@3dlabs.com To unsubscribe send a blank
email to xxxxx@lists.osr.com
ForwardSourceID:NT000060D2

I would think that it does, since the DDK doc’s say that it’s supposed to
return STATUS_SUCCESS if it’s succeeded, and “Otherwise, no storage was
allocated, and no conversion done”. It doesn’t explain what possible errors
you could get, but something along the lines of “Dude, where’s your buffer”
seems like a likely one (STATUS_INVALID_PARAMETER, perhaps?).


Mats

xxxxx@lists.osr.com wrote on 10/26/2004 03:14:15 PM:

So I’m guessing that RUSTAS returns an error, right? Like dude,
where’s your buffer?

=====================
Mark Roddy

From: Alex Farber [mailto:xxxxx@yahoo.com]
Sent: Tuesday, October 26, 2004 10:06 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] RtlUnicodeStringToAnsiString problem

I am struggling with RtlUnicodeStringToAnsiString function. This is my
code:

// pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
ANSI_STRING ansi_string;
DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);

RtlInitAnsiString(&ansi_string, NULL);
nLength = ansi_string.Length;
DbgPrint(“ANSI string length before conversion: %d\n”, nLength);

RtlUnicodeStringToAnsiString(&ansi_string, &pDevExt->ustrDeviceName.
uStr, FALSE);
nLength = ansi_string.Length;
DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
DbgPrint(“ANSI string after conversion:: %s\n”, ansi_string.Buffer);
Debug output is:
Source: \Device\LOOPBACK0
ANSI string length before conversion: 0
ANSI string length after conversion: 17
ANSI string after conversion:: (null)
Destination: (null)
What is wrong in this code? ansi_string length is OK, but buffer is
empty.
Do you Yahoo!?
Y! Messenger - Communicate in real time. Download now. —
Questions? First check the Kernel Driver FAQ at http://www.
osronline.com/article.cfm?id=256 You are currently subscribed to
ntdev as: xxxxx@stratus.com To unsubscribe send a blank email
to xxxxx@lists.osr.com
Questions? First check the Kernel Driver FAQ at http://www.
osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@3dlabs.com
To unsubscribe send a blank email to xxxxx@lists.osr.com
ForwardSourceID:NT000060DE

You should have either allocated a the ansi_string.Buffer your self before
calling RtlUnicodeStringToAnsiString() or put TRUE in the third parameter to
RtlUnicodeStringToAnsiString() so it will allocate.
Either way the problem is that no one allocated the ansi string’s buffer.

Shahar


From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Alex Farber
Sent: Tuesday, October 26, 2004 4:06 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] RtlUnicodeStringToAnsiString problem

I am struggling with RtlUnicodeStringToAnsiString function. This is my code:

// pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
ANSI_STRING ansi_string;
DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);

RtlInitAnsiString(&ansi_string, NULL);
nLength = ansi_string.Length;
DbgPrint(“ANSI string length before conversion: %d\n”, nLength);

RtlUnicodeStringToAnsiString(&ansi_string, &pDevExt->ustrDeviceName.uStr,
FALSE);
nLength = ansi_string.Length;
DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
DbgPrint(“ANSI string after conversion:: %s\n”, ansi_string.Buffer);

Debug output is:
Source: \Device\LOOPBACK0
ANSI string length before conversion: 0
ANSI string length after conversion: 17
ANSI string after conversion:: (null)
Destination: (null)

What is wrong in this code? ansi_string length is OK, but buffer is empty.


Do you Yahoo!?
Y!
http:hoo.com> Messenger - Communicate in real time. Download now. — Questions?
First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256 You are currently subscribed to
ntdev as: xxxxx@safend.com To unsubscribe send a blank email to
xxxxx@lists.osr.com</http:>

It looks like problem is solved by changing the code by this way:

// pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
ANSI_STRING ansi_string;
ansi_string.MaximumLength = (USHORT) pDevExt->ustrDeviceName.uStr.Length;
ansi_string.Buffer = (PSTR)ExAllocatePool(PagedPool, ansi_string.MaximumLength);
RtlUnicodeStringToAnsiString(&ansi_string, &pDevExt->ustrDeviceName.uStr, FALSE);

RtlFreeAnsiString(&ansi_string);

I works but I am not sure that RtlFreeAnsiString releases memory correct after such allocation. Is this OK to allocate ansi_string.Buffer with ExAllocatePool and call RtlFreeAnsiString for clean-up?

“Roddy, Mark” wrote:
So I’m guessing that RUSTAS returns an error, right? Like dude, where’s your buffer?

=====================
Mark Roddy

---------------------------------
From: Alex Farber [mailto:xxxxx@yahoo.com]
Sent: Tuesday, October 26, 2004 10:06 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] RtlUnicodeStringToAnsiString problem

I am struggling with RtlUnicodeStringToAnsiString function. This is my code:

// pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
ANSI_STRING ansi_string;
DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);

RtlInitAnsiString(&ansi_string, NULL);
nLength = ansi_string.Length;
DbgPrint(“ANSI string length before conversion: %d\n”, nLength);

RtlUnicodeStringToAnsiString(&ansi_string, &pDevExt->ustrDeviceName.uStr, FALSE);
nLength = ansi_string.Length;
DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
DbgPrint(“ANSI string after conversion:: %s\n”, ansi_string.Buffer);

Debug output is:
Source: \Device\LOOPBACK0
ANSI string length before conversion: 0
ANSI string length after conversion: 17
ANSI string after conversion:: (null)
Destination: (null)

What is wrong in this code? ansi_string length is OK, but buffer is empty.

---------------------------------
Do you Yahoo!?
Y! Messenger - Communicate in real time. Download now. — Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256 You are currently subscribed to ntdev as: xxxxx@stratus.com To unsubscribe send a blank email to xxxxx@lists.osr.com
Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@yahoo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Alex,

That would work, I suppose, but why not just pass in TRUE as the last
parameter, and RUSTAS will do it for you automatically. That is what the
last parameter is there for…

Unless of course, you have some special reason to call ExAllocatePool
specificially (to set some special mode of the pool parameters for
instance).

I think, if you’re using your own allocation, you should free it yourself
too, as the (looking at the code in the debugger) RtlFreeXXXString uses an
internal RtlHeapFree which isn’t the same as ExFreePool…

So if you allocate it, you free it. If RtlXXX allocates it, RtlFree can be
used to free it.


Mats

xxxxx@lists.osr.com wrote on 10/26/2004 03:36:48 PM:

It looks like problem is solved by changing the code by this way:

// pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
ANSI_STRING ansi_string;
ansi_string.MaximumLength = (USHORT) pDevExt->ustrDeviceName.uStr.Length;
ansi_string.Buffer = (PSTR)ExAllocatePool(PagedPool, ansi_string.
MaximumLength);
RtlUnicodeStringToAnsiString(&ansi_string, &pDevExt->ustrDeviceName.
uStr, FALSE);

RtlFreeAnsiString(&ansi_string);
I works but I am not sure that RtlFreeAnsiString releases memory
correct after such allocation. Is this OK to allocate ansi_string.
Buffer with ExAllocatePool and call RtlFreeAnsiString for clean-up?

“Roddy, Mark” wrote:
> So I’m guessing that RUSTAS returns an error, right? Like dude,
> where’s your buffer?
>
>
> =====================
> Mark Roddy
>
>
> From: Alex Farber [mailto:xxxxx@yahoo.com]
> Sent: Tuesday, October 26, 2004 10:06 AM
> To: Windows System Software Devs Interest List
> Subject: [ntdev] RtlUnicodeStringToAnsiString problem

> I am struggling with RtlUnicodeStringToAnsiString function. This is my
code:
>
> // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> ANSI_STRING ansi_string;
> DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);
>
> RtlInitAnsiString(&ansi_string, NULL);
> nLength = ansi_string.Length;
> DbgPrint(“ANSI string length before conversion: %d\n”, nLength);
>
> RtlUnicodeStringToAnsiString(&ansi_string, &pDevExt->ustrDeviceName.
> uStr, FALSE);
> nLength = ansi_string.Length;
> DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
> DbgPrint(“ANSI string after conversion:: %s\n”, ansi_string.Buffer);
> Debug output is:
> Source: \Device\LOOPBACK0
> ANSI string length before conversion: 0
> ANSI string length after conversion: 17
> ANSI string after conversion:: (null)
> Destination: (null)
> What is wrong in this code? ansi_string length is OK, but buffer is
empty.
> Do you Yahoo!?
> Y! Messenger - Communicate in real time. Download now. —
> Questions? First check the Kernel Driver FAQ at http://www.
> osronline.com/article.cfm?id=256 You are currently subscribed to
> ntdev as: xxxxx@stratus.com To unsubscribe send a blank email
> to xxxxx@lists.osr.com
> Questions? First check the Kernel Driver FAQ at http://www.
> osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@yahoo.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com — Questions? First check the Kernel Driver FAQ at

> http://www.osronline.com/article.cfm?id=256 You are currently
> subscribed to ntdev as: xxxxx@3dlabs.com To unsubscribe
> send a blank email to xxxxx@lists.osr.com
> ForwardSourceID:NT000060F2

Thanks.

Mats PETERSSON wrote:

Alex,

That would work, I suppose, but why not just pass in TRUE as the last
parameter, and RUSTAS will do it for you automatically. That is what the
last parameter is there for…

Unless of course, you have some special reason to call ExAllocatePool
specificially (to set some special mode of the pool parameters for
instance).

I think, if you’re using your own allocation, you should free it yourself
too, as the (looking at the code in the debugger) RtlFreeXXXString uses an
internal RtlHeapFree which isn’t the same as ExFreePool…

So if you allocate it, you free it. If RtlXXX allocates it, RtlFree can be
used to free it.


Mats

xxxxx@lists.osr.com wrote on 10/26/2004 03:36:48 PM:

> It looks like problem is solved by changing the code by this way:
>
> // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> ANSI_STRING ansi_string;
> ansi_string.MaximumLength = (USHORT) pDevExt->ustrDeviceName.uStr.Length;
> ansi_string.Buffer = (PSTR)ExAllocatePool(PagedPool, ansi_string.
> MaximumLength);
> RtlUnicodeStringToAnsiString(&ansi_string, &pDevExt->ustrDeviceName.
> uStr, FALSE);
> …
> RtlFreeAnsiString(&ansi_string);
> I works but I am not sure that RtlFreeAnsiString releases memory
> correct after such allocation. Is this OK to allocate ansi_string.
> Buffer with ExAllocatePool and call RtlFreeAnsiString for clean-up?
>
> “Roddy, Mark” wrote:
> So I’m guessing that RUSTAS returns an error, right? Like dude,
> where’s your buffer?
>
>
> =====================
> Mark Roddy
>
>
> From: Alex Farber [mailto:xxxxx@yahoo.com]
> Sent: Tuesday, October 26, 2004 10:06 AM
> To: Windows System Software Devs Interest List
> Subject: [ntdev] RtlUnicodeStringToAnsiString problem

> I am struggling with RtlUnicodeStringToAnsiString function. This is my
code:
>
> // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> ANSI_STRING ansi_string;
> DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);
>
> RtlInitAnsiString(&ansi_string, NULL);
> nLength = ansi_string.Length;
> DbgPrint(“ANSI string length before conversion: %d\n”, nLength);
>
> RtlUnicodeStringToAnsiString(&ansi_string, &pDevExt->ustrDeviceName.
> uStr, FALSE);
> nLength = ansi_string.Length;
> DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
> DbgPrint(“ANSI string after conversion:: %s\n”, ansi_string.Buffer);
> Debug output is:
> Source: \Device\LOOPBACK0
> ANSI string length before conversion: 0
> ANSI string length after conversion: 17
> ANSI string after conversion:: (null)
> Destination: (null)
> What is wrong in this code? ansi_string length is OK, but buffer is
empty.
> Do you Yahoo!?
> Y! Messenger - Communicate in real time. Download now. —
> Questions? First check the Kernel Driver FAQ at http://www.
> osronline.com/article.cfm?id=256 You are currently subscribed to
> ntdev as: xxxxx@stratus.com To unsubscribe send a blank email
> to xxxxx@lists.osr.com
> Questions? First check the Kernel Driver FAQ at http://www.
> osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@yahoo.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com — Questions? First check the Kernel Driver FAQ at

> http://www.osronline.com/article.cfm?id=256 You are currently
> subscribed to ntdev as: xxxxx@3dlabs.com To unsubscribe
> send a blank email to xxxxx@lists.osr.com
> ForwardSourceID:NT000060F2


Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@yahoo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

---------------------------------
Do you Yahoo!?
Yahoo! Mail Address AutoComplete - You start. We finish.

You should of course check the returned status value from RUSTAS and deal
with error cases regardless of when where and how you allocate the buffer.

=====================
Mark Roddy


From: Alex Farber [mailto:xxxxx@yahoo.com]
Sent: Tuesday, October 26, 2004 11:17 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Thanks.

Mats PETERSSON wrote:

Alex,

That would work, I suppose, but why not just pass in TRUE as the
last
parameter, and RUSTAS will do it for you automatically. That is what
the
last parameter is there for…

Unless of course, you have some special reason to call
ExAllocatePool
specificially (to set some special mode of the pool parameters for
instance).

I think, if you’re using your own allocation, you should free it
yourself
too, as the (looking at the code in the debugger) RtlFreeXXXString
uses an
internal RtlHeapFree which isn’t the same as ExFreePool…

So if you allocate it, you free it. If RtlXXX allocates it, RtlFree
can be
used to free it.


Mats

xxxxx@lists.osr.com wrote on 10/26/2004 03:36:48 PM:

> It looks like problem is solved by changing the code by this way:
>
> // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> ANSI_STRING ansi_string;
> ansi_string.MaximumLength = (USHORT)
pDevExt->ustrDeviceName.uStr.Length;
> ansi_string.Buffer = (PSTR)ExAllocatePool(PagedPool, ansi_string.
> MaximumLength);
> RtlUnicodeStringToAnsiString(&ansi_string,
&pDevExt->ustrDeviceName.
> uStr, FALSE);
> …
> RtlFreeAnsiString(&ansi_string);
> I works but I am not sure that RtlFreeAnsiString releases memory
> correct after such allocation. Is this OK to allocate ansi_string.
> Buffer with ExAllocatePool and call RtlFreeAnsiString for
clean-up?
>
> “Roddy, Mark” wrote:
> So I’m guessing that RUSTAS returns an error, right? Like dude,
> where’s your buffer?
>
>
> =====================
> Mark Roddy
>
>
> From: Alex Farber [mailto:xxxxx@yahoo.com]
> Sent: Tuesday, Octo! ber 26, 2004 10:06 AM
> To: Windows System Software Devs Interest List
> Subject: [ntdev] RtlUnicodeStringToAnsiString problem

> I am struggling with RtlUnicodeStringToAnsiString function. This
is my
code:
>
> // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> ANSI_STRING ansi_string;
> DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);
>
> RtlInitAnsiString(&ansi_string, NULL);
> nLength = ansi_string.Length;
> DbgPrint(“ANSI string length before conversion: %d\n”, nLength);
>
> RtlUnicodeStringToAnsiString(&ansi_string,
&pDevExt->ustrDeviceName.
> uStr, FALSE);
> nLength = ansi_string.Length;
> DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
> DbgPrint(“ANSI string after conversion:: %s\n”,
ansi_string.Buffer);
> Debug output is:
> Source: \Device\LOOPBACK0
> ANSI string length before conversion: 0> ANSI string length after
conversion: 17
> ANSI string after conversion:: (null)
> Destination: (null)
> What is wrong in this code? ansi_string length is OK, but buffer
is
empty.
> Do you Yahoo!?
> Y! Messenger - Communicate in real time. Download now. —
> Questions? First check the Kernel Driver FAQ at http://www.
> osronline.com/article.cfm?id=256 You are currently subscribed to
> ntdev as: xxxxx@stratus.com To unsubscribe send a blank email
> to xxxxx@lists.osr.com
> Questions? First check the Kernel Driver FAQ at http://www.
> osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@yahoo.com
> To unsubscribe send a blank email to
xxxxx@lists.osr.com
> __________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com — Questions? First check the Kernel Driver
FAQ at

> http://www.osronline.com/article.cfm?id=256 You are currently
> subscribed to ntdev as: xxxxx@3dlabs.com To unsubscribe
> send a blank email to xxxxx@lists.osr.com
> ForwardSourceID:NT000060F2


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@yahoo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com



Do you Yahoo!?
Yahoo! Mail Address AutoComplete
http:w_mail/static/ease.html> - You start. We finish. — Questions? First check
the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256 You are
currently subscribed to ntdev as: xxxxx@stratus.com To unsubscribe send
a blank email to xxxxx@lists.osr.com</http:>

Thanks for the help, my final code is:

ANSI_STRING ansi_string;
RtlInitAnsiString(&ansi_string, NULL);

status = RtlUnicodeStringToAnsiString(&ansi_string, &pDevExt->ustrDeviceName.uStr, TRUE);

if ( status == STATUS_SUCCESS )
{

}

RtlFreeAnsiString(&ansi_string);


pIrp->IoStatus.Status = status;
return status;

“Roddy, Mark” wrote:
You should of course check the returned status value from RUSTAS and deal
with error cases regardless of when where and how you allocate the buffer.

=====================
Mark Roddy



From: Alex Farber [mailto:xxxxx@yahoo.com]
Sent: Tuesday, October 26, 2004 11:17 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Thanks.

Mats PETERSSON wrote:

Alex,

That would work, I suppose, but why not just pass in TRUE as the
last
parameter, and RUSTAS will do it for you automatically. That is what
the
last parameter is there for…

Unless of course, you have some special reason to call
ExAllocatePool
specificially (to set some special mode of the pool parameters for
instance).

I think, if you’re using your own allocation, you should free it
yourself
too, as the (looking at the code in the debugger) RtlFreeXXXString
uses an
internal RtlHeapFree which isn’t the same as ExFreePool…

So if you allocate it, you free it. If RtlXXX allocates it, RtlFree
can be
used to free it.


Mats

xxxxx@lists.osr.com wrote on 10/26/2004 03:36:48 PM:

> It looks like problem is solved by changing the code by this way:
>
> // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> ANSI_STRING ansi_string;
> ansi_string.MaximumLength = (USHORT)
pDevExt->ustrDeviceName.uStr.Length;
> ansi_string.Buffer = (PSTR)ExAllocatePool(PagedPool, ansi_string.
> MaximumLength);
> RtlUnicodeStringToAnsiString(&ansi_string,
&pDevExt->ustrDeviceName.
> uStr, FALSE);
> …
> RtlFreeAnsiString(&ansi_string);
> I works but I am not sure that RtlFreeAnsiString releases memory
> correct after such allocation. Is this OK to allocate ansi_string.
> Buffer with ExAllocatePool and call RtlFreeAnsiString for
clean-up?
>
> “Roddy, Mark” wrote:
> So I’m guessing that RUSTAS returns an error, right? Like dude,
> where’s your buffer?
>
>
> =====================
> Mark Roddy
>
>
> From: Alex Farber [mailto:xxxxx@yahoo.com]
> Sent: Tuesday, Octo! ber 26, 2004 10:06 AM
> To: Windows System Software Devs Interest List
> Subject: [ntdev] RtlUnicodeStringToAnsiString problem

> I am struggling with RtlUnicodeStringToAnsiString function. This
is my
code:
>
> // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> ANSI_STRING ansi_string;
> DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);
>
> RtlInitAnsiString(&ansi_string, NULL);
> nLength = ansi_string.Length;
> DbgPrint(“ANSI string length before conversion: %d\n”, nLength);
>
> RtlUnicodeStringToAnsiString(&ansi_string,
&pDevExt->ustrDeviceName.
> uStr, FALSE);
> nLength = ansi_string.Length;
> DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
> DbgPrint(“ANSI string after conversion:: %s\n”,
ansi_string.Buffer);
> Debug output is:
> Source: \Device\LOOPBACK0
> ANSI string length before conversion: 0> ANSI string length after
conversion: 17
> ANSI string after conversion:: (null)
> Destination: (null)
> What is wrong in this code? ansi_string length is OK, but buffer
is
empty.
> Do you Yahoo!?
> Y! Messenger - Communicate in real time. Download now. —
> Questions? First check the Kernel Driver FAQ at http://www.
> osronline.com/article.cfm?id=256 You are currently subscribed to
> ntdev as: xxxxx@stratus.com To unsubscribe send a blank email
> to xxxxx@lists.osr.com
> Questions? First check the Kernel Driver FAQ at http://www.
> osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@yahoo.com
> To unsubscribe send a blank email to
xxxxx@lists.osr.com
>
__________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com — Questions? First check the Kernel Driver
FAQ at

> http://www.osronline.com/article.cfm?id=256 You are currently
> subscribed to ntdev as: xxxxx@3dlabs.com To unsubscribe
> send a blank email to xxxxx@lists.osr.com
> ForwardSourceID:NT000060F2


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@yahoo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

________________________________

Do you Yahoo!?
Yahoo! Mail Address AutoComplete
w_mail/static/ease.html> - You start. We finish. — Questions? First check
the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256 You are
currently subscribed to ntdev as: xxxxx@stratus.com To unsubscribe send
a blank email to xxxxx@lists.osr.com


Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@yahoo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

---------------------------------
Do you Yahoo!?
Yahoo! Mail Address AutoComplete - You start. We finish.

Agreed. Checking status of any function is NECESSARY to guarantee
functional drivers for instance when running in low-memory situations. Or
BSOD’s will be quite quick to happen.

Oh, and of course DEALING with the error condition in a suitable way. The
first step is to print out any unexpected failed functions and perhaps even
stopping (DebugBreak or similar) when the driver encounters anything
unexpected. Second, you need to figure out what to do when this happens
(Continue anyways isn’t a particularly good idea if you failed to allocate
memory, so failing whatever is going on is probably the right method at
such a case, but it all depends on exactly what is happening in the system
and what the consequences are of “just continuing”).


Mats

xxxxx@lists.osr.com wrote on 10/26/2004 04:26:59 PM:

You should of course check the returned status value from RUSTAS and deal
with error cases regardless of when where and how you allocate the
buffer.

=====================
Mark Roddy


From: Alex Farber [mailto:xxxxx@yahoo.com]
Sent: Tuesday, October 26, 2004 11:17 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Thanks.

Mats PETERSSON wrote:
>
>
>
>
>
>
> Alex,
>
> That would work, I suppose, but why not just pass in TRUE as the
> last
> parameter, and RUSTAS will do it for you automatically. That is what
> the
> last parameter is there for…
>
> Unless of course, you have some special reason to call
> ExAllocatePool
> specificially (to set some special mode of the pool parameters for
> instance).
>
> I think, if you’re using your own allocation, you should free it
> yourself
> too, as the (looking at the code in the debugger) RtlFreeXXXString
> uses an
> internal RtlHeapFree which isn’t the same as ExFreePool…
>
> So if you allocate it, you free it. If RtlXXX allocates it, RtlFree
> can be
> used to free it.
>
> –
> Mats
>
> xxxxx@lists.osr.com wrote on 10/26/2004 03:36:48 PM:
>
> > It looks like problem is solved by changing the code by this way:
> >
> > // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> > ANSI_STRING ansi_string;
> > ansi_string.MaximumLength = (USHORT)
> pDevExt->ustrDeviceName.uStr.Length;
> > ansi_string.Buffer = (PSTR)ExAllocatePool(PagedPool, ansi_string.
> > MaximumLength);
> > RtlUnicodeStringToAnsiString(&ansi_string,
> &pDevExt->ustrDeviceName.
> > uStr, FALSE);
> > …
> > RtlFreeAnsiString(&ansi_string);
> > I works but I am not sure that RtlFreeAnsiString releases memory
> > correct after such allocation. Is this OK to allocate ansi_string.
> > Buffer with ExAllocatePool and call RtlFreeAnsiString for
> clean-up?
> >
> > “Roddy, Mark” wrote:
> > So I’m guessing that RUSTAS returns an error, right? Like dude,
> > where’s your buffer?
> >
> >
> > =====================
> > Mark Roddy
> >
> >
> > From: Alex Farber [mailto:xxxxx@yahoo.com]
> > Sent: Tuesday, Octo! ber 26, 2004 10:06 AM
> > To: Windows System Software Devs Interest List
> > Subject: [ntdev] RtlUnicodeStringToAnsiString problem
>
> > I am struggling with RtlUnicodeStringToAnsiString function. This
> is my
> code:
> >
> > // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> > ANSI_STRING ansi_string;
> > DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);
> >
> > RtlInitAnsiString(&ansi_string, NULL);
> > nLength = ansi_string.Length;
> > DbgPrint(“ANSI string length before conversion: %d\n”, nLength);
> >
> > RtlUnicodeStringToAnsiString(&ansi_string,
> &pDevExt->ustrDeviceName.
> > uStr, FALSE);
> > nLength = ansi_string.Length;
> > DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
> > DbgPrint(“ANSI string after conversion:: %s\n”,
> ansi_string.Buffer);
> > Debug output is:
> > Source: \Device\LOOPBACK0
> > ANSI string length before conversion: 0> ANSI string length after
> conversion: 17
> > ANSI string after conversion:: (null)
> > Destination: (null)
> > What is wrong in this code? ansi_string length is OK, but buffer
> is
> empty.
> > Do you Yahoo!?
> > Y! Messenger - Communicate in real time. Download now. —
> > Questions? First check the Kernel Driver FAQ at http://www.
> > osronline.com/article.cfm?id=256 You are currently subscribed to
> > ntdev as: xxxxx@stratus.com To unsubscribe send a blank email
> > to xxxxx@lists.osr.com
> > Questions? First check the Kernel Driver FAQ at http://www.
> > osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as: xxxxx@yahoo.com
> > To unsubscribe send a blank email to
> xxxxx@lists.osr.com
> > __________________
> > Do You Yahoo!?
> > Tired of spam? Yahoo! Mail has the best spam protection around
> > http://mail.yahoo.com — Questions? First check the Kernel Driver
> FAQ at
>
> > http://www.osronline.com/article.cfm?id=256 You are currently
> > subscribed to ntdev as: xxxxx@3dlabs.com To unsubscribe
> > send a blank email to xxxxx@lists.osr.com
> > ForwardSourceID:NT000060F2
>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@yahoo.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>
>

>
> Do you Yahoo!?
> Yahoo! Mail Address AutoComplete
>
http:
> w_mail/static/ease.html> - You start. We finish. — Questions? First
check
> the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256 You
are
> currently subscribed to ntdev as: xxxxx@stratus.com To unsubscribe
send
> a blank email to xxxxx@lists.osr.com
>
>
> —
> Questions? First check the Kernel Driver FAQ at http://www.
> osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@3dlabs.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com

> ForwardSourceID:NT0000611E</http:>

Uh - you might want to rethink that code segment. Freeing unallocated
buffers generally has dire results.

=====================
Mark Roddy


From: Alex Farber [mailto:xxxxx@yahoo.com]
Sent: Tuesday, October 26, 2004 11:40 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Thanks for the help, my final code is:

ANSI_STRING ansi_string;
RtlInitAnsiString(&ansi_string, NULL);

status = RtlUnicodeStringToAnsiString(&ansi_string,
&pDevExt->ustrDeviceName.uStr, TRUE);

if ( status == STATUS_SUCCESS )
{

}

RtlFreeAnsiString(&ansi_string);


pIrp->IoStatus.Status = status;
return status;

“Roddy, Mark” wrote:

You should of course check the returned status value from RUSTAS and
deal
with error cases regardless of when where and how you allocate the
buffer.

=====================
Mark Roddy



From: Alex Farber [mailto:xxxxx@yahoo.com]
Sent: Tuesday, October 26, 2004 11:17 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Thanks.

Mats PETERSSON wrote:

Alex,

That would work, I suppose, but why not just pass in TRUE as the
last
parameter, and RUSTAS will do it for you automatically. That is what
the
last parameter is there for…

Unless of course, you have some special reason to call
ExAllocatePool
specificially (to set some special mode! of the pool parameters for
instance).

I think, if you’re using your own allocation, you should free it
yourself
too, as the (looking at the code in the debugger) RtlFreeXXXString
uses an
internal RtlHeapFree which isn’t the same as ExFreePool…

So if you allocate it, you free it. If RtlXXX allocates it, RtlFree
can be
used to free it.


Mats

xxxxx@lists.osr.com wrote on 10/26/2004 03:36:48 PM:

> It looks like problem is solved by changing the code by this way:
>
> // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> ANSI_STRING ansi_string;
> ansi_string.MaximumLength = (USHORT)
pDevExt->ustrDeviceName.uStr.Length;
> ansi_string.Buffer = (PSTR)ExAllocatePool(PagedPool, ansi_string.
> MaximumLength);
> RtlUnicodeStringToAnsiString(&ansi_string,
&pDevExt->ustrDeviceName.
> uStr, FALSE);
> …
> RtlFreeAnsiString(&ansi_string);
> I works but I am not sure that RtlFreeAnsiString releases memory
> correct after such allocation. Is this OK to allocate ansi_string.
> Buffer with ExAllocatePool and call RtlFreeAnsiString for
clean-up?
>
> “Roddy, Mark” wrote:
> So I’m guessing that RUSTAS returns an error, right? Like dude,
> where’s your buffer?
>
>
> =====================
> Mark Roddy
>
>
> From: Alex Farber [mailto:xxxxx@yahoo.com]
> Sent: Tuesday, Octo! ber 26, 2004 10:06 AM
> To: Windows System Software Devs Interest List
> Subject: [ntdev] RtlUnicodeStringToAnsiString problem

> I am struggling with RtlUnicodeStringToAnsiString function. This
is my
code:
>
> // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> ANSI_STRING ansi_string;
> DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);
>
> RtlInitAnsiString(&ansi_string, NULL);
> nLength = ansi_string.Length;
> DbgPrint(“ANSI string length before conversion: %d\n”, nLength);
>
> RtlUnicodeStringToAnsiString(&ansi_string,
&pDevExt->ustrDeviceName.
> uStr, FALSE);
> nLength = ansi_string.Length;
> DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
> DbgPrint(“ANSI string after conversion:: %s\n”,
ansi_string.Buffer);
> Debug output is:
> Source: \Device\LOOPBACK0
> ANSI string length before conversion: 0> ANSI string length after
conversion: 17
> ANSI string after conversion:: (null)
> Destination: (null)
> What is wrong in this code? ansi_string length is OK, but buffer
is
empty.
> Do you Yahoo!?
> Y! Messenger - Communicate in real time. Download now. —
> Questions? First check the Kernel Driver FAQ a! t http://www.
> osronline.com/article.cfm?id=256 You are currently subscribed to
> ntdev as: xxxxx@stratus.com To unsubscribe send a blank email
> to xxxxx@lists.osr.com
> Questions? First check the Kernel Driver FAQ at http://www.
> osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@yahoo.com
> To unsubscribe send a blank email to
xxxxx@lists.osr.com
>
__________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com — Questions? First check the Kernel Driver
FAQ at

> http://www.osronline.com/article.cfm?id=256 You are currently
> subscribed to ntdev as: xxxxx@3dlabs.com To unsubscribe
> send a blank email to xxxxx@lists.osr.com
> ForwardSourceID:NT000060F2


Questions? F! irst check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@yahoo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com



Do you Yahoo!?
Yahoo! Mail Address AutoComplete

w_mail/static/ease.html> - You start. We finish. — Questions?
First check
the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
You are
currently subscribed to ntdev as: xxxxx@stratus.com To
unsubscribe send
a blank email to xxxxx@lists.osr.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@yahoo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com



Do you Yahoo!?
Yahoo! Mail Address AutoComplete
http:w_mail/static/ease.html> - You start. We finish. — Questions? First check
the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256 You are
currently subscribed to ntdev as: xxxxx@stratus.com To unsubscribe send
a blank email to xxxxx@lists.osr.com</http:>

Buffer is allocated by RtlUnicodeStringToAnsiString because of TRUE flag.
Not the clearest of possible APIs, certainly - but documented.

-----Original Message-----
From: Roddy, Mark [mailto:xxxxx@stratus.com]
Sent: Tuesday, October 26, 2004 6:12 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Uh - you might want to rethink that code segment. Freeing unallocated
buffers generally has dire results.

=====================
Mark Roddy


From: Alex Farber [mailto:xxxxx@yahoo.com]
Sent: Tuesday, October 26, 2004 11:40 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Thanks for the help, my final code is:

ANSI_STRING ansi_string;
RtlInitAnsiString(&ansi_string, NULL);

status = RtlUnicodeStringToAnsiString(&ansi_string,
&pDevExt->ustrDeviceName.uStr, TRUE);

if ( status == STATUS_SUCCESS )
{

}

RtlFreeAnsiString(&ansi_string);


pIrp->IoStatus.Status = status;
return status;

“Roddy, Mark” wrote:

You should of course check the returned status value from RUSTAS and
deal
with error cases regardless of when where and how you allocate the
buffer.

=====================
Mark Roddy



From: Alex Farber [mailto:xxxxx@yahoo.com]
Sent: Tuesday, October 26, 2004 11:17 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Thanks.

Mats PETERSSON wrote:

Alex,

That would work, I suppose, but why not just pass in TRUE as the
last
parameter, and RUSTAS will do it for you automatically. That is what
the
last parameter is there for…

Unless of course, you have some special reason to call
ExAllocatePool
specificially (to set some special mode! of the pool parameters for
instance).

I think, if you’re using your own allocation, you should free it
yourself
too, as the (looking at the code in the debugger) RtlFreeXXXString
uses an
internal RtlHeapFree which isn’t the same as ExFreePool…

So if you allocate it, you free it. If RtlXXX allocates it, RtlFree
can be
used to free it.


Mats

xxxxx@lists.osr.com wrote on 10/26/2004 03:36:48 PM:

> It looks like problem is solved by changing the code by this way:
>
> // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> ANSI_STRING ansi_string;
> ansi_string.MaximumLength = (USHORT)
pDevExt->ustrDeviceName.uStr.Length;
> ansi_string.Buffer = (PSTR)ExAllocatePool(PagedPool, ansi_string.
> MaximumLength);
> RtlUnicodeStringToAnsiString(&ansi_string,
&pDevExt->ustrDeviceName.
> uStr, FALSE);
> …
> RtlFreeAnsiString(&ansi_string);
> I works but I am not sure that RtlFreeAnsiString releases memory
> correct after such allocation. Is this OK to allocate ansi_string.
> Buffer with ExAllocatePool and call RtlFreeAnsiString for
clean-up?
>
> “Roddy, Mark” wrote:
> So I’m guessing that RUSTAS returns an error, right? Like dude,
> where’s your buffer?
>
>
> =====================
> Mark Roddy
>
>
> From: Alex Farber [mailto:xxxxx@yahoo.com]
> Sent: Tuesday, Octo! ber 26, 2004 10:06 AM
> To: Windows System Software Devs Interest List
> Subject: [ntdev] RtlUnicodeStringToAnsiString problem

> I am struggling with RtlUnicodeStringToAnsiString function. This
is my
code:
>
> // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> ANSI_STRING ansi_string;
> DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);
>
> RtlInitAnsiString(&ansi_string, NULL);
> nLength = ansi_string.Length;
> DbgPrint(“ANSI string length before conversion: %d\n”, nLength);
>
> RtlUnicodeStringToAnsiString(&ansi_string,
&pDevExt->ustrDeviceName.
> uStr, FALSE);
> nLength = ansi_string.Length;
> DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
> DbgPrint(“ANSI string after conversion:: %s\n”,
ansi_string.Buffer);
> Debug output is:
> Source: \Device\LOOPBACK0
> ANSI string length before conversion: 0> ANSI string length after
conversion: 17
> ANSI string after conversion:: (null)
> Destination: (null)
> What is wrong in this code? ansi_string length is OK, but buffer
is
empty.
> Do you Yahoo!?
> Y! Messenger - Communicate in real time. Download now. —
> Questions? First check the Kernel Driver FAQ a! t http://www.
> osronline.com/article.cfm?id=256 You are currently subscribed to
> ntdev as: xxxxx@stratus.com To unsubscribe send a blank email
> to xxxxx@lists.osr.com
> Questions? First check the Kernel Driver FAQ at http://www.
> osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@yahoo.com
> To unsubscribe send a blank email to
xxxxx@lists.osr.com
>
__________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com — Questions? First check the Kernel Driver
FAQ at

> http://www.osronline.com/article.cfm?id=256 You are currently
> subscribed to ntdev as: xxxxx@3dlabs.com To unsubscribe
> send a blank email to xxxxx@lists.osr.com
> ForwardSourceID:NT000060F2


Questions? F! irst check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@yahoo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com



Do you Yahoo!?
Yahoo! Mail Address AutoComplete

w_mail/static/ease.html> - You start. We finish. — Questions?
First check
the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
You are
currently subscribed to ntdev as: xxxxx@stratus.com To
unsubscribe send
a blank email to xxxxx@lists.osr.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@yahoo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com



Do you Yahoo!?
Yahoo! Mail Address AutoComplete
http:w_mail/static/ease.html> - You start. We finish. — Questions? First check
the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256 You are
currently subscribed to ntdev as: xxxxx@stratus.com To unsubscribe send
a blank email to xxxxx@lists.osr.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@creo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com</http:>

xxxxx@lists.osr.com wrote on 10/26/2004 05:18:26 PM:

Buffer is allocated by RtlUnicodeStringToAnsiString because of TRUE flag.
Not the clearest of possible APIs, certainly - but documented.

I think what Mark was referring to, and what I pointed out in my excerpt
from the RUSTAS Doc’s, that if the call doesn’t return Succes, you didn’t
get any allocation, and thus YOU SHOULD NOT FREE it. Because you’ll either
try to free NULL, or worse, some random value. Freeing something that
hasn’t been allocated is a bad idea.

Now it may be OK to try to free a NULL, becasue from memory from looking at
the RtlFreeXXXString, it looked like it was checking for NULL and skipping
out. But I wouldn’t rely on such behaviour, since it may change in future
code (unless it’s specified that you can call RtlFreeXXXString with NULL in
the Doc’s). Also relying on a “Failed allocation” to be NULL is probably a
bit doubtful.

So, to keep the code clean and reliable, only free if it was a success.


Mats

-----Original Message-----
From: Roddy, Mark [mailto:xxxxx@stratus.com]
Sent: Tuesday, October 26, 2004 6:12 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Uh - you might want to rethink that code segment. Freeing unallocated
buffers generally has dire results.

=====================
Mark Roddy


From: Alex Farber [mailto:xxxxx@yahoo.com]
Sent: Tuesday, October 26, 2004 11:40 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Thanks for the help, my final code is:

ANSI_STRING ansi_string;
RtlInitAnsiString(&ansi_string, NULL);

status = RtlUnicodeStringToAnsiString(&ansi_string,
&pDevExt->ustrDeviceName.uStr, TRUE);

if ( status == STATUS_SUCCESS )
{

}

RtlFreeAnsiString(&ansi_string);


pIrp->IoStatus.Status = status;
return status;

“Roddy, Mark” wrote:
>
> You should of course check the returned status value from RUSTAS and
> deal
> with error cases regardless of when where and how you allocate the
> buffer.
>
>
>
> =====================
> Mark Roddy
>
>
>
>
>
>
> From: Alex Farber [mailto:xxxxx@yahoo.com]
> Sent: Tuesday, October 26, 2004 11:17 AM
> To: Windows System Software Devs Interest List
> Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem
>
>
> Thanks.
>
> Mats PETERSSON wrote:
>
>
>
>
>
>
> Alex,
>
> That would work, I suppose, but why not just pass in TRUE as the
> last
> parameter, and RUSTAS will do it for you automatically. That is what
> the
> last parameter is there for…
>
> Unless of course, you have some special reason to call
> ExAllocatePool
> specificially (to set some special mode! of the pool parameters for
> instance).
>
> I think, if you’re using your own allocation, you should free it
> yourself
> too, as the (looking at the code in the debugger) RtlFreeXXXString
> uses an
> internal RtlHeapFree which isn’t the same as ExFreePool…
>
> So if you allocate it, you free it. If RtlXXX allocates it, RtlFree
> can be
> used to free it.
>
> –
> Mats
>
> xxxxx@lists.osr.com wrote on 10/26/2004 03:36:48 PM:
>
> > It looks like problem is solved by changing the code by this way:
> >
> > // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> > ANSI_STRING ansi_string;
> > ansi_string.MaximumLength = (USHORT)
> pDevExt->ustrDeviceName.uStr.Length;
> > ansi_string.Buffer = (PSTR)ExAllocatePool(PagedPool, ansi_string.
> > MaximumLength);
> > RtlUnicodeStringToAnsiString(&ansi_string,
> &pDevExt->ustrDeviceName.
> > uStr, FALSE);
> > …
> > RtlFreeAnsiString(&ansi_string);
> > I works but I am not sure that RtlFreeAnsiString releases memory
> > correct after such allocation. Is this OK to allocate ansi_string.
> > Buffer with ExAllocatePool and call RtlFreeAnsiString for
> clean-up?
> >
> > “Roddy, Mark” wrote:
> > So I’m guessing that RUSTAS returns an error, right? Like dude,
> > where’s your buffer?
> >
> >
> > =====================
> > Mark Roddy
> >
> >
> > From: Alex Farber [mailto:xxxxx@yahoo.com]
> > Sent: Tuesday, Octo! ber 26, 2004 10:06 AM
> > To: Windows System Software Devs Interest List
> > Subject: [ntdev] RtlUnicodeStringToAnsiString problem
>
> > I am struggling with RtlUnicodeStringToAnsiString function. This
> is my
> code:
> >
> > // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> > ANSI_STRING ansi_string;
> > DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);
> >
> > RtlInitAnsiString(&ansi_string, NULL);
> > nLength = ansi_string.Length;
> > DbgPrint(“ANSI string length before conversion: %d\n”, nLength);
> >
> > RtlUnicodeStringToAnsiString(&ansi_string,
> &pDevExt->ustrDeviceName.
> > uStr, FALSE);
> > nLength = ansi_string.Length;
> > DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
> > DbgPrint(“ANSI string after conversion:: %s\n”,
> ansi_string.Buffer);
> > Debug output is:
> > Source: \Device\LOOPBACK0
> > ANSI string length before conversion: 0> ANSI string length after
> conversion: 17
> > ANSI string after conversion:: (null)
> > Destination: (null)
> > What is wrong in this code? ansi_string length is OK, but buffer
> is
> empty.
> > Do you Yahoo!?
> > Y! Messenger - Communicate in real time. Download now. —
> > Questions? First check the Kernel Driver FAQ a! t http://www.
> > osronline.com/article.cfm?id=256 You are currently subscribed to
> > ntdev as: xxxxx@stratus.com To unsubscribe send a blank email
> > to xxxxx@lists.osr.com
> > Questions? First check the Kernel Driver FAQ at http://www.
> > osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as: xxxxx@yahoo.com
> > To unsubscribe send a blank email to
> xxxxx@lists.osr.com
> >
__________________
> > Do You Yahoo!?
> > Tired of spam? Yahoo! Mail has the best spam protection around
> > http://mail.yahoo.com — Questions? First check the Kernel Driver
> FAQ at
>
> > http://www.osronline.com/article.cfm?id=256 You are currently
> > subscribed to ntdev as: xxxxx@3dlabs.com To unsubscribe
> > send a blank email to xxxxx@lists.osr.com
> > ForwardSourceID:NT000060F2
>
>
>
> —
> Questions? F! irst check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@yahoo.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>
>
>
> Do you Yahoo!?
> Yahoo! Mail Address AutoComplete
>
> w_mail/static/ease.html> - You start. We finish. — Questions?
> First check
> the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
> You are
> currently subscribed to ntdev as: xxxxx@stratus.com To
> unsubscribe send
> a blank email to xxxxx@lists.osr.com
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@yahoo.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>
>

>
> Do you Yahoo!?
> Yahoo! Mail Address AutoComplete
>
http:
> w_mail/static/ease.html> - You start. We finish. — Questions? First
check
> the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256 You
are
> currently subscribed to ntdev as: xxxxx@stratus.com To unsubscribe
send
> a blank email to xxxxx@lists.osr.com
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@creo.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>
> —
> Questions? First check the Kernel Driver FAQ at http://www.
> osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@3dlabs.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com

> ForwardSourceID:NT00006162</http:>

In the error case the buffer is not allocated. Calling RtlFreeAnsiString on
failure may or may not work depending on how RtlFreeAnsiString and
RtlUnicodeStringToAnsiString are implemented. RtlUnicodeStringToAnsiString
has to set the Buffer value of the string to NULL and RtlFreeAnsiString has
to not do anything if the Buffer value is NULL. You are depending on a lot
of internal behavior in these functions. It might work on one platform with
one OS release and fail on another.

=====================
Mark Roddy

-----Original Message-----
From: Alexander Krol [mailto:xxxxx@creo.com]
Sent: Tuesday, October 26, 2004 12:18 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Buffer is allocated by RtlUnicodeStringToAnsiString because of TRUE flag.
Not the clearest of possible APIs, certainly - but documented.

-----Original Message-----
From: Roddy, Mark [mailto:xxxxx@stratus.com]
Sent: Tuesday, October 26, 2004 6:12 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Uh - you might want to rethink that code segment. Freeing unallocated
buffers generally has dire results.

=====================
Mark Roddy


From: Alex Farber [mailto:xxxxx@yahoo.com]
Sent: Tuesday, October 26, 2004 11:40 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Thanks for the help, my final code is:

ANSI_STRING ansi_string;
RtlInitAnsiString(&ansi_string, NULL);

status = RtlUnicodeStringToAnsiString(&ansi_string,
&pDevExt->ustrDeviceName.uStr, TRUE);

if ( status == STATUS_SUCCESS )
{

}

RtlFreeAnsiString(&ansi_string);


pIrp->IoStatus.Status = status;
return status;

“Roddy, Mark” wrote:

You should of course check the returned status value from RUSTAS and
deal
with error cases regardless of when where and how you allocate the
buffer.

=====================
Mark Roddy



From: Alex Farber [mailto:xxxxx@yahoo.com]
Sent: Tuesday, October 26, 2004 11:17 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Thanks.

Mats PETERSSON wrote:

Alex,

That would work, I suppose, but why not just pass in TRUE as the
last
parameter, and RUSTAS will do it for you automatically. That is what
the
last parameter is there for…

Unless of course, you have some special reason to call
ExAllocatePool
specificially (to set some special mode! of the pool parameters for
instance).

I think, if you’re using your own allocation, you should free it
yourself
too, as the (looking at the code in the debugger) RtlFreeXXXString
uses an
internal RtlHeapFree which isn’t the same as ExFreePool…

So if you allocate it, you free it. If RtlXXX allocates it, RtlFree
can be
used to free it.


Mats

xxxxx@lists.osr.com wrote on 10/26/2004 03:36:48 PM:

> It looks like problem is solved by changing the code by this way:
>
> // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> ANSI_STRING ansi_string;
> ansi_string.MaximumLength = (USHORT)
pDevExt->ustrDeviceName.uStr.Length;
> ansi_string.Buffer = (PSTR)ExAllocatePool(PagedPool, ansi_string.
> MaximumLength);
> RtlUnicodeStringToAnsiString(&ansi_string,
&pDevExt->ustrDeviceName.
> uStr, FALSE);
> …
> RtlFreeAnsiString(&ansi_string);
> I works but I am not sure that RtlFreeAnsiString releases memory
> correct after such allocation. Is this OK to allocate ansi_string.
> Buffer with ExAllocatePool and call RtlFreeAnsiString for
clean-up?
>
> “Roddy, Mark” wrote:
> So I’m guessing that RUSTAS returns an error, right? Like dude,
> where’s your buffer?
>
>
> =====================
> Mark Roddy
>
>
> From: Alex Farber [mailto:xxxxx@yahoo.com]
> Sent: Tuesday, Octo! ber 26, 2004 10:06 AM
> To: Windows System Software Devs Interest List
> Subject: [ntdev] RtlUnicodeStringToAnsiString problem

> I am struggling with RtlUnicodeStringToAnsiString function. This
is my
code:
>
> // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> ANSI_STRING ansi_string;
> DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);
>
> RtlInitAnsiString(&ansi_string, NULL);
> nLength = ansi_string.Length;
> DbgPrint(“ANSI string length before conversion: %d\n”, nLength);
>
> RtlUnicodeStringToAnsiString(&ansi_string,
&pDevExt->ustrDeviceName.
> uStr, FALSE);
> nLength = ansi_string.Length;
> DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
> DbgPrint(“ANSI string after conversion:: %s\n”,
ansi_string.Buffer);
> Debug output is:
> Source: \Device\LOOPBACK0
> ANSI string length before conversion: 0> ANSI string length after
conversion: 17
> ANSI string after conversion:: (null)
> Destination: (null)
> What is wrong in this code? ansi_string length is OK, but buffer
is
empty.
> Do you Yahoo!?
> Y! Messenger - Communicate in real time. Download now. —
> Questions? First check the Kernel Driver FAQ a! t http://www.
> osronline.com/article.cfm?id=256 You are currently subscribed to
> ntdev as: xxxxx@stratus.com To unsubscribe send a blank email
> to xxxxx@lists.osr.com
> Questions? First check the Kernel Driver FAQ at http://www.
> osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@yahoo.com
> To unsubscribe send a blank email to
xxxxx@lists.osr.com
>
__________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com — Questions? First check the Kernel Driver
FAQ at

> http://www.osronline.com/article.cfm?id=256 You are currently
> subscribed to ntdev as: xxxxx@3dlabs.com To unsubscribe
> send a blank email to xxxxx@lists.osr.com
> ForwardSourceID:NT000060F2


Questions? F! irst check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@yahoo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com



Do you Yahoo!?
Yahoo! Mail Address AutoComplete

w_mail/static/ease.html> - You start. We finish. — Questions?
First check
the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
You are
currently subscribed to ntdev as: xxxxx@stratus.com To
unsubscribe send
a blank email to xxxxx@lists.osr.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@yahoo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com



Do you Yahoo!?
Yahoo! Mail Address AutoComplete
http:w_mail/static/ease.html> - You start. We finish. — Questions? First check
the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256 You are
currently subscribed to ntdev as: xxxxx@stratus.com To unsubscribe send
a blank email to xxxxx@lists.osr.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@creo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@stratus.com
To unsubscribe send a blank email to xxxxx@lists.osr.com</http:>

If RUSTAS doesn’t return success, one should not touch the damned stuff,
certainly - since nowhere it is documented that it will be NULL. On the
other side, standard deallocating routines safely accept NULL, and, though
it is not documented, I would be very surprised if RtlFreeXXXString would
duffer from the bunch.

-----Original Message-----
From: Mats PETERSSON [mailto:xxxxx@3dlabs.com]
Sent: Tuesday, October 26, 2004 6:33 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

xxxxx@lists.osr.com wrote on 10/26/2004 05:18:26 PM:

Buffer is allocated by RtlUnicodeStringToAnsiString because of TRUE flag.
Not the clearest of possible APIs, certainly - but documented.

I think what Mark was referring to, and what I pointed out in my excerpt
from the RUSTAS Doc’s, that if the call doesn’t return Succes, you didn’t
get any allocation, and thus YOU SHOULD NOT FREE it. Because you’ll either
try to free NULL, or worse, some random value. Freeing something that
hasn’t been allocated is a bad idea.

Now it may be OK to try to free a NULL, becasue from memory from looking at
the RtlFreeXXXString, it looked like it was checking for NULL and skipping
out. But I wouldn’t rely on such behaviour, since it may change in future
code (unless it’s specified that you can call RtlFreeXXXString with NULL in
the Doc’s). Also relying on a “Failed allocation” to be NULL is probably a
bit doubtful.

So, to keep the code clean and reliable, only free if it was a success.


Mats

-----Original Message-----
From: Roddy, Mark [mailto:xxxxx@stratus.com]
Sent: Tuesday, October 26, 2004 6:12 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Uh - you might want to rethink that code segment. Freeing unallocated
buffers generally has dire results.

=====================
Mark Roddy


From: Alex Farber [mailto:xxxxx@yahoo.com]
Sent: Tuesday, October 26, 2004 11:40 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Thanks for the help, my final code is:

ANSI_STRING ansi_string;
RtlInitAnsiString(&ansi_string, NULL);

status = RtlUnicodeStringToAnsiString(&ansi_string,
&pDevExt->ustrDeviceName.uStr, TRUE);

if ( status == STATUS_SUCCESS )
{

}

RtlFreeAnsiString(&ansi_string);


pIrp->IoStatus.Status = status;
return status;

“Roddy, Mark” wrote:
>
> You should of course check the returned status value from RUSTAS and
> deal
> with error cases regardless of when where and how you allocate the
> buffer.
>
>
>
> =====================
> Mark Roddy
>
>
>
>
>
>
> From: Alex Farber [mailto:xxxxx@yahoo.com]
> Sent: Tuesday, October 26, 2004 11:17 AM
> To: Windows System Software Devs Interest List
> Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem
>
>
> Thanks.
>
> Mats PETERSSON wrote:
>
>
>
>
>
>
> Alex,
>
> That would work, I suppose, but why not just pass in TRUE as the
> last
> parameter, and RUSTAS will do it for you automatically. That is what
> the
> last parameter is there for…
>
> Unless of course, you have some special reason to call
> ExAllocatePool
> specificially (to set some special mode! of the pool parameters for
> instance).
>
> I think, if you’re using your own allocation, you should free it
> yourself
> too, as the (looking at the code in the debugger) RtlFreeXXXString
> uses an
> internal RtlHeapFree which isn’t the same as ExFreePool…
>
> So if you allocate it, you free it. If RtlXXX allocates it, RtlFree
> can be
> used to free it.
>
> –
> Mats
>
> xxxxx@lists.osr.com wrote on 10/26/2004 03:36:48 PM:
>
> > It looks like problem is solved by changing the code by this way:
> >
> > // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> > ANSI_STRING ansi_string;
> > ansi_string.MaximumLength = (USHORT)
> pDevExt->ustrDeviceName.uStr.Length;
> > ansi_string.Buffer = (PSTR)ExAllocatePool(PagedPool, ansi_string.
> > MaximumLength);
> > RtlUnicodeStringToAnsiString(&ansi_string,
> &pDevExt->ustrDeviceName.
> > uStr, FALSE);
> > …
> > RtlFreeAnsiString(&ansi_string);
> > I works but I am not sure that RtlFreeAnsiString releases memory
> > correct after such allocation. Is this OK to allocate ansi_string.
> > Buffer with ExAllocatePool and call RtlFreeAnsiString for
> clean-up?
> >
> > “Roddy, Mark” wrote:
> > So I’m guessing that RUSTAS returns an error, right? Like dude,
> > where’s your buffer?
> >
> >
> > =====================
> > Mark Roddy
> >
> >
> > From: Alex Farber [mailto:xxxxx@yahoo.com]
> > Sent: Tuesday, Octo! ber 26, 2004 10:06 AM
> > To: Windows System Software Devs Interest List
> > Subject: [ntdev] RtlUnicodeStringToAnsiString problem
>
> > I am struggling with RtlUnicodeStringToAnsiString function. This
> is my
> code:
> >
> > // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> > ANSI_STRING ansi_string;
> > DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);
> >
> > RtlInitAnsiString(&ansi_string, NULL);
> > nLength = ansi_string.Length;
> > DbgPrint(“ANSI string length before conversion: %d\n”, nLength);
> >
> > RtlUnicodeStringToAnsiString(&ansi_string,
> &pDevExt->ustrDeviceName.
> > uStr, FALSE);
> > nLength = ansi_string.Length;
> > DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
> > DbgPrint(“ANSI string after conversion:: %s\n”,
> ansi_string.Buffer);
> > Debug output is:
> > Source: \Device\LOOPBACK0
> > ANSI string length before conversion: 0> ANSI string length after
> conversion: 17
> > ANSI string after conversion:: (null)
> > Destination: (null)
> > What is wrong in this code? ansi_string length is OK, but buffer
> is
> empty.
> > Do you Yahoo!?
> > Y! Messenger - Communicate in real time. Download now. —
> > Questions? First check the Kernel Driver FAQ a! t http://www.
> > osronline.com/article.cfm?id=256 You are currently subscribed to
> > ntdev as: xxxxx@stratus.com To unsubscribe send a blank email
> > to xxxxx@lists.osr.com
> > Questions? First check the Kernel Driver FAQ at http://www.
> > osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as: xxxxx@yahoo.com
> > To unsubscribe send a blank email to
> xxxxx@lists.osr.com
> >
__________________
> > Do You Yahoo!?
> > Tired of spam? Yahoo! Mail has the best spam protection around
> > http://mail.yahoo.com — Questions? First check the Kernel Driver
> FAQ at
>
> > http://www.osronline.com/article.cfm?id=256 You are currently
> > subscribed to ntdev as: xxxxx@3dlabs.com To unsubscribe
> > send a blank email to xxxxx@lists.osr.com
> > ForwardSourceID:NT000060F2
>
>
>
> —
> Questions? F! irst check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@yahoo.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>
>
>
> Do you Yahoo!?
> Yahoo! Mail Address AutoComplete
>
> w_mail/static/ease.html> - You start. We finish. — Questions?
> First check
> the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
> You are
> currently subscribed to ntdev as: xxxxx@stratus.com To
> unsubscribe send
> a blank email to xxxxx@lists.osr.com
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@yahoo.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>
>

>
> Do you Yahoo!?
> Yahoo! Mail Address AutoComplete
>
http:
> w_mail/static/ease.html> - You start. We finish. — Questions? First
check
> the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256 You
are
> currently subscribed to ntdev as: xxxxx@stratus.com To unsubscribe
send
> a blank email to xxxxx@lists.osr.com
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@creo.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>
> —
> Questions? First check the Kernel Driver FAQ at http://www.
> osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@3dlabs.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com

> ForwardSourceID:NT00006162


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@creo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com</http:>

Well I was going to proclaim that it would just crash and burn, but at least
on w2k3 it happens to be safe to call RtlFreeAnsiString after
RtlUnicodeStringToAnsiString fails. Relying on this behavior is rather
foolish.

=====================
Mark Roddy

-----Original Message-----
From: Alexander Krol [mailto:xxxxx@creo.com]
Sent: Tuesday, October 26, 2004 12:44 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

If RUSTAS doesn’t return success, one should not touch the damned stuff,
certainly - since nowhere it is documented that it will be NULL. On the
other side, standard deallocating routines safely accept NULL, and, though
it is not documented, I would be very surprised if RtlFreeXXXString would
duffer from the bunch.

-----Original Message-----
From: Mats PETERSSON [mailto:xxxxx@3dlabs.com]
Sent: Tuesday, October 26, 2004 6:33 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

xxxxx@lists.osr.com wrote on 10/26/2004 05:18:26 PM:

Buffer is allocated by RtlUnicodeStringToAnsiString because of TRUE flag.
Not the clearest of possible APIs, certainly - but documented.

I think what Mark was referring to, and what I pointed out in my excerpt
from the RUSTAS Doc’s, that if the call doesn’t return Succes, you didn’t
get any allocation, and thus YOU SHOULD NOT FREE it. Because you’ll either
try to free NULL, or worse, some random value. Freeing something that hasn’t
been allocated is a bad idea.

Now it may be OK to try to free a NULL, becasue from memory from looking at
the RtlFreeXXXString, it looked like it was checking for NULL and skipping
out. But I wouldn’t rely on such behaviour, since it may change in future
code (unless it’s specified that you can call RtlFreeXXXString with NULL in
the Doc’s). Also relying on a “Failed allocation” to be NULL is probably a
bit doubtful.

So, to keep the code clean and reliable, only free if it was a success.


Mats

-----Original Message-----
From: Roddy, Mark [mailto:xxxxx@stratus.com]
Sent: Tuesday, October 26, 2004 6:12 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Uh - you might want to rethink that code segment. Freeing unallocated
buffers generally has dire results.

=====================
Mark Roddy


From: Alex Farber [mailto:xxxxx@yahoo.com]
Sent: Tuesday, October 26, 2004 11:40 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Thanks for the help, my final code is:

ANSI_STRING ansi_string;
RtlInitAnsiString(&ansi_string, NULL);

status = RtlUnicodeStringToAnsiString(&ansi_string,
&pDevExt->ustrDeviceName.uStr, TRUE);

if ( status == STATUS_SUCCESS )
{

}

RtlFreeAnsiString(&ansi_string);


pIrp->IoStatus.Status = status;
return status;

“Roddy, Mark” wrote:
>
> You should of course check the returned status value from RUSTAS
> and deal
> with error cases regardless of when where and how you allocate the
> buffer.
>
>
>
> =====================
> Mark Roddy
>
>
>
>
>
>
> From: Alex Farber [mailto:xxxxx@yahoo.com]
> Sent: Tuesday, October 26, 2004 11:17 AM
> To: Windows System Software Devs Interest List
> Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem
>
>
> Thanks.
>
> Mats PETERSSON wrote:
>
>
>
>
>
>
> Alex,
>
> That would work, I suppose, but why not just pass in TRUE as the
> last
> parameter, and RUSTAS will do it for you automatically. That is what
> the
> last parameter is there for…
>
> Unless of course, you have some special reason to call
> ExAllocatePool
> specificially (to set some special mode! of the pool parameters for
> instance).
>
> I think, if you’re using your own allocation, you should free it
> yourself
> too, as the (looking at the code in the debugger) RtlFreeXXXString
> uses an
> internal RtlHeapFree which isn’t the same as ExFreePool…
>
> So if you allocate it, you free it. If RtlXXX allocates it, RtlFree
> can be
> used to free it.
>
> –
> Mats
>
> xxxxx@lists.osr.com wrote on 10/26/2004 03:36:48 PM:
>
> > It looks like problem is solved by changing the code by this way:
> >
> > // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> > ANSI_STRING ansi_string;
> > ansi_string.MaximumLength = (USHORT)
> pDevExt->ustrDeviceName.uStr.Length;
> > ansi_string.Buffer = (PSTR)ExAllocatePool(PagedPool, ansi_string.
> > MaximumLength);
> > RtlUnicodeStringToAnsiString(&ansi_string,
> &pDevExt->ustrDeviceName.
> > uStr, FALSE);
> > …
> > RtlFreeAnsiString(&ansi_string);
> > I works but I am not sure that RtlFreeAnsiString releases memory
> > correct after such allocation. Is this OK to allocate ansi_string.
> > Buffer with ExAllocatePool and call RtlFreeAnsiString for
> clean-up?
> >
> > “Roddy, Mark” wrote:
> > So I’m guessing that RUSTAS returns an error, right? Like dude,
> > where’s your buffer?
> >
> >
> > =====================
> > Mark Roddy
> >
> >
> > From: Alex Farber [mailto:xxxxx@yahoo.com]
> > Sent: Tuesday, Octo! ber 26, 2004 10:06 AM
> > To: Windows System Software Devs Interest List
> > Subject: [ntdev] RtlUnicodeStringToAnsiString problem
>
> > I am struggling with RtlUnicodeStringToAnsiString function. This
> is my
> code:
> >
> > // pDevExt->ustrDeviceName.uStr is valid UNICODE_STRING
> > ANSI_STRING ansi_string;
> > DbgPrint(“Source: %ws\n”,pDevExt->ustrDeviceName.uStr.Buffer);
> >
> > RtlInitAnsiString(&ansi_string, NULL);
> > nLength = ansi_string.Length;
> > DbgPrint(“ANSI string length before conversion: %d\n”, nLength);
> >
> > RtlUnicodeStringToAnsiString(&ansi_string,
> &pDevExt->ustrDeviceName.
> > uStr, FALSE);
> > nLength = ansi_string.Length;
> > DbgPrint(“ANSI string length after conversion: %d\n”, nLength);
> > DbgPrint(“ANSI string after conversion:: %s\n”,
> ansi_string.Buffer);
> > Debug output is:
> > Source: \Device\LOOPBACK0
> > ANSI string length before conversion: 0> ANSI string length after
> conversion: 17
> > ANSI string after conversion:: (null)
> > Destination: (null)
> > What is wrong in this code? ansi_string length is OK, but buffer
> is
> empty.
> > Do you Yahoo!?
> > Y! Messenger - Communicate in real time. Download now. —
> > Questions? First check the Kernel Driver FAQ a! t http://www.
> > osronline.com/article.cfm?id=256 You are currently subscribed to
> > ntdev as: xxxxx@stratus.com To unsubscribe send a blank email
> > to xxxxx@lists.osr.com
> > Questions? First check the Kernel Driver FAQ at http://www.
> > osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as: xxxxx@yahoo.com
> > To unsubscribe send a blank email to
> xxxxx@lists.osr.com
> >
__________________
> > Do You Yahoo!?
> > Tired of spam? Yahoo! Mail has the best spam protection around
> > http://mail.yahoo.com — Questions? First check the Kernel Driver
> FAQ at
>
> > http://www.osronline.com/article.cfm?id=256 You are currently
> > subscribed to ntdev as: xxxxx@3dlabs.com To unsubscribe
> > send a blank email to xxxxx@lists.osr.com
> > ForwardSourceID:NT000060F2
>
>
>
> —
> Questions? F! irst check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@yahoo.com
> To unsubscribe send a blank email to
> xxxxx@lists.osr.com
>
>
>
>
> Do you Yahoo!?
> Yahoo! Mail Address AutoComplete
>
> w_mail/static/ease.html> - You start. We finish. — Questions?
> First check
> the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
> You are
> currently subscribed to ntdev as: xxxxx@stratus.com To
> unsubscribe send
> a blank email to xxxxx@lists.osr.com
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@yahoo.com
> To unsubscribe send a blank email to
> xxxxx@lists.osr.com
>
>
>

>
> Do you Yahoo!?
> Yahoo! Mail Address AutoComplete
>
http:
> w_mail/static/ease.html> - You start. We finish. — Questions? First
check
> the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
> You
are
> currently subscribed to ntdev as: xxxxx@stratus.com To
> unsubscribe
send
> a blank email to xxxxx@lists.osr.com
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@creo.com To
> unsubscribe send a blank email to xxxxx@lists.osr.com
>
>
> —
> Questions? First check the Kernel Driver FAQ at http://www.
> osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@3dlabs.com To
> unsubscribe send a blank email to xxxxx@lists.osr.com

> ForwardSourceID:NT00006162


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@creo.com To
unsubscribe send a blank email to xxxxx@lists.osr.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@stratus.com To
unsubscribe send a blank email to xxxxx@lists.osr.com</http:>

Returning NULL pointer on failed allocation is C idiom; ditto safely feeding
that same pointer to deallocation function (see both C and C++ standards).
Is this such a big deal to ask for this behavior to be legal and documented
in RTLXXX functions and in DDK environment in general?

-----Original Message-----
From: Roddy, Mark [mailto:xxxxx@stratus.com]
Sent: Tuesday, October 26, 2004 6:57 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Well I was going to proclaim that it would just crash and burn, but at least
on w2k3 it happens to be safe to call RtlFreeAnsiString after
RtlUnicodeStringToAnsiString fails. Relying on this behavior is rather
foolish.

=====================
Mark Roddy

-----Original Message-----
From: Alexander Krol [mailto:xxxxx@creo.com]
Sent: Tuesday, October 26, 2004 12:44 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

If RUSTAS doesn’t return success, one should not touch the damned stuff,
certainly - since nowhere it is documented that it will be NULL. On the
other side, standard deallocating routines safely accept NULL, and, though
it is not documented, I would be very surprised if RtlFreeXXXString would
duffer from the bunch.

-----Original Message-----
From: Mats PETERSSON [mailto:xxxxx@3dlabs.com]
Sent: Tuesday, October 26, 2004 6:33 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

xxxxx@lists.osr.com wrote on 10/26/2004 05:18:26 PM:

Buffer is allocated by RtlUnicodeStringToAnsiString because of TRUE flag.
Not the clearest of possible APIs, certainly - but documented.

I think what Mark was referring to, and what I pointed out in my excerpt
from the RUSTAS Doc’s, that if the call doesn’t return Succes, you didn’t
get any allocation, and thus YOU SHOULD NOT FREE it. Because you’ll either
try to free NULL, or worse, some random value. Freeing something that hasn’t
been allocated is a bad idea.

Now it may be OK to try to free a NULL, becasue from memory from looking at
the RtlFreeXXXString, it looked like it was checking for NULL and skipping
out. But I wouldn’t rely on such behaviour, since it may change in future
code (unless it’s specified that you can call RtlFreeXXXString with NULL in
the Doc’s). Also relying on a “Failed allocation” to be NULL is probably a
bit doubtful.

So, to keep the code clean and reliable, only free if it was a success.


Mats

Oh I agree that this should be the documented behavior of all varieties of
delete in the kernel, however it isn’t.

=====================
Mark Roddy

-----Original Message-----
From: Alexander Krol [mailto:xxxxx@creo.com]
Sent: Wednesday, October 27, 2004 3:55 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Returning NULL pointer on failed allocation is C idiom; ditto safely feeding
that same pointer to deallocation function (see both C and C++ standards).
Is this such a big deal to ask for this behavior to be legal and documented
in RTLXXX functions and in DDK environment in general?

-----Original Message-----
From: Roddy, Mark [mailto:xxxxx@stratus.com]
Sent: Tuesday, October 26, 2004 6:57 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

Well I was going to proclaim that it would just crash and burn, but at least
on w2k3 it happens to be safe to call RtlFreeAnsiString after
RtlUnicodeStringToAnsiString fails. Relying on this behavior is rather
foolish.

=====================
Mark Roddy

-----Original Message-----
From: Alexander Krol [mailto:xxxxx@creo.com]
Sent: Tuesday, October 26, 2004 12:44 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

If RUSTAS doesn’t return success, one should not touch the damned stuff,
certainly - since nowhere it is documented that it will be NULL. On the
other side, standard deallocating routines safely accept NULL, and, though
it is not documented, I would be very surprised if RtlFreeXXXString would
duffer from the bunch.

-----Original Message-----
From: Mats PETERSSON [mailto:xxxxx@3dlabs.com]
Sent: Tuesday, October 26, 2004 6:33 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] RtlUnicodeStringToAnsiString problem

xxxxx@lists.osr.com wrote on 10/26/2004 05:18:26 PM:

Buffer is allocated by RtlUnicodeStringToAnsiString because of TRUE flag.
Not the clearest of possible APIs, certainly - but documented.

I think what Mark was referring to, and what I pointed out in my excerpt
from the RUSTAS Doc’s, that if the call doesn’t return Succes, you didn’t
get any allocation, and thus YOU SHOULD NOT FREE it. Because you’ll either
try to free NULL, or worse, some random value. Freeing something that hasn’t
been allocated is a bad idea.

Now it may be OK to try to free a NULL, becasue from memory from looking at
the RtlFreeXXXString, it looked like it was checking for NULL and skipping
out. But I wouldn’t rely on such behaviour, since it may change in future
code (unless it’s specified that you can call RtlFreeXXXString with NULL in
the Doc’s). Also relying on a “Failed allocation” to be NULL is probably a
bit doubtful.

So, to keep the code clean and reliable, only free if it was a success.


Mats


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@stratus.com To
unsubscribe send a blank email to xxxxx@lists.osr.com