Showing tag results for Code

Jan 6, 2012
Post comments count0
Post likes count0

Why did HeapFree fail with ERROR_POSSIBLE_DEADLOCK?

Raymond Chen
Raymond Chen

A customer reported that they were receiving some assertion failures because the function was failing with what they believed to be a valid heap block, and the function reported that the reason for failure was . What's going on? One of my colleagues asked the psychic question, "Is the process exiting?" "Why yes, in fact it is. How did you know?...

Code
Jan 5, 2012
Post comments count0
Post likes count1

When DLL_PROCESS_DETACH tells you that the process is exiting, your best bet is just to return without doing anything

Raymond Chen
Raymond Chen

When the function receives a reason code of , the increasingly-inaccurately-named parameter to is used to indicate whether the process is exiting. And if the process is exiting, then you should just return without doing anything. No, really. Don't worry about freeing memory; it will all go away when the process address space is destroyed. Don...

Code
Jan 4, 2012
Post comments count0
Post likes count0

Creating context menus on menus

Raymond Chen
Raymond Chen

Last week we looked at menu drag/drop. Another little-used menu feature added in Windows 2000 is the ability to show context menus on menus. The message is and the flag is . Let's demonstrate with a simple program. Start with the scratch program, and add the function just so our context menu can do something. When we receive the me...

Code
Dec 30, 2011
Post comments count0
Post likes count1

Using the MNS_DRAGDROP style: Menu rearrangement

Raymond Chen
Raymond Chen

In order to do drag-drop rearrangement of menus, you need four things, most of which we already know how to do. Dragging an item out of a menu. Check. Dropping an item into a menu. Check. Connecting the drag with the drop. Rearranging menu items in response to the operation. Let's do step 4 first, just to mix things up. ...

Code
Dec 29, 2011
Post comments count0
Post likes count0

Using the MNS_DRAGDROP style: Dropping in

Raymond Chen
Raymond Chen

Last time, we looked at using the style for dragging items out of a menu. Today, we'll look at dropping them in. Take the program from last time and make the following additions. First, let's add a second item to the menu. Yes, I hard-coded another path. This is a demo, not production code. Anyway, it's time to hook up the message: To...

Code
Dec 28, 2011
Post comments count0
Post likes count0

Using the MNS_DRAGDROP style: Dragging out

Raymond Chen
Raymond Chen

Windows 2000 introduced the menu style, which permits drag/drop operations in a menu. Nobody uses this style, probably because it's totally undiscoverable by the end-user. But I'll write a sample program anyway. Mind you, I knew nothing about the menu style until I started writing this entry. But I simply read the documentation, which say...

Code
Dec 27, 2011
Post comments count0
Post likes count0

Introducing the for-if 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 for-if anti-pattern, also known as "We'll sell you the whole seat, but you'll only need the edge." This is a special case of the for-case anti-pattern, where all but one of the cases is null. This can natural...

Code
Dec 23, 2011
Post comments count0
Post likes count0

How do I get the full path for the target of a shortcut file?

Raymond Chen
Raymond Chen

A customer was having trouble obtaining information from a shortcut file. "Here is a sample program that tries to print the target of a shortcut file, but it only gets the file name without a directory. How do I get the full path?" Recall that the structure contains only a file name in the member. It doesn't have any path information. The st...

Code
Dec 22, 2011
Post comments count0
Post likes count0

How do I determine programmatically whether a particular language is LTR or RTL?

Raymond Chen
Raymond Chen

Given an , how does one determine whether the language lays out left-to-right or right-to-left? One suggestion was simply to hard-code the list of known right-to-left languages, and if the language isn't on the list, then assume that it is left-to-right. This technique is clearly fragile, because Windows adds support for new languages not infrequen...

Code
Dec 19, 2011
Post comments count0
Post likes count0

Paint messages will come in as fast as you let them

Raymond Chen
Raymond Chen

There is a class of messages which are generated on demand rather than explicitly posted into a message queue. If you call or and the queue is empty, then the window manager will look to see if one of these generated-on-demand messages is due, messages like , , and . Neil wonders, "In that program that called 100,000 times, how many paint messa...

Code