Why is this OK in kbfiltr?

I have two questions,

=====================
I. Struct = Struct
I defined the following in kbfiltr.h:
typedef struct _DEVICE_EXTENSION
{
//
// The real connect data that this driver reports to
//
CONNECT_DATA ConnectData;
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;

Then in kbfiltr.c, I found this line:
Function(PDEVICE_EXTENSION kbExtension)
{
PCONNECT_DATA connectData;

kbExtension->ConnectData = *connectData;

}

is this OK? I mean kbExtension->ConnectData is a structure, can I really just assign it using this line:

“kbExtension->ConnectData = *connectData;”

=====================
II. Typecast service callback

devExt = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;

(*(PSERVICE_CALLBACK_ROUTINE) devExt->UpperConnectData.ClassService)(
devExt->UpperConnectData.ClassDeviceObject,
InputDataStart,
InputDataEnd,
InputDataConsumed);

Why need to typecast “(*(PSERVICE_CALLBACK_ROUTINE) devExt->UpperConnectData.ClassService)”?

thanks!

Yes that is a completely valid C assignment operation. In C++ terms it
invokes the default copy constructor, which does a memcpy operation on
the bits in the rhs to the bits in the lhs.

Mark Roddy

On Wed, Jun 1, 2011 at 8:06 AM, wrote:
> I have two questions,
>
> =====================
> I. Struct = Struct
> I defined the following in kbfiltr.h:
> typedef struct _DEVICE_EXTENSION
> {
> ? ?//
> ? ?// The real connect data that this driver reports to
> ? ?//
> ? ?CONNECT_DATA ConnectData;
> } DEVICE_EXTENSION, *PDEVICE_EXTENSION;
>
> Then in kbfiltr.c, I found this line:
> Function(PDEVICE_EXTENSION kbExtension)
> {
> PCONNECT_DATA ?connectData;
>
> kbExtension->ConnectData = connectData;
>
> }
>
> is this OK? I mean kbExtension->ConnectData is a structure, can I really just assign it using this line:
>
> "kbExtension->ConnectData = connectData;"
>
> =====================
> II. Typecast service callback
>
> devExt = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
>
> (
(PSERVICE_CALLBACK_ROUTINE) devExt->UpperConnectData.ClassService)(
> ? ? ? ? ? ? ? ?devExt->UpperConnectData.ClassDeviceObject,
> ? ? ? ? ? ? ? ?InputDataStart,
> ? ? ? ? ? ? ? ?InputDataEnd,
> ? ? ? ? ? ? ? ?InputDataConsumed);
>
> Why need to typecast "(
(PSERVICE_CALLBACK_ROUTINE) devExt->UpperConnectData.ClassService)"?
>
> thanks!
>
> —
> 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
>

xxxxx@yahoo.com wrote:

I have two questions,

A few minutes of independent research would have answered BOTH of these
questions way faster than this mailing list.

=====================
I. Struct = Struct

Then in kbfiltr.c, I found this line:
Function(PDEVICE_EXTENSION kbExtension)
{
PCONNECT_DATA connectData;
kbExtension->ConnectData = *connectData;
}

is this OK? I mean kbExtension->ConnectData is a structure, can I really just assign it using this line:

“kbExtension->ConnectData = *connectData;”

Did you try compiling this? Did it compile? Did you think the compiler
was lying to you?

II. Typecast service callback

devExt = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;

(*(PSERVICE_CALLBACK_ROUTINE) devExt->UpperConnectData.ClassService)(
devExt->UpperConnectData.ClassDeviceObject,
InputDataStart,
InputDataEnd,
InputDataConsumed);

Why need to typecast “(*(PSERVICE_CALLBACK_ROUTINE) devExt->UpperConnectData.ClassService)”?

Did you take any time at all to look up what type of variable
ClassService is? UpperConnectData is of type CONNECT_DATA. That’s a
publicly defined structure in the WDK. CONNECT_DATA::ClassService is of
type PVOID, probably for purely historical reasons. Thus, using it in
any way requires a cast.


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

xxxxx@yahoo.com wrote:

I have two questions,

=====================
I. Struct = Struct
I defined the following in kbfiltr.h:
typedef struct _DEVICE_EXTENSION
{
//
// The real connect data that this driver reports to
//
CONNECT_DATA ConnectData;
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;

Then in kbfiltr.c, I found this line:
Function(PDEVICE_EXTENSION kbExtension)
{
PCONNECT_DATA connectData;

kbExtension->ConnectData = *connectData;

}

is this OK? I mean kbExtension->ConnectData is a structure, can I really just assign it using this line:

“kbExtension->ConnectData = *connectData;”

Ordinary C, why do you think there’s a problem with it?

=====================
II. Typecast service callback

devExt = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;

(*(PSERVICE_CALLBACK_ROUTINE) devExt->UpperConnectData.ClassService)(
devExt->UpperConnectData.ClassDeviceObject,
InputDataStart,
InputDataEnd,
InputDataConsumed);

Why need to typecast “(*(PSERVICE_CALLBACK_ROUTINE) devExt->UpperConnectData.ClassService)”?

I assume you mean “cast”. You need to cast it for the same reason as you
ever need to cast anything - the original object is not the type it
needs to be. It’s being used to hold a PSERVICE_CALLBACK_ROUTINE but it
isn’t declared as a PSERVICE_CALLBACK_ROUTINE.

You don’t need the dereference though,

(PSERVICE_CALLBACK_ROUTINE)devExt->UpperConnectData.ClassService(

would be fine.

sorry for the inconvenience…

thanks!