Programmatically (from kernel mode) accessing pathname containing %WINDIR%

In my KMDF driver D0Entry routine, I want to read a file in order to do firmware download to my device. I am using ZwCreateFile() to open the file. I am packaging the firmware file with the .sys file, so it lives in the \System32\Drivers directory.

My question is how to specify a fully-qualified file name which contains a macro expansion like %WINDIR%. The pathname string being supplied to ZwCreateFile() looks like “\DosDevices\%WINDIR%\System32\Drivers\MyDevice.hex”. This fails. Is there a way to programmatically get the expanded string first?

On Mon, Apr 21, 2008 at 05:59:00PM -0400, xxxxx@waters.com wrote:

My question is how to specify a fully-qualified file name which
contains a macro expansion like %WINDIR%. The pathname string being
supplied to ZwCreateFile() looks like
“\DosDevices\%WINDIR%\System32\Drivers\MyDevice.hex”. This fails.
Is there a way to programmatically get the expanded string first?

You can use “\SystemRoot.…”, assuming your driver is loaded late
enough in the boot process. Environment variables are a per-process
user-mode concept.

xxxxx@waters.com wrote:

In my KMDF driver D0Entry routine, I want to read a file in order to do firmware download to my device. I am using ZwCreateFile() to open the file. I am packaging the firmware file with the .sys file, so it lives in the \System32\Drivers directory.

My question is how to specify a fully-qualified file name which contains a macro expansion like %WINDIR%. The pathname string being supplied to ZwCreateFile() looks like “\DosDevices\%WINDIR%\System32\Drivers\MyDevice.hex”. This fails. Is there a way to programmatically get the expanded string first?

The environment is strictly a user-mode concept. There is no equivalent
in the kernel.

Fortunately, you don’t need it. The kernel namespace has an alias
called \SystemRoot that points to the Windows directory. So, just use
“\SystemRoot\System32\Drivers\MyDevice.hex”


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

\SystemRoot will get you to %windir%, but there are 2 things that you must account for

  1. do you need to restore firmware everytime the device loses power? If so, D0Entry is the correct spot to do this. If not and it is only across reboots that you must download firmware, PrepareHarware is the right spot for this action

  2. the FS may not be up yet if your device is started during boot

d

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@waters.com
Sent: Monday, April 21, 2008 2:59 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Programmatically (from kernel mode) accessing pathname containing %WINDIR%

In my KMDF driver D0Entry routine, I want to read a file in order to do firmware download to my device. I am using ZwCreateFile() to open the file. I am packaging the firmware file with the .sys file, so it lives in the \System32\Drivers directory.

My question is how to specify a fully-qualified file name which contains a macro expansion like %WINDIR%. The pathname string being supplied to ZwCreateFile() looks like “\DosDevices\%WINDIR%\System32\Drivers\MyDevice.hex”. This fails. Is there a way to programmatically get the expanded string first?


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

I recommend you use the path \SystemRoot\System32\Drivers instead. The
symbolic link \SystemRoot in ‘object manager’ space resolves to the place
you are trying to get to with %WINDIR% and is far more friendly to kernel
mode programming.

Good Luck,
-dave


From:
Sent: Monday, April 21, 2008 5:59 PM
To: “Windows System Software Devs Interest List”
Subject: [ntdev] Programmatically (from kernel mode) accessing pathname
containing %WINDIR%

> In my KMDF driver D0Entry routine, I want to read a file in order to do
> firmware download to my device. I am using ZwCreateFile() to open the
> file. I am packaging the firmware file with the .sys file, so it lives in
> the \System32\Drivers directory.
>
> My question is how to specify a fully-qualified file name which contains a
> macro expansion like %WINDIR%. The pathname string being supplied to
> ZwCreateFile() looks like
> “\DosDevices\%WINDIR%\System32\Drivers\MyDevice.hex”. This fails.
> Is there a way to programmatically get the expanded string first?
>
> —
> 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
>

Thanks, everyone, for your timely replies. Using \SystemRoot works great.