A giuide to writing device drivers in c++

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver.
I know about developing windows device drivers in c.

thanks

Robert Fernando
www.rowanclose.com


Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.583 / Virus Database: 369 - Release Date: 10/02/2004

DriverStudio.

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver.
I know about developing windows device drivers in c.

Back in 1996 there was an article in MSJ (Microsoft System Journal; Ah those
were the times … :)).
You can check it in http://www.microsoft.com/msj/backissues96.aspx.
A lot of thing has changed from then (in driver development too), but it
might be helpful as startup reading for “brain warming”.
WBR Primoz

-----Original Message-----
From: Robert Fernando -ntlworld [mailto:xxxxx@ntlworld.com]
Sent: Wednesday, February 11, 2004 12:00 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] A giuide to writing device drivers in c++

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver.
I know about developing windows device drivers in c.

thanks

Robert Fernando
www.rowanclose.com


Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.583 / Virus Database: 369 - Release Date: 10/02/2004


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

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

You have a long way to go baby!

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Robert Fernando
-ntlworld
Sent: Wednesday, February 11, 2004 3:00 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] A giuide to writing device drivers in c++

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver.
I know about developing windows device drivers in c.

thanks

Robert Fernando
www.rowanclose.com


Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.583 / Virus Database: 369 - Release Date: 10/02/2004


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

Sucks

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Loren Wilton
Sent: Wednesday, February 11, 2004 3:25 AM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] A giuide to writing device drivers in c++

DriverStudio.

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver.
I know about developing windows device drivers in c.


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

Seems like we will have get ready for yet another prime time :slight_smile::slight_smile:

Fight for 2 chars ( ++ :).

-prokash

-----Original Message-----
From: Jamey Kirby [mailto:xxxxx@storagecraft.com]
Sent: Wednesday, February 11, 2004 10:00 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Sucks

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Loren Wilton
Sent: Wednesday, February 11, 2004 3:25 AM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] A giuide to writing device drivers in c++

DriverStudio.

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver. I know about developing windows
device drivers in c.


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


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

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

Why ? Constructive criticism is always wanted, we’ll do our best to
incorporate it in our next releases.

Alberto.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Jamey Kirby
Sent: Wednesday, February 11, 2004 1:00 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Sucks

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Loren Wilton
Sent: Wednesday, February 11, 2004 3:25 AM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] A giuide to writing device drivers in c++

DriverStudio.

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver.
I know about developing windows device drivers in c.


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


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

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

The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it.

Hi Robert,

I think this problem can be split into two pieces:

  1. Difficulties using C++ in an environment that doesn’t support C++ fully.
  2. Designing driver code using good C++.

I can be of some help with 1:
You need to do a fair bit of work to get C++ to run in a driver environment.
The major, and obvious points, are new and delete functionality. You don’t
have a standard C runtime library, so you can’t just malloc/free to do
memory allocation (which is essentially the way new/delete works in a normal
application).

You can replace new/delete with your own “overrides”, which should solve
this.
void * __cdecl::operator new( unsigned int size )
{
void *ptr;

ptr = allocatememory(size);
assert(ptr);
return ptr;
}

void __cdecl ::operator delete( void *ptr ) {
if( ptr )
freememory(ptr);
}

How you implement allocatememory and freememory is another story… :wink:

You also can’t have static objects, because there is no “default
constructor”, so objects will not be constructed until you create them. This
means that this form won’t work:

class SomeClass x;
void func(…)
{
x.something = 7;
}

This is because x will not get constructed. You need to reconstruction with
a x as a pointer to SomeClass and then add
x = new SomeClass;
at “start of day”.

There’s a bunch of other support functions that are less obvious, like
“You’ve called a pure virtual function”.

int __cdecl _purecall( void )
{
assert( 0 );
}

I’m sure there are some other gotchas too, but these will certainly help
you.

You may also find that the stack space used is greater, due to the “ease”
with which you create objects and store more than you though on the stack.
The stack of a driver is only small.

I can’t really do much to explain #2 in the list above, simply because what
we use the C++ for is to simulate the next generation graphics chip, and
it’s linked into the driver as a chip simulator. We have severe problems
with for instance stack usage, but sidestep the issue by breaking the rules
and creating a second stack that we switch to before calling the simulator
with a “pretend to write this register” or “pretend you read this memory”.
(We also have our own multithreading in the C++ code, which doesn’t use
Windows threads, to simulate the parallelism of different units working on
the same clock-cycle in the chip).


Mats

-----Original Message-----
From: Robert Fernando -ntlworld [mailto:xxxxx@ntlworld.com]
Sent: Wednesday, February 11, 2004 11:00 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] A giuide to writing device drivers in c++

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver.
I know about developing windows device drivers in c.

thanks

Robert Fernando
www.rowanclose.com


Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.583 / Virus Database: 369 - Release Date: 10/02/2004


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

