Showing tag results for Code

Jul 2, 2008
Post comments count0
Post likes count1

Uninitialized floating point variables can be deadly

Raymond Chen

A colleague of mine related to me this story about uninitialized floating point variables. He had a function that went something like this, simplified for expository purposes. The infoType parameter specified which piece of information you're requesting, and depending on what you're asking for, one or the other of the output parameters may not con...

Code
Jun 23, 2008
Post comments count0
Post likes count1

Just because you're using a smart pointer class doesn't mean you can abdicate understanding what it does

Raymond Chen

It's great when you have a tool to make programming easier, but you still must understand what it does or you're just replacing one set of problems with another set of more subtle problems. For example, we discussed earlier the importance of knowing when your destructor runs. Here's another example, courtesy of my colleague Chris Ashton. This was...

Code
Jun 16, 2008
Post comments count0
Post likes count1

How do the common controls convert between ANSI and Unicode?

Raymond Chen

Commenter Chris Becke asks how the common controls convert ANSI parameters to Unicode, since the common controls are Unicode internally. Everything goes through , pretty much by definition. The ANSI code page is . That's what ACP stands for, after all. Now, there are some function families that do not use ANSI. The console subsystem, for example...

Code
Jun 11, 2008
Post comments count0
Post likes count1

If you say that you don't care about something, you shouldn't be upset that it contains garbage

Raymond Chen

There are many situations where you pass a structure to a function, and the function fills in the structure with information you request. In some cases, the function always fills in the entire structure (example: ). In other cases, you tell the function which bits of information you care about, to save the function the effort of computing somethin...

Code
Jun 9, 2008
Post comments count0
Post likes count1

Why are DLLs uninitialized in the "wrong" order?

Raymond Chen

This is really just a corollary to Why are DLLs unloaded in the "wrong" order. Exactly the same logic that explains why DLLs are unloaded in the "wrong" order also explains why they are uninitialized in the "wrong" order. Once you understand the first issue, the second comes for free; just change to in your analysis. Apply the logic to the sc...

Code
Jun 6, 2008
Post comments count0
Post likes count1

Why does OpenProcess succeed even when I add three to the process ID?

Raymond Chen

A customer noticed that if you add three to a process ID and pass it to the function, it still succeeds. Why is that? Well, first of all, I need to say up front that the behavior you're seeing is an artifact of the implementation and is not part of the contract. You're passing intentionally invalid parameters, what did you expect? The context of...

Code
Jun 2, 2008
Post comments count0
Post likes count1

Why are accelerators for hidden controls still active?

Raymond Chen

In the suggestion box, Serge Wautier asked why accelerators for hidden controls remain active. He's apparently rather insistent because he asked the question again a few months later. Asking the same question multiple times reduces the likelihood that I'll answer it. Consider yourself lucky that I wrote this answer before I noticed the duplicate...

Code
May 28, 2008
Post comments count0
Post likes count1

Reading a contract from the other side: SHSetInstanceExplorer and SHGetInstanceExplorer

Raymond Chen

Shell extensions that create worker threads need to call the function so that Explorer will not exit while the worker thread is still running. When your worker thread finishes, you release the that you obtained to tell the host program, "Okay, I'm done now, thanks for waiting." You can read this contract from the other side. Instead of thinkin...

Code
May 27, 2008
Post comments count0
Post likes count1

You can't give away something that isn't yours

Raymond Chen

This rule of real life applies to code as well. If something doesn't belong to you, then you can't give it away. For example, functions like and take ownership of the item that you pass it. In SetClipboardData's case, the memory block you pass as the parameter becomes the property of the clipboard. For it's the that becomes the property of ...

Code