Showing tag results for Code

Sep 23, 2009
Post comments count0
Post likes count1

The COM marshaller uses the COM task allocator to allocate and free memory

Raymond Chen

It should be second nature to you that the code which allocates memory and the code which frees memory need to use the same allocator. Most of the time, you think of it as "If you allocate memory, you need to free it with the corresponding mechanism," but this sentence works in the reverse direction as well: If you hand memory to a function that w...

Code
Sep 21, 2009
Post comments count0
Post likes count1

What is the logic behind the thumb size and position calculation in scroll bars?

Raymond Chen

Commenter sarathc asks, "How do we implement a custom scroll bar as Windows does? What is the logic behind the thumb size and position calculation? How we could dynamically manage it?" Let's look at the three questions in turn. To implement a custom scroll bar... don't do it. It's just not worth the effort, and there will almost always be little...

Code
Sep 14, 2009
Post comments count0
Post likes count1

Can you create an information context for the display?

Raymond Chen

Adrian McCarthy asks, "Can you create an information context for the display? ... I can call CreateDC("DISPLAY"), but perhaps that wouldn't generalize for a multiple-monitor display with different settings on each screen. I'm trying to avoid constantly creating and destroying DCs when all I need to do is measure strings, check color depth, dpi, etc...

Code
Sep 11, 2009
Post comments count0
Post likes count1

If you're handling an out of memory exception, you probably shouldn't allocate memory

Raymond Chen

With the assistance of Application Verifier, specifically, low resource simulation (also known as fault injection), a tester found a stack overflow condition. As we learned earlier, the important thing to look at when studying a stack overflow is the repeating section. When this stack trace was shown to the development team, they instantly re...

Code
Sep 4, 2009
Post comments count0
Post likes count1

Reading the error message carefully can help you see how the computer misinterpreted what you typed

Raymond Chen

The details have been changed since they aren't important but the lesson is the same. A customer had the following problem with a command-line tool: I've created a taglist but I can't seem to get it to work with the command. When I ask it to track the taglist, it can't find it. But if I ask for all my taglists, there it is. Yes, the com...

Code
Aug 28, 2009
Post comments count0
Post likes count1

The great thing about naming conventions is that not everybody will follow them

Raymond Chen

The naming convention for is «facility»«severity»«name», where the facility portion (and the underscore) is omitted if the facility is or . Good luck finding anybody who follows this naming convention. Okay, fine, if you look closely you might be able to find some people who do. Actually, I guess I was a bi...

Code
Aug 21, 2009
Post comments count0
Post likes count1

Restating the obvious about the WM_NOTIFY message

Raymond Chen

It appears that people seemed to appreciate restating the obvious about the message, so I'll try it again with the message. The message is typically used by a control to communicate with its parent, either to provide information, request it, or both. Although that is the typical use, there are exceptions. For example, property sheets send th...

Code
Aug 17, 2009
Post comments count0
Post likes count1

The format rectangle is recalculated whenever the window resizes, and that's a good thing

Raymond Chen

Reader asmguru62 asks why a custom format rectangle of a multi-line edit control is lost when the edit control is resized. That's right, the format rectangle is recalculated whenever the window resizes, and that's a good thing. Imagine if it weren't recalculated. You create a multi-line edit control. Like many programs, you might create it at a ...

Code
Aug 14, 2009
Post comments count0
Post likes count1

Why can't I declare a type that derives from a generic type parameter?

Raymond Chen

A lot of questions about C# generics come from the starting point that they are just a cutesy C# name for C++ templates. But while the two may look similar in the source code, they are actually quite different. C++ templates are macros on steroids. No code gets generated when a template is "compiled"; the compiler merely hangs onto the source co...

Code
Aug 13, 2009
Post comments count0
Post likes count1

Common gotchas when writing your own p/invoke

Raymond Chen

If you're looking to get into some p/invoke action, you'd be well-served to check out the pinvoke wiki to see if somebody else has done it too. If what you need isn't there, you may end up forced to write your own, and here are some gotchas I've seen people run into: C++ and Win32 are not the same as C# (aka ). In Win32, is ...

Code