Do not ASSERT that alloc will return a valid pointer. Low resources are
comment and you must handle this in operator new and treat it as the
norm, not an exceptional case.

D

This posting is provided “AS IS” with no warranties, and confers no
rights

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@3Dlabs.com
Sent: Wednesday, February 11, 2004 10:42 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Hi Robert,

I think this problem can be split into two pieces:

  1. Difficulties using C++ in an environment that doesn’t support C++
    fully.
  2. Designing driver code using good C++.

I can be of some help with 1:
You need to do a fair bit of work to get C++ to run in a driver
environment.
The major, and obvious points, are new and delete functionality. You
don’t
have a standard C runtime library, so you can’t just malloc/free to do
memory allocation (which is essentially the way new/delete works in a
normal
application).

You can replace new/delete with your own “overrides”, which should solve
this.
void * __cdecl::operator new( unsigned int size )
{
void *ptr;

ptr = allocatememory(size);
assert(ptr);
return ptr;
}

void __cdecl ::operator delete( void *ptr ) {
if( ptr )
freememory(ptr);
}

How you implement allocatememory and freememory is another story… :wink:

You also can’t have static objects, because there is no “default
constructor”, so objects will not be constructed until you create them.
This
means that this form won’t work:

class SomeClass x;
void func(…)
{
x.something = 7;
}

This is because x will not get constructed. You need to reconstruction
with
a x as a pointer to SomeClass and then add
x = new SomeClass;
at “start of day”.

There’s a bunch of other support functions that are less obvious, like
“You’ve called a pure virtual function”.

int __cdecl _purecall( void )
{
assert( 0 );
}

I’m sure there are some other gotchas too, but these will certainly help
you.

You may also find that the stack space used is greater, due to the
“ease”
with which you create objects and store more than you though on the
stack.
The stack of a driver is only small.

I can’t really do much to explain #2 in the list above, simply because
what
we use the C++ for is to simulate the next generation graphics chip, and
it’s linked into the driver as a chip simulator. We have severe problems
with for instance stack usage, but sidestep the issue by breaking the
rules
and creating a second stack that we switch to before calling the
simulator
with a “pretend to write this register” or “pretend you read this
memory”.
(We also have our own multithreading in the C++ code, which doesn’t use
Windows threads, to simulate the parallelism of different units working
on
the same clock-cycle in the chip).


Mats

-----Original Message-----
From: Robert Fernando -ntlworld [mailto:xxxxx@ntlworld.com]
Sent: Wednesday, February 11, 2004 11:00 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] A giuide to writing device drivers in c++

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver.
I know about developing windows device drivers in c.

thanks

Robert Fernando
www.rowanclose.com


Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.583 / Virus Database: 369 - Release Date: 10/02/2004


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


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

Mat

Not to pointificate, but IIRC the stack frame will have only one extra
4-byte *this* ptr where it is implicit !!!

-prokash

-----Original Message-----
From: xxxxx@3Dlabs.com [mailto:xxxxx@3Dlabs.com]
Sent: Wednesday, February 11, 2004 10:42 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Hi Robert,

I think this problem can be split into two pieces:

  1. Difficulties using C++ in an environment that doesn’t support C++ fully.
  2. Designing driver code using good C++.

I can be of some help with 1:
You need to do a fair bit of work to get C++ to run in a driver environment.
The major, and obvious points, are new and delete functionality. You don’t
have a standard C runtime library, so you can’t just malloc/free to do
memory allocation (which is essentially the way new/delete works in a normal
application).

You can replace new/delete with your own “overrides”, which should solve
this.
void * __cdecl::operator new( unsigned int size )
{
void *ptr;

ptr = allocatememory(size);
assert(ptr);
return ptr;
}

void __cdecl ::operator delete( void *ptr ) {
if( ptr )
freememory(ptr);
}

How you implement allocatememory and freememory is another story… :wink:

You also can’t have static objects, because there is no “default
constructor”, so objects will not be constructed until you create them. This
means that this form won’t work:

class SomeClass x;
void func(…)
{
x.something = 7;
}

This is because x will not get constructed. You need to reconstruction with
a x as a pointer to SomeClass and then add
x = new SomeClass;
at “start of day”.

There’s a bunch of other support functions that are less obvious, like
“You’ve called a pure virtual function”.

int __cdecl _purecall( void )
{
assert( 0 );
}

I’m sure there are some other gotchas too, but these will certainly help
you.

You may also find that the stack space used is greater, due to the “ease”
with which you create objects and store more than you though on the stack.
The stack of a driver is only small.

I can’t really do much to explain #2 in the list above, simply because what
we use the C++ for is to simulate the next generation graphics chip, and
it’s linked into the driver as a chip simulator. We have severe problems
with for instance stack usage, but sidestep the issue by breaking the rules
and creating a second stack that we switch to before calling the simulator
with a “pretend to write this register” or “pretend you read this memory”.
(We also have our own multithreading in the C++ code, which doesn’t use
Windows threads, to simulate the parallelism of different units working on
the same clock-cycle in the chip).


