How to use reinterpret_cast in kernel driver

Hi, Guys!

As the title has describled, I don’t know how to use the cast
languages in kernek driver, such as static_cast and reinterpret_cast
and so on. I have noticed that many drivers used these, but I always
don’t know how to use them. I look into the msdn, and tried to add
many windows header files to the kernel driver, but they all failed.
Can any one who knows help me?
Many Thanks!
Best Regards!

Well, if you posted some of your source code with the problem, as well
as the errors that BUILD gives you, that would really help. However,
the problem is definitely no one of the wrong header files, as part of
the core C++ language. Some possibilities:

  1. You’re compiling the source files as C, not C++. This will cause a
    lot of problems. You either need to add to instruct BUILD to build them
    as C++ by adding ‘USECXX_FLAG=-TP’ to your SOURCES file, or if you
    prefer, you can change the extension of each ‘.c’ file to ‘.cpp.’

  2. You’re just not using them correctly. There’s no way to say whether
    this is the problem or not without your source code.

  3. Have you asked yourself why you want to use these? If you’re
    heading toward more general use of C++, understand that you have a lot
    of work ahead of you to get that working in the kernel, and it will
    totally unsupported by almost everyone on this list. I actually use C++
    in the kernel myself, but that’s just how it is. If you just want to
    use C++ new casting operators (other than dynamic_cast<>; that requires
    runtime support), then shouldn’t have any problems or require anything
    special, other than instructing BUILD to us the C++ compiler instead of
    C. If you have any intention of putting this driver through WHQL, you
    might want to see if use of the C++ compiler is a deal breaker, even you
    only use features that require no runtime support; I really don’t know.

Good luck,

mm

张佩 wrote:

Hi, Guys!

As the title has describled, I don’t know how to use the cast
languages in kernek driver, such as static_cast and reinterpret_cast
and so on. I have noticed that many drivers used these, but I always
don’t know how to use them. I look into the msdn, and tried to add
many windows header files to the kernel driver, but they all failed.
Can any one who knows help me?
Many Thanks!
Best Regards!

> As the title has describled, I don’t know how to use the cast

languages in kernek driver, such as static_cast and reinterpret_cast

I would forget these postmodern C++'s dirty misfeatures forever and limit yourself to usual C-style casts (even in C++ code).


Maxim S. Shatskih
Windows DDK MVP
xxxxx@storagecraft.com
http://www.storagecraft.com

The answer to the OP’s question, minus all the snarkage, is:
reinterpret_cast works just fine.

On 1/20/09, Maxim S. Shatskih wrote:
>> As the title has describled, I don’t know how to use the cast
>> languages in kernek driver, such as static_cast and reinterpret_cast
>
> I would forget these postmodern C++'s dirty misfeatures forever and limit
> yourself to usual C-style casts (even in C++ code).
>
> –
> Maxim S. Shatskih
> Windows DDK MVP
> xxxxx@storagecraft.com
> http://www.storagecraft.com
>
>
> —
> 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
>


Mark Roddy

> The answer to the OP’s question, minus all the snarkage, is:

reinterpret_cast works just fine.

Correct. Unlike dynamic_cast, they do not require runtime support.


Maxim S. Shatskih
Windows DDK MVP
xxxxx@storagecraft.com
http://www.storagecraft.com

The snarkage IS pretty good, however. “post modern C++ dirty misfeatures”… GOSH I like that phrase. Wish I had written then in a Peter Pontificates…

Peter
OSR

The c++ <> cast ‘misfeatures’ are annotations that describe what the cast is
going to do, and in some cases, with runtime support (not present in the
kernel), prevent a cast from doing the wrong thing.
I suppose it might be considered a bad thing for programmers to be explicit
about their intentions.

Mark Roddy

On Tue, Jan 20, 2009 at 10:03 AM, wrote:

>


>
> The snarkage IS pretty good, however. “post modern C++ dirty
> misfeatures”… GOSH I like that phrase. Wish I had written then in a Peter
> Pontificates…
>
> Peter
> OSR
>
>
>
> —
> 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
>

?? wrote:

Hi, Guys!

