Newbie Questions #2

1: Can I use try and catch to prevent KMode exceptions?

2: In a device driver (Kernel mode) is this the proper way of converting a char * string to a Unicode string?

STRING Name;
UNICODE_STRING DeviceName;
char tmp[128];

KdPrint((“Worm.sys:DriverEntry - RegPath:%s\n”, RegistryPath->Buffer));

sprintf(tmp, “\Device\Worm%d”, DriverCount++);
KdPrint((“Worm.sys:DriverEntry - tmp=%s\n”, tmp));
RtlInitString(&Name, tmp);
RtlAnsiStringToUnicodeString(&DeviceName, &Name, TRUE);
KdPrint((“Worm.sys:DriverEntry - DeviceName=%s\n”, DeviceName));

3: I’m using SysInternals DbgView to view my Debug output… But it “seem” that when turning on “Log Boot” it will Blue Screen my system with a:
STOP: 0x0000001E (0xC0000005, 0x8046231D, 0x00000000, 0x001C001A) KMODE_EXCEPTION NOT HANDLED

Thanks
Weston

  1. With try/except, the except clause won’t get control for some
    exceptions, eg, page fault at elevated IRQL. Pity: That would make some
    things a lot easier. For example, one could dispense with some buffer
    checking and detect the error when it arises; this makes for better
    performance in main, as opposed to error, paths.

  2. I’ve used this technique.


If replying by e-mail, please remove “nospam.” from the address.

James Antognini

  1. some, but some will get through. Also just catching the exception
    doesn’t magically mean the system is somehow saved. If you’ve got a
    stale pointer somewhere you’ve got a serious bug in your code. Rather
    than just glossing over it with exception handling you should probably
    try fixing it.

sorry to be cynical here but i’ve had to debug too many cases where
people thought an exception handler was the proper way to fix their
constant use of their own invalid pointers.

  1. or you can use wsprintf (or is it swprintf … i can never remember
    which one the kernel exports) to print directly to a WCHAR buffer and
    then call RtlInitUnicodeString. No memory allocation, no overhead of
    converting the string.

-p

-----Original Message-----
From: Weston Fryatt [mailto:xxxxx@muuf.com]
Sent: Tuesday, November 05, 2002 8:54 AM
To: NT Developers Interest List
Subject: [ntdev] Newbie Questions #2

1: Can I use try and catch to prevent KMode exceptions?

2: In a device driver (Kernel mode) is this the proper way of converting
a char * string to a Unicode string?

STRING Name;
UNICODE_STRING DeviceName;
char tmp[128];

KdPrint((“Worm.sys:DriverEntry - RegPath:%s\n”,
RegistryPath->Buffer));

sprintf(tmp, “\Device\Worm%d”, DriverCount++);
KdPrint((“Worm.sys:DriverEntry - tmp=%s\n”, tmp));
RtlInitString(&Name, tmp);
RtlAnsiStringToUnicodeString(&DeviceName, &Name, TRUE);
KdPrint((“Worm.sys:DriverEntry - DeviceName=%s\n”, DeviceName));

3: I’m using SysInternals DbgView to view my Debug output… But it
“seem” that when turning on “Log Boot” it will Blue Screen my system
with a:
STOP: 0x0000001E (0xC0000005, 0x8046231D, 0x00000000, 0x001C001A)
KMODE_EXCEPTION NOT HANDLED

Thanks
Weston


You are currently subscribed to ntdev as: xxxxx@microsoft.com
To unsubscribe send a blank email to %%email.unsub%%

Thanks…
Weston

----- Original Message -----
From: “Peter Wieland”
To: “NT Developers Interest List”
Sent: Tuesday, November 05, 2002 11:38 AM
Subject: [ntdev] RE: Newbie Questions #2

1) some, but some will get through. Also just catching the exception
doesn’t magically mean the system is somehow saved. If you’ve got a
stale pointer somewhere you’ve got a serious bug in your code. Rather
than just glossing over it with exception handling you should probably
try fixing it.

sorry to be cynical here but i’ve had to debug too many cases where
people thought an exception handler was the proper way to fix their
constant use of their own invalid pointers.

2) or you can use wsprintf (or is it swprintf … i can never remember
which one the kernel exports) to print directly to a WCHAR buffer and
then call RtlInitUnicodeString. No memory allocation, no overhead of
converting the string.