Mats

-----Original Message-----
From: Robert Fernando -ntlworld [mailto:xxxxx@ntlworld.com]
Sent: Wednesday, February 11, 2004 11:00 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] A giuide to writing device drivers in c++

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver. I know about developing windows
device drivers in c.

thanks

Robert Fernando
www.rowanclose.com


Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.583 / Virus Database: 369 - Release Date: 10/02/2004


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


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

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

Because it promotes C++ in device drivers; no other reason…

Jamey

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Moreira, Alberto
Sent: Wednesday, February 11, 2004 10:26 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Why ? Constructive criticism is always wanted, we’ll do our best to
incorporate it in our next releases.

Alberto.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Jamey Kirby
Sent: Wednesday, February 11, 2004 1:00 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Sucks

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Loren Wilton
Sent: Wednesday, February 11, 2004 3:25 AM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] A giuide to writing device drivers in c++

DriverStudio.

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver.
I know about developing windows device drivers in c.


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


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

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

The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it.


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

Switching stacks in a driver… Please, some shoot me! Remind me again what
driver(s) we are talking about. I want to make sure that it/they NEVER
get(s) loaded on my system(s).

Jamey

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@3Dlabs.com
Sent: Wednesday, February 11, 2004 10:42 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Hi Robert,

I think this problem can be split into two pieces:

  1. Difficulties using C++ in an environment that doesn’t support C++ fully.
  2. Designing driver code using good C++.

I can be of some help with 1:
You need to do a fair bit of work to get C++ to run in a driver environment.
The major, and obvious points, are new and delete functionality. You don’t
have a standard C runtime library, so you can’t just malloc/free to do
memory allocation (which is essentially the way new/delete works in a normal
application).

You can replace new/delete with your own “overrides”, which should solve
this.
void * __cdecl::operator new( unsigned int size )
{
void *ptr;

ptr = allocatememory(size);
assert(ptr);
return ptr;
}

void __cdecl ::operator delete( void *ptr ) {
if( ptr )
freememory(ptr);
}

How you implement allocatememory and freememory is another story… :wink:

You also can’t have static objects, because there is no “default
constructor”, so objects will not be constructed until you create them. This
means that this form won’t work:

class SomeClass x;
void func(…)
{
x.something = 7;
}

This is because x will not get constructed. You need to reconstruction with
a x as a pointer to SomeClass and then add
x = new SomeClass;
at “start of day”.

There’s a bunch of other support functions that are less obvious, like
“You’ve called a pure virtual function”.

int __cdecl _purecall( void )
{
assert( 0 );
}

I’m sure there are some other gotchas too, but these will certainly help
you.

You may also find that the stack space used is greater, due to the “ease”
with which you create objects and store more than you though on the stack.
The stack of a driver is only small.

I can’t really do much to explain #2 in the list above, simply because what
we use the C++ for is to simulate the next generation graphics chip, and
it’s linked into the driver as a chip simulator. We have severe problems
with for instance stack usage, but sidestep the issue by breaking the rules
and creating a second stack that we switch to before calling the simulator
with a “pretend to write this register” or “pretend you read this memory”.
(We also have our own multithreading in the C++ code, which doesn’t use
Windows threads, to simulate the parallelism of different units working on
the same clock-cycle in the chip).


Mats

-----Original Message-----
From: Robert Fernando -ntlworld [mailto:xxxxx@ntlworld.com]
Sent: Wednesday, February 11, 2004 11:00 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] A giuide to writing device drivers in c++

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver.
I know about developing windows device drivers in c.

thanks

Robert Fernando
www.rowanclose.com


Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.583 / Virus Database: 369 - Release Date: 10/02/2004


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


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

Good point.

As I pointed out, we use this for a simulator of a new chip. It’s not
expected to run (or behave well) in “low memory situations”, so therefore,
the check for NULL there is the correct thing to do.

It is not so in a proper driver…

Apologies to all involved.


Mats

-----Original Message-----
From: Doron Holan [mailto:xxxxx@windows.microsoft.com]
Sent: Wednesday, February 11, 2004 6:56 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Do not ASSERT that alloc will return a valid pointer. Low
resources are
comment and you must handle this in operator new and treat it as the
norm, not an exceptional case.

D

This posting is provided “AS IS” with no warranties, and confers no
rights

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@3Dlabs.com
Sent: Wednesday, February 11, 2004 10:42 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Hi Robert,

I think this problem can be split into two pieces:

  1. Difficulties using C++ in an environment that doesn’t support C++
    fully.
  2. Designing driver code using good C++.

