Showing tag results for Code

Apr 5, 2012
Post comments count0
Post likes count0

You can use an OVERLAPPED structure with synchronous I/O, too

Raymond Chen
Raymond Chen

Even if you didn't open a file with , you can still use the structure when you issue reads and writes. Mind you, the I/O will still complete synchronously, but you can take advantage of the other stuff that has to offer. Specifically, you can take advantage of the and members to issue the I/O against a file location different from the curren...

Code
Apr 3, 2012
Post comments count0
Post likes count0

Introducing the unrolled-switch anti-pattern

Raymond Chen
Raymond Chen

Over the years, I've seen a bunch of coding anti-patterns. I figured maybe I'll share a few. Today, I'll introduce what I'm calling the unrolled-switch anti-pattern, also known as "Specialization is always faster, right?" As we all know, special-case code is faster than general-purpose code. Instead of writing slow general-purpose code: we ...

Code
Mar 30, 2012
Post comments count0
Post likes count0

How do I perform shell file operations while avoiding shell copy hooks?

Raymond Chen
Raymond Chen

Okay, the subject line of the article gives away the answer to the puzzle, but here's the puzzle anyway: A customer reported a problem with the function: Consider the following program: If "a" is a file, then everything works fine, but if it's a directory, then Application Verifier raises the following error: Heap violation detected Memor...

Code
Mar 29, 2012
Post comments count0
Post likes count0

Why can't I delete a file immediately after terminating the process that has the file open?

Raymond Chen
Raymond Chen

A customer discovered a bug where terminating a process did not close the handles that the process had open, resulting in their emergency cleanup code not working: Their workaround was to insert a call to before deleting the file. The customer wanted to know whether they discovered a bug in , and they were concerned that their workaround could...

Code
Mar 26, 2012
Post comments count0
Post likes count0

Why does a maximized window have the wrong window rectangle?

Raymond Chen
Raymond Chen

Commenter configurator wonders why the maximum size for a form is the screen size plus (12,12). Somebody else wonders why it's the screen size plus (16,16). Let's start by rewinding the clock to Windows 3.0. When you maximize a window, the default position of the window is such that all the resizing borders hang "off the edges of the scree...

Code
Mar 22, 2012
Post comments count0
Post likes count0

Why does my window style change when I call SetWindowText?

Raymond Chen
Raymond Chen

A customer observed some strange behavior with window styles: We ran some weird behavior: Calling the function causes a change in window styles. Specifically, calling results in the and messages, and sometimes the result is that the style is removed. Is this a bug? What would cause this? The message sends the message to the control, at wh...

Code
Mar 21, 2012
Post comments count0
Post likes count0

Isn't there a race condition in GetFileVersionInfoSize?

Raymond Chen
Raymond Chen

In response to my explanation of what the parameter in is used for, Steve Nuchia wonders if there's a race condition between the time you get the size and the time you ask for the data. Yes, there is a race condition, but calling the function in a loop won't help because the function does not report that the buffer is too small to hold all th...

Code
Mar 16, 2012
Post comments count0
Post likes count0

Memory allocation functions can give you more memory than you ask for, and you are welcome to use the freebies too, but watch out for the free lunch

Raymond Chen
Raymond Chen

Memory allocation functions like , , , and all have the property that they can return more memory than you requested. For example, if you ask for 13 bytes, you may very well get a pointer to 16 bytes. The corresponding functions return the actual size of the memory block, and you are welcome to use all the memory in the block up to the actual si...

Code
Mar 15, 2012
Post comments count0
Post likes count0

Why does the VerQueryValue function give me the wrong file version number?

Raymond Chen
Raymond Chen

A customer was writing a test to verify that their patching system was working properly, but they found that even after the patch was installed, a call to reported that the file was still the original version. Why was the function reporting the wrong version? Recall that the version resource is, well, a resource. And one of the things that happe...

Code