As the title has describled, I don’t know how to use the cast
languages in kernek driver, such as static_cast and reinterpret_cast
and so on. I have noticed that many drivers used these, but I always
don’t know how to use them. I look into the msdn, and tried to add
many windows header files to the kernel driver, but they all failed.

There is a handy macro for static_cast, if you have module(s)
that should compile in either c and c++ mode:

#if defined(__cplusplus)
#define RTL_CONST_CAST(type) const_cast
#else
#define RTL_CONST_CAST(type) (type)
#endif

For reinterpret_cast, just use C-stype cast.

– pa

“failed”? I have no idea what that means. Perhaps a DESCRIPTION of the
concept of “failed” might help…

Also note that these are C++, not C.

The simplest way to write a reinterpret cast to type T in the kernel is
(T)expression

The rest is syntactic saccharine (sort of like syntactic sugar, but totally
fake)
joe

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of ??
Sent: Tuesday, January 20, 2009 1:21 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] How to use reinterpret_cast in kernel driver

Hi, Guys!

As the title has describled, I don’t know how to use the cast languages in
kernek driver, such as static_cast and reinterpret_cast and so on. I have
noticed that many drivers used these, but I always don’t know how to use
them. I look into the msdn, and tried to add many windows header files to
the kernel driver, but they all failed.
Can any one who knows help me?
Many Thanks!
Best Regards!


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


This message has been scanned for viruses and dangerous content by
MailScanner, and is believed to be clean.

Now that is a Shatskih-ism that you would expect Chekov to mutter to Sulu as
he turns back to the weapons console.

:slight_smile:


The personal opinion of
Gary G. Little

wrote in message news:xxxxx@ntdev…
>


>
> The snarkage IS pretty good, however. “post modern C++ dirty
> misfeatures”… GOSH I like that phrase. Wish I had written then in a
> Peter Pontificates…
>
> Peter
> OSR
>
>
>

>I look into the msdn, and tried to add many windows header files to

the kernel driver, but they all failed.

As Joe says, that’s kind of a broad problem description.

However, by the sound of it you are saying that you took SDK header
files and tried to add them to your driver project.

The SDK and WDK are two totally independent and complete
environments, the headers are NOT interchangeable, or at least until
you really really know what you are doing.

You have a lot of problems. Take a step back and solve them one by
one. If you need to post questions here, try not to assume any
telepathic abilities amongst list members.

Mark.

At 16:22 20/01/2009, Joseph M. Newcomer wrote:

“failed”? I have no idea what that means. Perhaps a DESCRIPTION of the
concept of “failed” might help…

Also note that these are C++, not C.

The simplest way to write a reinterpret cast to type T in the kernel is
(T)expression

The rest is syntactic saccharine (sort of like syntactic sugar, but totally
fake)
joe

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of ??
Sent: Tuesday, January 20, 2009 1:21 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] How to use reinterpret_cast in kernel driver

Hi, Guys!

As the title has describled, I don’t know how to use the cast languages in
kernek driver, such as static_cast and reinterpret_cast and so on. I have
noticed that many drivers used these, but I always don’t know how to use
them. I look into the msdn, and tried to add many windows header files to
the kernel driver, but they all failed.
Can any one who knows help me?
Many Thanks!
Best Regards!

xxxxx@osr.com wrote:

The snarkage IS pretty good, however. “post modern C++ dirty misfeatures”… GOSH I like that phrase. Wish I had written then in a Peter Pontificates…

Yes, Maxim is clearly in rare form this morning…


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

张佩 wrote:

As the title has describled, I don’t know how to use the cast
languages in kernek driver, such as static_cast and reinterpret_cast
and so on. I have noticed that many drivers used these, but I always
don’t know how to use them. I look into the msdn, and tried to add
many windows header files to the kernel driver, but they all failed.

No one has actually answered your question, but that’s probably because
this is really the wrong list for this question. These template-like
casts are part of C++. If your drivers are not written in C++, you
cannot use them, no matter how many include files you add.