I can be of some help with 1:
You need to do a fair bit of work to get C++ to run in a driver
environment.
The major, and obvious points, are new and delete functionality. You
don’t
have a standard C runtime library, so you can’t just malloc/free to do
memory allocation (which is essentially the way new/delete works in a
normal
application).

You can replace new/delete with your own “overrides”, which
should solve
this.
void * __cdecl::operator new( unsigned int size )
{
void *ptr;

ptr = allocatememory(size);
assert(ptr);
return ptr;
}

void __cdecl ::operator delete( void *ptr ) {
if( ptr )
freememory(ptr);
}

How you implement allocatememory and freememory is another
story… :wink:

You also can’t have static objects, because there is no “default
constructor”, so objects will not be constructed until you
create them.
This
means that this form won’t work:

class SomeClass x;
void func(…)
{
x.something = 7;
}

This is because x will not get constructed. You need to reconstruction
with
a x as a pointer to SomeClass and then add
x = new SomeClass;
at “start of day”.

There’s a bunch of other support functions that are less obvious, like
“You’ve called a pure virtual function”.

int __cdecl _purecall( void )
{
assert( 0 );
}

I’m sure there are some other gotchas too, but these will
certainly help
you.

You may also find that the stack space used is greater, due to the
“ease”
with which you create objects and store more than you though on the
stack.
The stack of a driver is only small.

I can’t really do much to explain #2 in the list above, simply because
what
we use the C++ for is to simulate the next generation
graphics chip, and
it’s linked into the driver as a chip simulator. We have
severe problems
with for instance stack usage, but sidestep the issue by breaking the
rules
and creating a second stack that we switch to before calling the
simulator
with a “pretend to write this register” or “pretend you read this
memory”.
(We also have our own multithreading in the C++ code, which
doesn’t use
Windows threads, to simulate the parallelism of different
units working
on
the same clock-cycle in the chip).


Mats

> -----Original Message-----
> From: Robert Fernando -ntlworld
[mailto:xxxxx@ntlworld.com]
Sent: Wednesday, February 11, 2004 11:00 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] A giuide to writing device drivers in c++

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver.
I know about developing windows device drivers in c.

thanks

Robert Fernando
www.rowanclose.com


Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.583 / Virus Database: 369 - Release Date: 10/02/2004


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


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


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

Switching stacks ? How so ? And yet, if you want a driver to be really
secure, you don’t want it to run on someone else’s stack, not even the OS’s.
But pity, that’s of course not the way Windows was designed.

Alberto.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Jamey Kirby
Sent: Wednesday, February 11, 2004 2:06 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Switching stacks in a driver… Please, some shoot me! Remind me again what
driver(s) we are talking about. I want to make sure that it/they NEVER
get(s) loaded on my system(s).

Jamey

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@3Dlabs.com
Sent: Wednesday, February 11, 2004 10:42 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Hi Robert,

I think this problem can be split into two pieces:

  1. Difficulties using C++ in an environment that doesn’t support C++ fully.
  2. Designing driver code using good C++.

I can be of some help with 1:
You need to do a fair bit of work to get C++ to run in a driver environment.
The major, and obvious points, are new and delete functionality. You don’t
have a standard C runtime library, so you can’t just malloc/free to do
memory allocation (which is essentially the way new/delete works in a normal
application).

You can replace new/delete with your own “overrides”, which should solve
this.
void * __cdecl::operator new( unsigned int size )
{
void *ptr;

ptr = allocatememory(size);
assert(ptr);
return ptr;
}

void __cdecl ::operator delete( void *ptr ) {
if( ptr )
freememory(ptr);
}

How you implement allocatememory and freememory is another story… :wink:

You also can’t have static objects, because there is no “default
constructor”, so objects will not be constructed until you create them. This
means that this form won’t work:

class SomeClass x;
void func(…)
{
x.something = 7;
}

This is because x will not get constructed. You need to reconstruction with
a x as a pointer to SomeClass and then add
x = new SomeClass;
at “start of day”.

There’s a bunch of other support functions that are less obvious, like
“You’ve called a pure virtual function”.

int __cdecl _purecall( void )
{
assert( 0 );
}

I’m sure there are some other gotchas too, but these will certainly help
you.

You may also find that the stack space used is greater, due to the “ease”
with which you create objects and store more than you though on the stack.
The stack of a driver is only small.

I can’t really do much to explain #2 in the list above, simply because what
we use the C++ for is to simulate the next generation graphics chip, and
it’s linked into the driver as a chip simulator. We have severe problems
with for instance stack usage, but sidestep the issue by breaking the rules
and creating a second stack that we switch to before calling the simulator
with a “pretend to write this register” or “pretend you read this memory”.
(We also have our own multithreading in the C++ code, which doesn’t use
Windows threads, to simulate the parallelism of different units working on
the same clock-cycle in the chip).


Mats

