Actually, the moral of the story is, know the language spec. Initializing
Union variables is an ANSI C innovation, so, use it the way the spec says it
should be used !
By the way, I can’t compile it as you wrote. So, I changed it to reflect
what I think you mean, apologies in advance if I misread you ! But this kind
of thing belongs in a constructor. If you use “.cpp” as your file extension,
you can do this:
struct s
{
union
{
char a;
long b;
};
s(long x) { b = x; }
};
Works beautifully, and cleanly too. You can even have another constructor
for the char:
struct s
{
union
{
char a;
long b;
};
s(long x) { b = x; }
s(char x) { a = x; /* TODO: whatever else you might want to do to munge
your char ! */ }
};
Or, you can use inheritance to get rid of that union altogether, and write
something like
struct s { };
struct l : public s { long b; l(long x) { b = x; } };
struct c : public s { char a; c(char x) { a = x; } };
So that you write “l my_l(92345)” for a long or “c my_c(‘a’)” for a char.
Alberto.
-----Original Message-----
From: Henry Gabryjelski [mailto:xxxxx@windows.microsoft.com]
Sent: Thursday, October 09, 2003 11:08 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] RE: Worker Thread and GetOverlappedResult…
Caveat:
Be careful when initializing structures with = {0}. Our group now
considers this a “bad” programming practice, because we got bitten by
come very obscure rules in the C spec. Here’s why:
Consider the following structure:
// #pragma pack(1) for this struct…
typedef struct _MY_STRUCT {
union { UCHAR a; } OneChar;
union { ULONG b; } OneUlong;
} MY_STRUCT, *PMY_STRUCT;
If you were to zero out the struct using the following line:
MY_STRUCT tmp = {0};
It would not behave as you may expect – only the first byte would be
set to zero! Why? Because by using the “{}” notation, you are filling
in the values within the struct, and C says that all non-specified items
get zero-filled. However, in the case of a union, the C compiler just
picks the first (undefined which is first?) union to fill to satisfy
this requirement.
Moral of the story: Always RtlZeroMemory() your structs, and remove the
“= {0};” constructs.
I hope this helps someone out there…
.
-----Original Message-----
From: subodh gupta [mailto:xxxxx@softhome.net]
Sent: Tuesday, September 30, 2003 8:27 PM
Subject: RE: Worker Thread and GetOverlappedResult…
Thanks arlie, Thanks a lot for sheding light on both the methods.For me
Me simply specifying Overlapped = {0}; and Waiting on the file Handle
works.Event method Still does not works:).
Regards…
Subodh
----- Original Message -----
From: Arlie Davis mailto:xxxxx
To: Windows System Software Devs Interest List
mailto:xxxxx
Sent: Friday, October 03, 2003 7:46 PM
Subject: [ntdev] RE: Worker Thread and GetOverlappedResult…
There are no such restrictions on which thread may issue
overlapped I/O. You are probably issuing the overlapped I/O
incorrectly. What completion mechanism are you using?
It sounds like you are using GetOverlappedResult in blocking
mode (bWait = TRUE). If so, then you are either providing an event
handle in OVERLAPPED.hEvent, or you are not. If you are not providing
an event handle, then GetOverlappedResult is trying to block on the file
handle – which is why it is blocking forever.
You need to do one of the following:
1) Create an event object, and set OVERLAPPED.hEvent to the
event’s handle, before calling DeviceIoControl. GetOverlappedResult
should then behave as you expect.
Or, 2) Set OVERLAPPED.hEvent to the file handle before calling
DeviceIoControl. This allows you to use the file handle itself as a
dispatcher object, and it will be signaled with the DeviceIoControl
request succeeds. I do NOT suggest doing this, though – it is sloppy,
and you’ll cause problems for yourself when you try to issue more than
one overlapped operation at a time.
Or, 3) Use a different completion mechanism, e.g. I/O completion
ports.
– arlie
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of subodh gupta
Sent: Tuesday, September 30, 2003 4:25 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Worker Thread and
GetOverlappedResult…
Hi,
I use overlapped Io in my application to interact with
my tdi filter driver.The Main thread in aplication opens the device with
FILE_FLAG_OVERLAPPED,creates a worker thread which issues the
DeviceIoControl Commands on behalf of application.
Now the Thread tries to use Overlapped Io In Device Io
Control call.I see the request going from user mode to kernel mode and
completing in driver in Windbg But In User mode the thread which is
waiting using GetOverlappedResult Call does not get wake up ?It hangs
forever.if i dont use overlapped things work fine.
Is there any problem like the thread which opened the
device using CreateFile Can Only issue the overlapped Io Request ?Since
the handle is within the same process i do not think this could be a
problem, But one the same hand Irp has got a thread Filed in
Tail.overlay which probably relates to the current thread am i right ?
Is the change of Thread is the problem ?
How can i overcome this problem ? By opening the device
in the worker thread proc ?
Please throw some light on the issue …
any help is appreciated…
–Subodh
—
Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
You are currently subscribed to ntdev as:
xxxxx@sublinear.org
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@softhome.net
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.</mailto:xxxxx></mailto:xxxxx>