-p

-----Original Message-----
From: Weston Fryatt [mailto:xxxxx@muuf.com]
Sent: Tuesday, November 05, 2002 8:54 AM
To: NT Developers Interest List
Subject: [ntdev] Newbie Questions #2

1: Can I use try and catch to prevent KMode exceptions?

2: In a device driver (Kernel mode) is this the proper way of converting
a char * string to a Unicode string?

STRING Name;
UNICODE_STRING DeviceName;
char tmp[128];

KdPrint((“Worm.sys:DriverEntry - RegPath:%s\n”,
RegistryPath->Buffer));

sprintf(tmp, “\Device\Worm%d”, DriverCount++);
KdPrint((“Worm.sys:DriverEntry - tmp=%s\n”, tmp));
RtlInitString(&Name, tmp);
RtlAnsiStringToUnicodeString(&DeviceName, &Name, TRUE);
KdPrint((“Worm.sys:DriverEntry - DeviceName=%s\n”, DeviceName));

3: I’m using SysInternals DbgView to view my Debug output… But it
“seem” that when turning on “Log Boot” it will Blue Screen my system
with a:
STOP: 0x0000001E (0xC0000005, 0x8046231D, 0x00000000, 0x001C001A)
KMODE_EXCEPTION NOT HANDLED

Thanks
Weston


You are currently subscribed to ntdev as: xxxxx@microsoft.com
To unsubscribe send a blank email to %%email.unsub%%


You are currently subscribed to ntdev as: xxxxx@muuf.com
To unsubscribe send a blank email to %%email.unsub%%

Your C0000005 is certainly caused by the first and/or the third KdPrint.

The first should be :

KdPrint((“Worm.sys:DriverEntry - RegPath:%ws\n”, RegistryPath->Buffer));

The third should be :

KdPrint((“Worm.sys:DriverEntry - DeviceName=%wZ\n”, &DeviceName));

or

KdPrint((“Worm.sys:DriverEntry - DeviceName=%ws\n”, DeviceName->Buffer ));

When using %ws, assure that the “…->Buffer” is closed with
a terminating WCHAR(0) :

i.e :

  • DeviceName->Buffer[DeviceName->Length / sizeof(WCHAR)] = WCHAR(0) ;
  • DeviceName->MaximumLength >= DeviceName->Length + sizeof(WCHAR)

Just use “DbgPrint”. The output can be traced with “DbgView” from
SysInternals.

That’s exactly what I tried to focus with “assure …->Buffer” is closed with a terminating zero" since
one may not assume that this is the case. Developers have to know this, since there seems
not to exist a function that converts a unicode string to a multibyte wide character string ( or ??? ).
Thus developers have to allocate memory with size = unicode_string.Length + sizeof(WCHAR) , copy
the string from unicode_sting.Buffer into it AND terminate it with a wchar(0). I also assume that
developers may store themselves the terminating zero at the correct position within the
unicode_sting.Buffer if the unicode_sting.MaximumLength >= Length + sizoef(WCHAR). This avoids
unnecessary dynamic overhead. Many DDK functions do accept double-byte character strings
( e.g. " RtlQueryRegistryValues " ) and nothing else.

----- Original Message -----
From: “Wes Witt”
To: “Christiaan Ghijselinck” ; “Weston Fryatt”

Sent: Tuesday, November 05, 2002 8:10 PM
Subject: RE: [ntdev] Re: Newbie Questions #2

> Actually that is incorrect. You cannot, by definition, assume that a
> UNICODE_STRING or STRING buffer is NULL terminated — that is the whole
> point of the length field. You should always use the %wZ printf format
> string.
>
> -----Original Message-----
> From: Christiaan Ghijselinck [mailto:xxxxx@Compaqnet.be]
> Sent: Tuesday, November 05, 2002 10:47 AM
> To: NT Developers Interest List
> Subject: [ntdev] Re: Newbie Questions #2
>
>
>
>
> Your C0000005 is certainly caused by the first and/or the third KdPrint.
>
>
> The first should be :
>
> KdPrint((“Worm.sys:DriverEntry - RegPath:%ws\n”, RegistryPath->Buffer));
>
>
> The third should be :
>
> KdPrint((“Worm.sys:DriverEntry - DeviceName=%wZ\n”, &DeviceName));
>
> or
>
> KdPrint((“Worm.sys:DriverEntry - DeviceName=%ws\n”, DeviceName->Buffer
> ));
>
> When using %ws, assure that the “…->Buffer” is closed with
> a terminating WCHAR(0) :
>
> i.e :
> - DeviceName->Buffer[DeviceName->Length / sizeof(WCHAR)] = WCHAR(0) ;
> - DeviceName->MaximumLength >= DeviceName->Length + sizeof(WCHAR)
>
> —
> You are currently subscribed to ntdev as: xxxxx@microsoft.com
> To unsubscribe send a blank email to %%email.unsub%%
>