-----Original Message-----
From: Robert Fernando -ntlworld [mailto:xxxxx@ntlworld.com]
Sent: Wednesday, February 11, 2004 11:00 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] A giuide to writing device drivers in c++

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver.
I know about developing windows device drivers in c.

thanks

Robert Fernando
www.rowanclose.com


Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.583 / Virus Database: 369 - Release Date: 10/02/2004


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


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


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

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

The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it.

DriverWorks in DriverStudio 3.1 has a C wizard !

Alberto.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Jamey Kirby
Sent: Wednesday, February 11, 2004 2:02 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Because it promotes C++ in device drivers; no other reason…

Jamey

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Moreira, Alberto
Sent: Wednesday, February 11, 2004 10:26 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Why ? Constructive criticism is always wanted, we’ll do our best to
incorporate it in our next releases.

Alberto.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Jamey Kirby
Sent: Wednesday, February 11, 2004 1:00 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Sucks

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Loren Wilton
Sent: Wednesday, February 11, 2004 3:25 AM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] A giuide to writing device drivers in c++

DriverStudio.

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver.
I know about developing windows device drivers in c.


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


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

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

The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it.


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


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

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

The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it.

Usually the this pointer is stuffed into ecx, not pushed onto the stack.
This has a sideaffect though that there is one less register to use in
the function so more stack space may be used later.

d

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Sinha, Prokash
Sent: Wednesday, February 11, 2004 10:59 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Mat

Not to pointificate, but IIRC the stack frame will have only one extra
4-byte *this* ptr where it is implicit !!!

-prokash

-----Original Message-----
From: xxxxx@3Dlabs.com [mailto:xxxxx@3Dlabs.com]
Sent: Wednesday, February 11, 2004 10:42 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Hi Robert,

I think this problem can be split into two pieces:

  1. Difficulties using C++ in an environment that doesn’t support C++
    fully.
  2. Designing driver code using good C++.

I can be of some help with 1:
You need to do a fair bit of work to get C++ to run in a driver
environment.
The major, and obvious points, are new and delete functionality. You
don’t
have a standard C runtime library, so you can’t just malloc/free to do
memory allocation (which is essentially the way new/delete works in a
normal
application).

You can replace new/delete with your own “overrides”, which should solve
this.
void * __cdecl::operator new( unsigned int size )
{
void *ptr;

ptr = allocatememory(size);
assert(ptr);
return ptr;
}

void __cdecl ::operator delete( void *ptr ) {
if( ptr )
freememory(ptr);
}

How you implement allocatememory and freememory is another story… :wink:

You also can’t have static objects, because there is no “default
constructor”, so objects will not be constructed until you create them.
This
means that this form won’t work:

class SomeClass x;
void func(…)
{
x.something = 7;
}

This is because x will not get constructed. You need to reconstruction
with
a x as a pointer to SomeClass and then add
x = new SomeClass;
at “start of day”.

There’s a bunch of other support functions that are less obvious, like
“You’ve called a pure virtual function”.

int __cdecl _purecall( void )
{
assert( 0 );
}

I’m sure there are some other gotchas too, but these will certainly help
you.

You may also find that the stack space used is greater, due to the
“ease”
with which you create objects and store more than you though on the
stack.
The stack of a driver is only small.

I can’t really do much to explain #2 in the list above, simply because
what
we use the C++ for is to simulate the next generation graphics chip, and
it’s linked into the driver as a chip simulator. We have severe problems
with for instance stack usage, but sidestep the issue by breaking the
rules
and creating a second stack that we switch to before calling the
simulator
with a “pretend to write this register” or “pretend you read this
memory”.
(We also have our own multithreading in the C++ code, which doesn’t use
Windows threads, to simulate the parallelism of different units working
on
the same clock-cycle in the chip).


Mats

-----Original Message-----
From: Robert Fernando -ntlworld [mailto:xxxxx@ntlworld.com]
Sent: Wednesday, February 11, 2004 11:00 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] A giuide to writing device drivers in c++

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver. I know about developing windows
device drivers in c.

thanks

Robert Fernando
www.rowanclose.com


Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.583 / Virus Database: 369 - Release Date: 10/02/2004


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


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

You are currently subscribed to ntdev as: xxxxx@maxtor.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@windows.microsoft.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

The major restrictions are no c++ exceptions, no Microsoft supplied c++
runtime support for global new/delete or global constructors, no STL. The
caveats are that the compiler MIGHT put generated functions into the wrong
page type (as in paged when it ought to be nonpaged.)

This last one is a bit tricky. Essentially template, virtual, and default
functions do not have any mechanism to force them into either paged or
nonpaged pool. Real world experience from numerous C++ kernel projects
indicates that while this MIGHT cause problems, it very rarely does. The
best practice is to check the linker output and see exactly where everything
ended up. Microsoft really ought to fix this with a pragma of some sort.

