Is there a good example of how to do this? I’m updating a driver that is taking in from an external source a char string. I’m not sure if this is
correct, but this is what I have come up with:
ANSI_STRING Ansi;
ULONG size;
RtlInitAnsiString( &Ansi, pProgBoard->ProgChip[i].ChipHexFilename );
size = RtlAnsiStringToUnicodeSize( &Ansi );
if ( devContext->ChipHexFilename[i].Buffer )
{
ExFreePool((PVOID) devContext->ChipHexFilename[i].Buffer );
}
devContext->ChipHexFilename[i].Buffer = AllocateBuffer( size + sizeof( WCHAR ) );
devContext->ChipHexFilename[i].MaximumLength = (USHORT) size + sizeof( WCHAR ) );
RtlAnsiStringToUnicaodeString( &( devContext->ChipHexFilename[i] ), &Ansi, FALSE );
devContext->ChipHexFilename[i].Buffer[devContext->ChipHexFilename[i].Length/sizeof( WCHAR )] = ‘\0’;
You don’t need to manually allocate your buffer, you can pass TRUE as the
third parameter to RtlAnsiStringToUnicodeString() and it will allocate the
buffer and update all the size members appropriately. You do need to free
the buffer when replacing it as you did in your code.
BTW, copy/paste your code, don’t retype it. You can unintentionally fix or
introduce bugs in the posted code.
Phil
Philip D. Barila??? (303) 776-1264
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Michael Wade
Sent: Thursday, December 23, 2010 12:53 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Unicode from char
Is there a good example of how to do this? I’m updating a driver that is
taking in from an external source a char string. I’m not sure if this is
correct, but this is what I have come up with:
ANSI_STRING Ansi;
ULONG size;
RtlInitAnsiString( &Ansi,
pProgBoard->ProgChip[i].ChipHexFilename );
size = RtlAnsiStringToUnicodeSize( &Ansi );
if ( devContext->ChipHexFilename[i].Buffer )
{
ExFreePool((PVOID) devContext->ChipHexFilename[i].Buffer
);
}
devContext->ChipHexFilename[i].Buffer = AllocateBuffer( size
- sizeof( WCHAR ) );
devContext->ChipHexFilename[i].MaximumLength = (USHORT) size
- sizeof( WCHAR ) );
RtlAnsiStringToUnicaodeString( &(
devContext->ChipHexFilename[i] ), &Ansi, FALSE );
devContext->ChipHexFilename[i].Buffer[devContext->ChipHexFilename[i].Length/
sizeof( WCHAR )] = ‘\0’;
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer
A few problems I see. First, you are freeing the Buffer then using
Ansi, but RtlInitAnsiString justs uses the buffer it is passed to set
the ANSI_STRING structure so once you free the Buffer you have an
invalid memory reference.
Second, are you really going to DMA the UNICODE string, if not you might
just consider:
RtlAnsiStringToUnicodeString( &( devContext->ChipHexFilename[i] ),
&Ansi, TRUE );
Of course this brings us to the third question/problem, namely what is
the logic of having a NULL terminator, it is not safe to assume that
UNICODE_STRINGS do, so what requirement are you addressing?
If you do need a null terminated wide character string, then you can up
the Length and MaxLength fields of Ansi.
Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr
“Michael Wade” wrote in message
news:xxxxx@ntdev:
> Is there a good example of how to do this? I’m updating a driver that is taking in from an external source a char string. I’m not sure if this is
> correct, but this is what I have come up with:
> ANSI_STRING Ansi;
> ULONG size;
> RtlInitAnsiString( &Ansi, pProgBoard->ProgChip[i].ChipHexFilename );
> size = RtlAnsiStringToUnicodeSize( &Ansi );
> if ( devContext->ChipHexFilename[i].Buffer )
> {
> ExFreePool((PVOID) devContext->ChipHexFilename[i].Buffer );
> }
>
> devContext->ChipHexFilename[i].Buffer = AllocateBuffer( size + sizeof( WCHAR ) );
> devContext->ChipHexFilename[i].MaximumLength = (USHORT) size + sizeof( WCHAR ) );
> RtlAnsiStringToUnicaodeString( &( devContext->ChipHexFilename[i] ), &Ansi, FALSE );
> devContext->ChipHexFilename[i].Buffer[devContext->ChipHexFilename[i].Length/sizeof( WCHAR )] = ‘\0’;