Or better:

UNICODE_STRING DeviceName;
WCHAR tmp[128];

wsprintf(tmp, L"\Device\Worm%d", DriverCount++);
RtlInitUnicodeString(&DeviceName, tmp);

Max

----- Original Message -----
From: Weston Fryatt
To: NT Developers Interest List
Sent: Tuesday, November 05, 2002 7:54 PM
Subject: [ntdev] Newbie Questions #2

1: Can I use try and catch to prevent KMode exceptions?

2: In a device driver (Kernel mode) is this the proper way of converting a char * string to a Unicode string?

STRING Name;
UNICODE_STRING DeviceName;
char tmp[128];

KdPrint((“Worm.sys:DriverEntry - RegPath:%s\n”, RegistryPath->Buffer));

sprintf(tmp, “\Device\Worm%d”, DriverCount++);
KdPrint((“Worm.sys:DriverEntry - tmp=%s\n”, tmp));
RtlInitString(&Name, tmp);
RtlAnsiStringToUnicodeString(&DeviceName, &Name, TRUE);
KdPrint((“Worm.sys:DriverEntry - DeviceName=%s\n”, DeviceName));

3: I’m using SysInternals DbgView to view my Debug output… But it “seem” that when turning on “Log Boot” it will Blue Screen my system with a:
STOP: 0x0000001E (0xC0000005, 0x8046231D, 0x00000000, 0x001C001A) KMODE_EXCEPTION NOT HANDLED

Thanks
Weston


You are currently subscribed to ntdev as: xxxxx@storagecraft.com
To unsubscribe send a blank email to %%email.unsub%%

This works, although that big local array should probably be allocated
dynamically to save stack space.

  • Nicholas Ryan

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Maxim S. Shatskih
Sent: Wednesday, November 06, 2002 12:35 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Newbie Questions #2

Or better:

UNICODE_STRING DeviceName;
WCHAR tmp[128];

wsprintf(tmp, L" <file:> \Device\Worm%d",
DriverCount++);
RtlInitUnicodeString(&DeviceName, tmp);

Max

----- Original Message -----
From: Weston Fryatt mailto:xxxxx
To: NT Developers Interest List mailto:xxxxx
Sent: Tuesday, November 05, 2002 7:54 PM
Subject: [ntdev] Newbie Questions #2

1: Can I use try and catch to prevent KMode exceptions?

2: In a device driver (Kernel mode) is this the proper way of converting
a char * string to a Unicode string?

STRING Name;
UNICODE_STRING DeviceName;
char tmp[128];

KdPrint((“Worm.sys:DriverEntry - RegPath:%s\n”,
RegistryPath->Buffer));

sprintf(tmp, “\Device\Worm%d”, DriverCount++);
KdPrint((“Worm.sys:DriverEntry - tmp=%s\n”, tmp));
RtlInitString(&Name, tmp);
RtlAnsiStringToUnicodeString(&DeviceName, &Name, TRUE);
KdPrint((“Worm.sys:DriverEntry - DeviceName=%s\n”, DeviceName));

3: I’m using SysInternals DbgView to view my Debug output… But it
“seem” that when turning on “Log Boot” it will Blue Screen my system
with a:
STOP: 0x0000001E (0xC0000005, 0x8046231D, 0x00000000, 0x001C001A)
KMODE_EXCEPTION NOT HANDLED

Thanks
Weston


You are currently subscribed to ntdev as: xxxxx@storagecraft.com
To unsubscribe send a blank email to %%email.unsub%%


You are currently subscribed to ntdev as: xxxxx@nryan.com
To unsubscribe send a blank email to %%email.unsub%%</mailto:xxxxx></mailto:xxxxx></file:>