Runtime support except for c++ exceptions and STL is available public domain
from several sources, including me at www.hollistech.com. Rumor has it that
STL or something like it has been ported into the kernel. The general
consensus is to avoid c++ exceptions. This means that you have to have an
‘init’ method for classes that need to allocate resources in what ought to
be their constructor, and that construction is a two step process. This
cruft was a major motivation for building c++ exceptions to begin with, but
that’s the way things are, deal with it.

ExFreePoolWithTag can’t be supported in a global delete operator, although
it can be supported in a class delete operator. Not a big deal until
verifier demands the use of ExFreePoolWithTag.

Other constructive suggestions in this thread apply.

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

-----Original Message-----
From: Doron Holan [mailto:xxxxx@windows.microsoft.com]
Sent: Wednesday, February 11, 2004 1:56 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Do not ASSERT that alloc will return a valid pointer. Low
resources are comment and you must handle this in operator
new and treat it as the norm, not an exceptional case.

D

This posting is provided “AS IS” with no warranties, and
confers no rights

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@3Dlabs.com
Sent: Wednesday, February 11, 2004 10:42 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Hi Robert,

I think this problem can be split into two pieces:

  1. Difficulties using C++ in an environment that doesn’t
    support C++ fully.
  2. Designing driver code using good C++.

I can be of some help with 1:
You need to do a fair bit of work to get C++ to run in a
driver environment.
The major, and obvious points, are new and delete
functionality. You don’t have a standard C runtime library,
so you can’t just malloc/free to do memory allocation (which
is essentially the way new/delete works in a normal application).

You can replace new/delete with your own “overrides”, which
should solve this.
void * __cdecl::operator new( unsigned int size ) {
void *ptr;

ptr = allocatememory(size);
assert(ptr);
return ptr;
}

void __cdecl ::operator delete( void *ptr ) {
if( ptr )
freememory(ptr);
}

How you implement allocatememory and freememory is another
story… :wink:

You also can’t have static objects, because there is no
“default constructor”, so objects will not be constructed
until you create them.
This
means that this form won’t work:

class SomeClass x;
void func(…)
{
x.something = 7;
}

This is because x will not get constructed. You need to
reconstruction with a x as a pointer to SomeClass and then
add x = new SomeClass; at “start of day”.

There’s a bunch of other support functions that are less
obvious, like “You’ve called a pure virtual function”.

int __cdecl _purecall( void )
{
assert( 0 );
}

I’m sure there are some other gotchas too, but these will
certainly help you.

You may also find that the stack space used is greater, due
to the “ease”
with which you create objects and store more than you though
on the stack.
The stack of a driver is only small.

I can’t really do much to explain #2 in the list above,
simply because what we use the C++ for is to simulate the
next generation graphics chip, and it’s linked into the
driver as a chip simulator. We have severe problems with for
instance stack usage, but sidestep the issue by breaking the
rules and creating a second stack that we switch to before
calling the simulator with a “pretend to write this register”
or “pretend you read this memory”.
(We also have our own multithreading in the C++ code, which
doesn’t use Windows threads, to simulate the parallelism of
different units working on the same clock-cycle in the chip).


Mats

> -----Original Message-----
> From: Robert Fernando -ntlworld
[mailto:xxxxx@ntlworld.com]
> Sent: Wednesday, February 11, 2004 11:00 AM
> To: Windows System Software Devs Interest List
> Subject: [ntdev] A giuide to writing device drivers in c++
>
>
> Hi all,
> Is there a good guide / samples which will outline how you
use c++ to
> develop a windows device driver.
> I know about developing windows device drivers in c.
>
> thanks
>
> Robert Fernando
> www.rowanclose.com
>
>
>
> —
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.583 / Virus Database: 369 - Release Date: 10/02/2004
>
>
> —
> 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
>


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


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

I think that purecall is in the kernel runtime.

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

-----Original Message-----
From: xxxxx@3Dlabs.com [mailto:xxxxx@3Dlabs.com]
Sent: Wednesday, February 11, 2004 1:42 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Hi Robert,

I think this problem can be split into two pieces:

  1. Difficulties using C++ in an environment that doesn’t
    support C++ fully.
  2. Designing driver code using good C++.

I can be of some help with 1:
You need to do a fair bit of work to get C++ to run in a
driver environment.
The major, and obvious points, are new and delete
functionality. You don’t have a standard C runtime library,
so you can’t just malloc/free to do memory allocation (which
is essentially the way new/delete works in a normal application).

You can replace new/delete with your own “overrides”, which
should solve this.
void * __cdecl::operator new( unsigned int size ) {
void *ptr;

ptr = allocatememory(size);
assert(ptr);
return ptr;
}

void __cdecl ::operator delete( void *ptr ) {
if( ptr )
freememory(ptr);
}

How you implement allocatememory and freememory is another
story… :wink:

