converting char to unicode strings

I am having trouble understanding the DDK
documentation in regards to UNICODE_STRINGS.

i am writing a driver that writes an output file.

instead of having the name of the file hard-coded
inside the driver source code, I would like to send it
a file name from a user application.

using buffered I/O, i would something like the
following in my test_app in user mode…

unsigned char FileInBuffer = “\??\c:\test.txt”;

and then do buffered I/O using DeviceIoControl

IoctlResult = DeviceIoControl(hTest,
(unsigned long)IOCTL_WRITE_FILE,
&FileInBuffer,
sizeof(FileInBuffer),
NULL,
0,
&ReturnedLength,
NULL);

and in my IO DeviceControl function in the driver
source code i have…

pBuffer = (UCHAR *)Irp->AssociatedIrp.SystemBuffer;

i would like to convert pBuffer to a unicode string,
so that I can do something like this before I create
my file object attributes using
InitializeObjectAttributes…

RtlInitUnicodeString(myObjectName, pBuffer_US);

where myObjectName is declared as…
UNICODE_STRING myObjectName;

and pBuffer_US is the equivalent of
L"\??\c:\test.txt"

instead of the normal…
RtlInitUnicodeStringmyObjectName,
L"\??\c:\test.txt");
…which works fine in my driver

thanks in advance for any assistance
-SA


Do you Yahoo!?
Send a seasonal email greeting and help others. Do good.
http://celebrity.mail.yahoo.com

Use:

ANSI_STRING AS;
UNICODE_STRING US;

RtlInitAnsiString(&AS, pBuffer);
RtlAnsiStringToUnicodeString(&US, &AS, TRUE);

RtlFreeUnicodeString(&US);


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Remove StopSpam from the email to reply

“Steven Aumack” wrote in message news:xxxxx@ntdev…
> I am having trouble understanding the DDK
> documentation in regards to UNICODE_STRINGS.
>
> i am writing a driver that writes an output file.
>
> instead of having the name of the file hard-coded
> inside the driver source code, I would like to send it
> a file name from a user application.
>
> using buffered I/O, i would something like the
> following in my test_app in user mode…
>
> unsigned char FileInBuffer = “\??\c:\test.txt”;
>
> and then do buffered I/O using DeviceIoControl
>
> IoctlResult = DeviceIoControl(hTest,
> (unsigned long)IOCTL_WRITE_FILE,
> &FileInBuffer,
> sizeof(FileInBuffer),
> NULL,
> 0,
> &ReturnedLength,
> NULL);
>
> and in my IO DeviceControl function in the driver
> source code i have…
>
> pBuffer = (UCHAR *)Irp->AssociatedIrp.SystemBuffer;
>
> i would like to convert pBuffer to a unicode string,
> so that I can do something like this before I create
> my file object attributes using
> InitializeObjectAttributes…
>
> RtlInitUnicodeString(myObjectName, pBuffer_US);
>
> where myObjectName is declared as…
> UNICODE_STRING myObjectName;
>
> and pBuffer_US is the equivalent of
> L"\??\c:\test.txt"
>
> instead of the normal…
> RtlInitUnicodeStringmyObjectName,
> L"\??\c:\test.txt");
> …which works fine in my driver
>
>
> thanks in advance for any assistance
> -SA
>
>
>
> __________________________________
> Do you Yahoo!?
> Send a seasonal email greeting and help others. Do good.
> http://celebrity.mail.yahoo.com
>

Design the IOCTL interface to use Unicode and do the conversion in user
mode. The lesser is the kernel-mode code - the better.

Also it is a good idea to use Unicode only in the apps too :), unless you
need UNIX portability.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

----- Original Message -----
From: “Steven Aumack”
To: “Windows System Software Devs Interest List”
Sent: Tuesday, December 21, 2004 12:32 AM
Subject: [ntdev] converting char to unicode strings

> I am having trouble understanding the DDK
> documentation in regards to UNICODE_STRINGS.
>
> i am writing a driver that writes an output file.
>
> instead of having the name of the file hard-coded
> inside the driver source code, I would like to send it
> a file name from a user application.
>
> using buffered I/O, i would something like the
> following in my test_app in user mode…
>
> unsigned char FileInBuffer = “\??\c:\test.txt”;
>
> and then do buffered I/O using DeviceIoControl
>
> IoctlResult = DeviceIoControl(hTest,
> (unsigned long)IOCTL_WRITE_FILE,
> &FileInBuffer,
> sizeof(FileInBuffer),
> NULL,
> 0,
> &ReturnedLength,
> NULL);
>
> and in my IO DeviceControl function in the driver
> source code i have…
>
> pBuffer = (UCHAR *)Irp->AssociatedIrp.SystemBuffer;
>
> i would like to convert pBuffer to a unicode string,
> so that I can do something like this before I create
> my file object attributes using
> InitializeObjectAttributes…
>
> RtlInitUnicodeString(myObjectName, pBuffer_US);
>
> where myObjectName is declared as…
> UNICODE_STRING myObjectName;
>
> and pBuffer_US is the equivalent of
> L"\??\c:\test.txt"
>
> instead of the normal…
> RtlInitUnicodeStringmyObjectName,
> L"\??\c:\test.txt");
> …which works fine in my driver
>
>
> thanks in advance for any assistance
> -SA
>
>
>
> __________________________________
> Do you Yahoo!?
> Send a seasonal email greeting and help others. Do good.
> http://celebrity.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@storagecraft.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>