There are four template-like casts: static_cast<>, const_cast<>,
reinterpret_cast<>, and dynamic_cast<>. Static_cast is closest to the
C-style cast. Static_cast from double to int, for example, causes the
value to be converted to an integer. Reinterpret_cast basically says
“take these bits and treat them as if they were of that type”. It is
commonly used to convert between pointer types. Const_cast<> is used to
remove the const or volatile attribute from a value, such as when you
are given a “const char *” that you need to pass into an API that is
declared as taking a “char *”, even when it won’t modify the value.
Dynamic_cast is a rather expensive operation which converts a pointer
(or reference) to another pointer type, and then validates at run-time
that the pointer really is of the new type. In order to do that, the
compiler has to add additional type identifying information to every
structure in your code. This is called RTTI (run-time type
identification), and is very expensive.


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

>I suppose it might be considered a bad thing for programmers to be explicit about their intentions.

The thing is that “what cast is going to do” is always clear from the dst type, which is in brackets for C-style cast.

I can understand that probably using the same (TYPE)Obj notation both to remove “const” and to cast to the base class is probably the violation of some principles, but it is well-looking.

Now compare reinterpret_castType2Val with (PTYPE1)(PVOID)Type2Val. Isn’t the second notation clearer?

dynamic_cast is another song. First we introduce polymorphism to the language, and then we break it by introducing the language-embedded RTTI.

“Postmodern” is related mainly to syntax of these features. Use a template syntax for a cast is like using the bitwise shift operator to print to stdout (this “postmodernism” is also in C++).


Maxim S. Shatskih
Windows DDK MVP
xxxxx@storagecraft.com
http://www.storagecraft.com

Pavel A. wrote:

There is a handy macro for static_cast, if you have module(s)

Sorry… this should be : for const_cast --pa

that should compile in either c and c++ mode:

#if defined(__cplusplus)
#define RTL_CONST_CAST(type) const_cast
> #else
> #define RTL_CONST_CAST(type) (type)
> #endif
>
> For reinterpret_cast, just use C-stype cast.
>
> – pa
>

I answered it, a long, long time ago.

Don’t believe the hype.

mm

Tim Roberts wrote:

张佩 wrote:
> As the title has describled, I don’t know how to use the cast
> languages in kernek driver, such as static_cast and reinterpret_cast
> and so on. I have noticed that many drivers used these, but I always
> don’t know how to use them. I look into the msdn, and tried to add
> many windows header files to the kernel driver, but they all failed.
>

No one has actually answered your question, but that’s probably because
this is really the wrong list for this question. These template-like
casts are part of C++. If your drivers are not written in C++, you
cannot use them, no matter how many include files you add.

There are four template-like casts: static_cast<>, const_cast<>,
reinterpret_cast<>, and dynamic_cast<>. Static_cast is closest to the
C-style cast. Static_cast from double to int, for example, causes the
value to be converted to an integer. Reinterpret_cast basically says
“take these bits and treat them as if they were of that type”. It is
commonly used to convert between pointer types. Const_cast<> is used to
remove the const or volatile attribute from a value, such as when you
are given a “const char *” that you need to pass into an API that is
declared as taking a “char *”, even when it won’t modify the value.
Dynamic_cast is a rather expensive operation which converts a pointer
(or reference) to another pointer type, and then validates at run-time
that the pointer really is of the new type. In order to do that, the
compiler has to add additional type identifying information to every
structure in your code. This is called RTTI (run-time type
identification), and is very expensive.

> The thing is that “what cast is going to do” is always clear from the dst type, which is in brackets for C-style cast.

Yes, but “what cast is going to do” is not always what it was meant to do, nor what is safe to do. C-style casts are overloaded and produce very different code depending on what is on both side of the cast and will change meaning without a warning if the types ever change. C++ cast will only do what they are explicitly told to do. I’ll take explicit and visible breakage over implicit meaning and silent breakage any day.

I agree. This is more along the line of the C++ design thoughts, IIRC.

Two things I’ve bumped into quite often are -

(1) C++ code in windows kernel mode drivers, and

(2) Building them using some form of VS ide ( thank god, it was not Boreland
IDE)

I guess when things move to C#, probably there will be project template for
KM and UM drivers on VS IDE. Kind of shooting two birds with one bullet !

Well, as if I’ve to sign something !
Prokash Sinha
http://prokash.squarespace.com
Success has many fathers, but failure is an orphan.