You also can’t have static objects, because there is no
“default constructor”, so objects will not be constructed
until you create them. This means that this form won’t work:

class SomeClass x;
void func(…)
{
x.something = 7;
}

This is because x will not get constructed. You need to
reconstruction with a x as a pointer to SomeClass and then
add x = new SomeClass; at “start of day”.

There’s a bunch of other support functions that are less
obvious, like “You’ve called a pure virtual function”.

int __cdecl _purecall( void )
{
assert( 0 );
}

I’m sure there are some other gotchas too, but these will
certainly help you.

You may also find that the stack space used is greater, due
to the “ease”
with which you create objects and store more than you though
on the stack.
The stack of a driver is only small.

I can’t really do much to explain #2 in the list above,
simply because what we use the C++ for is to simulate the
next generation graphics chip, and it’s linked into the
driver as a chip simulator. We have severe problems with for
instance stack usage, but sidestep the issue by breaking the
rules and creating a second stack that we switch to before
calling the simulator with a “pretend to write this register”
or “pretend you read this memory”.
(We also have our own multithreading in the C++ code, which
doesn’t use Windows threads, to simulate the parallelism of
different units working on the same clock-cycle in the chip).


Mats

> -----Original Message-----
> From: Robert Fernando -ntlworld
[mailto:xxxxx@ntlworld.com]
> Sent: Wednesday, February 11, 2004 11:00 AM
> To: Windows System Software Devs Interest List
> Subject: [ntdev] A giuide to writing device drivers in c++
>
>
> Hi all,
> Is there a good guide / samples which will outline how you
use c++ to
> develop a windows device driver.
> I know about developing windows device drivers in c.
>
> thanks
>
> Robert Fernando
> www.rowanclose.com
>
>
>
> —
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.583 / Virus Database: 369 - Release Date: 10/02/2004
>
>
> —
> 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
>


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

Presumably Mats is referring to a non-production driver. Otherwise, if
his driver ships with a C++ simulator of a 3D graphics chip, Half Life
isn’t going to run very fast. =^)

Chuck

----- Original Message -----
From: “Jamey Kirby”
To: “Windows System Software Devs Interest List”
Sent: Thursday, February 12, 2004 2:05 AM
Subject: RE: [ntdev] A giuide to writing device drivers in c++