why wouldn’t your I/O control just take a unicode string in the first
place rather than forcing the driver to do the string conversion?

You can initialize an ANSI string using the UCHAR array you get from
your client application, then call RtlAnsiStringToUnicodeString in order
to do the conversion. Of course you first need to check the string
you’re given to ensure it’s null terminated and not longer than the
InputBufferLength of the I/O control.

To avoid security problems you either need to create the file in the I/O
control dispatch routine, that you specify the IO_FORCE_ACCESS_CHECK
flag to IoCreateFile, and that you set OBJ_KERNEL_HANDLE in the object
attributes so that you don’t end up letting an arbitrary application
open your device driver and overwrite any file on the disk.

Why does your driver write to a file in the first place? Is this for
logging (if so you could look into event-tracing for windows)? Is this
it’s method of outputting data (if so perhaps you could use ReadFile)?

-p

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Steven Aumack
Sent: Monday, December 20, 2004 1:32 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] converting char to unicode strings

I am having trouble understanding the DDK documentation in
regards to UNICODE_STRINGS.

i am writing a driver that writes an output file.

instead of having the name of the file hard-coded inside the
driver source code, I would like to send it a file name from
a user application.

using buffered I/O, i would something like the following in
my test_app in user mode…

unsigned char FileInBuffer = “\??\c:\test.txt”;

and then do buffered I/O using DeviceIoControl

IoctlResult = DeviceIoControl(hTest,
(unsigned long)IOCTL_WRITE_FILE,
&FileInBuffer,
sizeof(FileInBuffer),
NULL,
0,
&ReturnedLength,
NULL);

and in my IO DeviceControl function in the driver source code
i have…

pBuffer = (UCHAR *)Irp->AssociatedIrp.SystemBuffer;

i would like to convert pBuffer to a unicode string, so that
I can do something like this before I create my file object
attributes using InitializeObjectAttributes…

RtlInitUnicodeString(myObjectName, pBuffer_US);

where myObjectName is declared as…
UNICODE_STRING myObjectName;

and pBuffer_US is the equivalent of
L"\??\c:\test.txt"

instead of the normal…
RtlInitUnicodeStringmyObjectName,
L"\??\c:\test.txt");
…which works fine in my driver

thanks in advance for any assistance
-SA


Do you Yahoo!?
Send a seasonal email greeting and help others. Do good.
http://celebrity.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@windows.microsoft.com To unsubscribe send a blank
email to xxxxx@lists.osr.com

RtlAnsiStringToUnicodeString uses the current system locale, while a user
mode app can be running in any locale. Therefore, you cannot safely convert
an arbitrary ansi string to unicode with RtlAnsiStringToUnicodeString.
However, this can be easily done with user mode MultiByteToWideChar.

You should pass only unicode strings to your driver, and ideally your app
should be unicode, too, as suggested above.

http://www.firestreamer.com - NTBackup to DVD and DV

“Steven Aumack” wrote in message news:xxxxx@ntdev…
>I am having trouble understanding the DDK
> documentation in regards to UNICODE_STRINGS.
>
> i am writing a driver that writes an output file.
>
> instead of having the name of the file hard-coded
> inside the driver source code, I would like to send it
> a file name from a user application.
>
> using buffered I/O, i would something like the
> following in my test_app in user mode…
>
> unsigned char FileInBuffer = “\??\c:\test.txt”;
>
> and then do buffered I/O using DeviceIoControl
>
> IoctlResult = DeviceIoControl(hTest,
> (unsigned long)IOCTL_WRITE_FILE,
> &FileInBuffer,
> sizeof(FileInBuffer),
> NULL,
> 0,
> &ReturnedLength,
> NULL);
>
> and in my IO DeviceControl function in the driver
> source code i have…
>
> pBuffer = (UCHAR *)Irp->AssociatedIrp.SystemBuffer;
>
> i would like to convert pBuffer to a unicode string,
> so that I can do something like this before I create
> my file object attributes using
> InitializeObjectAttributes…
>
> RtlInitUnicodeString(myObjectName, pBuffer_US);
>
> where myObjectName is declared as…
> UNICODE_STRING myObjectName;
>
> and pBuffer_US is the equivalent of
> L"\??\c:\test.txt"
>
> instead of the normal…
> RtlInitUnicodeStringmyObjectName,
> L"\??\c:\test.txt");
> …which works fine in my driver
>
>
> thanks in advance for any assistance
> -SA
>
>
>
> __________________________________
> Do you Yahoo!?
> Send a seasonal email greeting and help others. Do good.
> http://celebrity.mail.yahoo.com
>