----- Original Message -----
From:
To: “Windows System Software Devs Interest List”
Sent: Wednesday, January 21, 2009 10:03 AM
Subject: RE:[ntdev] How to use reinterpret_cast in kernel driver

>> The thing is that “what cast is going to do” is always clear from the dst
>> type, which is in brackets for C-style cast.
>
> Yes, but “what cast is going to do” is not always what it was meant to do,
> nor what is safe to do. C-style casts are overloaded and produce very
> different code depending on what is on both side of the cast and will
> change meaning without a warning if the types ever change. C++ cast will
> only do what they are explicitly told to do. I’ll take explicit and
> visible breakage over implicit meaning and silent breakage any day.
>
> —
> 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

Hi, Guys!
Many thanks to all everyone’s answer. From the discussion, I gained a
strong sense of your kindness and hight skill. Thank you.

2009/1/22 Prokash Sinha :
> I agree. This is more along the line of the C++ design thoughts, IIRC.
>
> Two things I’ve bumped into quite often are -
>
> (1) C++ code in windows kernel mode drivers, and
>
> (2) Building them using some form of VS ide ( thank god, it was not Boreland
> IDE)
>
> I guess when things move to C#, probably there will be project template for
> KM and UM drivers on VS IDE. Kind of shooting two birds with one bullet !
>
> Well, as if I’ve to sign something !
> Prokash Sinha
> http://prokash.squarespace.com
> Success has many fathers, but failure is an orphan.
>
> ----- Original Message ----- From:
> To: “Windows System Software Devs Interest List”
> Sent: Wednesday, January 21, 2009 10:03 AM
> Subject: RE:[ntdev] How to use reinterpret_cast in kernel driver
>
>
>>> The thing is that “what cast is going to do” is always clear from the dst
>>> type, which is in brackets for C-style cast.
>>
>> Yes, but “what cast is going to do” is not always what it was meant to do,
>> nor what is safe to do. C-style casts are overloaded and produce very
>> different code depending on what is on both side of the cast and will change
>> meaning without a warning if the types ever change. C++ cast will only do
>> what they are explicitly told to do. I’ll take explicit and visible breakage
>> over implicit meaning and silent breakage any day.
>>
>> —
>> 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
>
>
> —
> 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
>

At the DDC they had an “Ask The Experts” evening. Some folks had some news
about a currently unapproved/unfunded/unofficial project that enables Visual
Studio to write, create, and debug drivers. It will have some intelligence
but it will use the VS debugger to run the kernel debugger on the target
system. Not a lot of details, but they did mention our support can help get
the project funded and made a part of a future release. I can see some
advantages even if it is a big install. It probably won’t sell a lot of VS
to the world but it may help us.

“Prokash Sinha” wrote in message news:xxxxx@ntdev…
>I agree. This is more along the line of the C++ design thoughts, IIRC.
>
> Two things I’ve bumped into quite often are -
>
> (1) C++ code in windows kernel mode drivers, and
>
> (2) Building them using some form of VS ide ( thank god, it was not
> Boreland IDE)
>
> I guess when things move to C#, probably there will be project template
> for KM and UM drivers on VS IDE. Kind of shooting two birds with one
> bullet !
>
> Well, as if I’ve to sign something !
> Prokash Sinha
> http://prokash.squarespace.com
> Success has many fathers, but failure is an orphan.
>
> ----- Original Message -----
> From:
> To: “Windows System Software Devs Interest List”
> Sent: Wednesday, January 21, 2009 10:03 AM
> Subject: RE:[ntdev] How to use reinterpret_cast in kernel driver
>
>
>>> The thing is that “what cast is going to do” is always clear from the
>>> dst type, which is in brackets for C-style cast.
>>
>> Yes, but “what cast is going to do” is not always what it was meant to
>> do, nor what is safe to do. C-style casts are overloaded and produce very
>> different code depending on what is on both side of the cast and will
>> change meaning without a warning if the types ever change. C++ cast will
>> only do what they are explicitly told to do. I’ll take explicit and
>> visible breakage over implicit meaning and silent breakage any day.
>>
>> —
>> 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
>
>