> Switching stacks in a driver… Please, some shoot me! Remind me
again what
> driver(s) we are talking about. I want to make sure that it/they NEVER
> get(s) loaded on my system(s).
>
> Jamey
>
> -----Original Message-----
> From: xxxxx@lists.osr.com
> [mailto:xxxxx@lists.osr.com] On Behalf Of
> xxxxx@3Dlabs.com
> Sent: Wednesday, February 11, 2004 10:42 AM
> To: Windows System Software Devs Interest List
> Subject: RE: [ntdev] A giuide to writing device drivers in c++
>
> Hi Robert,
>
> I think this problem can be split into two pieces:
> 1. Difficulties using C++ in an environment that doesn’t support C++
fully.
> 2. Designing driver code using good C++.
>
> I can be of some help with 1:
> You need to do a fair bit of work to get C++ to run in a driver
environment.
> The major, and obvious points, are new and delete functionality. You
don’t
> have a standard C runtime library, so you can’t just malloc/free to do
> memory allocation (which is essentially the way new/delete works in a
normal
> application).
>
> You can replace new/delete with your own “overrides”, which should
solve
> this.
> void * __cdecl::operator new( unsigned int size )
> {
> void *ptr;
>
> ptr = allocatememory(size);
> assert(ptr);
> return ptr;
> }
>
> void__cdecl ::operator delete( void *ptr ) {
> if( ptr )
> freememory(ptr);
> }
>
> How you implement allocatememory and freememory is another story…
:wink:
>
> You also can’t have static objects, because there is no “default
> constructor”, so objects will not be constructed until you create
them. This
> means that this form won’t work:
>
> class SomeClass x;
> void func(…)
> {
> x.something = 7;
> }
>
> This is because x will not get constructed. You need to reconstruction
with
> a x as a pointer to SomeClass and then add
> x = new SomeClass;
> at “start of day”.
>
> There’s a bunch of other support functions that are less obvious, like
> “You’ve called a pure virtual function”.
>
> int __cdecl _purecall( void )
> {
> assert( 0 );
> }
>
> I’m sure there are some other gotchas too, but these will certainly
help
> you.
>
> You may also find that the stack space used is greater, due to the
“ease”
> with which you create objects and store more than you though on the
stack.
> The stack of a driver is only small.
>
> I can’t really do much to explain #2 in the list above, simply because
what
> we use the C++ for is to simulate the next generation graphics chip,
and
> it’s linked into the driver as a chip simulator. We have severe
problems
> with for instance stack usage, but sidestep the issue by breaking the
rules
> and creating a second stack that we switch to before calling the
simulator
> with a “pretend to write this register” or “pretend you read this
memory”.
> (We also have our own multithreading in the C++ code, which doesn’t
use
> Windows threads, to simulate the parallelism of different units
working on
> the same clock-cycle in the chip).
>
> –
> Mats
>
> > -----Original Message-----
> > From: Robert Fernando -ntlworld
[mailto:xxxxx@ntlworld.com]
> > Sent: Wednesday, February 11, 2004 11:00 AM
> > To: Windows System Software Devs Interest List
> > Subject: [ntdev] A giuide to writing device drivers in c++
> >
> >
> > Hi all,
> > Is there a good guide / samples which will outline how you use c++
to
> > develop a windows device driver.
> > I know about developing windows device drivers in c.
> >
> > thanks
> >
> > Robert Fernando
> > www.rowanclose.com
> >
> >
> >
> > —
> > Outgoing mail is certified Virus Free.
> > Checked by AVG anti-virus system (http://www.grisoft.com).
> > Version: 6.0.583 / Virus Database: 369 - Release Date: 10/02/2004
> >
> >
> > —
> > 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
> >
>
> —
> 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
>
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@cbatson.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>

Yes that’s right Doron,

I’m wrong on double count(s). Now I think, he was talking about local
stack variable(s) using class object(s), well then anyone can blow the
stack up ( longlong arrary[1024][12] ) or some such…

-prokash

-----Original Message-----
From: Doron Holan [mailto:xxxxx@windows.microsoft.com]
Sent: Wednesday, February 11, 2004 11:11 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Usually the this pointer is stuffed into ecx, not pushed onto the stack.
This has a sideaffect though that there is one less register to use in
the function so more stack space may be used later.

d

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Sinha, Prokash
Sent: Wednesday, February 11, 2004 10:59 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Mat

Not to pointificate, but IIRC the stack frame will have only one extra
4-byte *this* ptr where it is implicit !!!

-prokash

-----Original Message-----
From: xxxxx@3Dlabs.com [mailto:xxxxx@3Dlabs.com]
Sent: Wednesday, February 11, 2004 10:42 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A giuide to writing device drivers in c++

Hi Robert,

I think this problem can be split into two pieces:

  1. Difficulties using C++ in an environment that doesn’t support C++
    fully.
  2. Designing driver code using good C++.

I can be of some help with 1:
You need to do a fair bit of work to get C++ to run in a driver
environment. The major, and obvious points, are new and delete
functionality. You don’t have a standard C runtime library, so you can’t
just malloc/free to do memory allocation (which is essentially the way
new/delete works in a normal application).

You can replace new/delete with your own “overrides”, which should solve
this.
void * __cdecl::operator new( unsigned int size )
{
void *ptr;

ptr = allocatememory(size);
assert(ptr);
return ptr;
}

void __cdecl ::operator delete( void *ptr ) {
if( ptr )
freememory(ptr);
}

How you implement allocatememory and freememory is another story… :wink:

You also can’t have static objects, because there is no “default
constructor”, so objects will not be constructed until you create them.
This means that this form won’t work:

class SomeClass x;
void func(…)
{
x.something = 7;
}

This is because x will not get constructed. You need to reconstruction
with a x as a pointer to SomeClass and then add
x = new SomeClass;
at “start of day”.

There’s a bunch of other support functions that are less obvious, like
“You’ve called a pure virtual function”.

int __cdecl _purecall( void )
{
assert( 0 );
}

I’m sure there are some other gotchas too, but these will certainly help
you.

You may also find that the stack space used is greater, due to the
“ease” with which you create objects and store more than you though on
the stack. The stack of a driver is only small.

I can’t really do much to explain #2 in the list above, simply because
what we use the C++ for is to simulate the next generation graphics
chip, and it’s linked into the driver as a chip simulator. We have
severe problems with for instance stack usage, but sidestep the issue by
breaking the rules and creating a second stack that we switch to before
calling the simulator with a “pretend to write this register” or
“pretend you read this memory”. (We also have our own multithreading in
the C++ code, which doesn’t use Windows threads, to simulate the
parallelism of different units working on the same clock-cycle in the
chip).


Mats

-----Original Message-----
From: Robert Fernando -ntlworld [mailto:xxxxx@ntlworld.com]
Sent: Wednesday, February 11, 2004 11:00 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] A giuide to writing device drivers in c++

Hi all,
Is there a good guide / samples which will outline how you use c++ to
develop a windows device driver. I know about developing windows
device drivers in c.

thanks

Robert Fernando
www.rowanclose.com


Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.583 / Virus Database: 369 - Release Date: 10/02/2004


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


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

You are currently subscribed to ntdev as: xxxxx@maxtor.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@windows.microsoft.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@maxtor.com To
unsubscribe send a blank email to xxxxx@lists.osr.com