Showing results for Code - The Old New Thing

Aug 26, 2010
Post comments count0
Post likes count0

How do I recover the window handle passed to ShellExecute?

Raymond Chen
Raymond Chen

A customer had the following question: I'm using the function to launch a new process and am passing the handle to my application's main window as the parameter. From the new process, I want to get information from the old process, and to do that, I need the window handle. How can I recover that window handle from the new process? You can't. ...

Code
Aug 23, 2010
Post comments count0
Post likes count0

Be careful that your splash screen doesn't squander the foreground love

Raymond Chen
Raymond Chen

Commenter Erbi has a program which creates a splash screen on a background thread while the main thread initializes. "I create and then destroy this splash screen window just before creating and displaying the main window." The problem is that the main window fails to obtain foreground activation. Commenting out the code that creates the splash scr...

Code
Aug 13, 2010
Post comments count0
Post likes count0

When do I need to use GC.KeepAlive?

Raymond Chen
Raymond Chen

Finalization is the crazy wildcard in garbage collection. It operates "behind the GC", running after the GC has declared an object dead. Think about it: Finalizers run on objects that have no active references. How can be a reference to an object that has no references? That's just crazy-talk! Finalizers are a Ouija board, permitting dead objec...

Code
Aug 12, 2010
Post comments count0
Post likes count0

How can I find all objects of a particular type?

Raymond Chen
Raymond Chen

More than one customer has asked a question like this: I'm looking for a way to search for all instances of a particular type at runtime. My goal is to invoke a particular method on each of those instances. Note that I did not create these object myself or have any other access to them. Is this possible? Imagine what the world would be like if...

Code
Aug 6, 2010
Post comments count0
Post likes count0

Some known folders cannot be moved, but others can, and you'll just have to accept that

Raymond Chen
Raymond Chen

Locations in the shell known folder system can be marked as , which makes them immovable. Conversely, if a file system folder is not immovable, then it can be moved. This dichotomy appears simple and unworthy of discussion, except that customers sometimes have trouble incorporating this concept into their world view. I have some code that calls...

Code
Jul 29, 2010
Post comments count0
Post likes count0

Why is my icon being drawn at the wrong size when I call DrawIcon?

Raymond Chen
Raymond Chen

Some time ago I had a problem with icon drawing. When I tried to draw an icon with it ended up being drawn at the wrong size. A call to confirmed that the icon was 48×48, but it drew at 32×32. The answer is documented in a backwards sort of way in the function, which says at the bottom, To duplicate DrawIcon (hDC, X, Y, hIcon),...

Code
Jul 23, 2010
Post comments count0
Post likes count1

If I'm not supposed to call IsBadXxxPtr, how can I check if a pointer is bad?

Raymond Chen
Raymond Chen

Some time ago, I opined that should really be called and you really should just let the program crash if somebody passes you a bad pointer. It is common to put pointer validation code at the start of functions for debugging purposes (as long as you don't make logic decisions based on whether the pointer is valid). But if you can't use , how can...

Code
Jul 19, 2010
Post comments count0
Post likes count0

To enable and disable a window, use the EnableWindow function

Raymond Chen
Raymond Chen

Commenter Chris 'Xenon' Hanson points out that fiddling with the style directly via leads to strange behavior. However it isn't the case that "most widget classes work fine." Reaching in and fiddling the style bit directly is like reaching into a program's internal variables and just changing the values: All the other work that is associated wi...

Code