Showing tag results for Code

Apr 24, 2013
Post comments count0
Post likes count1

Dark corners of C/C++: The typedef keyword doesn’t need to be the first word on the line

Raymond Chen
Raymond Chen

Here are some strange but legal declarations in C/C++: int typedef a; short unsigned typedef b; By convention, the typedef keyword comes at the beginning of the line, but this is not actually required by the language. The above declarations are equivalent to typedef int a; typedef short unsigned b; The C language (but not C++) also permits...

Code
Apr 22, 2013
Post comments count0
Post likes count1

Getting the current selection from an Explorer window

Raymond Chen
Raymond Chen

Today's Little Program prints the current selection in all open Explorer windows. (This is an alternative to the C++ version that involves a ridiculous amount of typing.) var shellWindows = new ActiveXObject("Shell.Application").Windows(); for (var i = 0; i < shellWindows.Count; i++) { var w = shellWindows.Item(i); WScript.StdOut.WriteLine...

Code
Apr 19, 2013
Post comments count0
Post likes count3

Why does CoCreateInstance work even though my thread never called CoInitialize? The curse of the implicit MTA

Raymond Chen
Raymond Chen

While developing tests, a developer observed erratic behavior with respect to : In my test, I call and it fails with . Fair enough, because my test forgot to call . But then I went and checked the production code: In response to a client request, the production code creates a brand new thread to service the request. The brand new thread does ...

Code
Apr 15, 2013
Post comments count0
Post likes count1

Using opportunistic locks to get out of the way if somebody wants the file

Raymond Chen
Raymond Chen

Opportunistic locks allow you to be notified when somebody else tries to access a file you have open. This is usually done if you want to use a file provided nobody else wants it. For example, you might be a search indexer that wants to extract information from a file, but if somebody opens the file for writing, you don't want them to get Sharin...

Code
Apr 12, 2013
Post comments count0
Post likes count3

Is it legal to have a cross-process parent/child or owner/owned window relationship?

Raymond Chen
Raymond Chen

A customer liaison asked whether it was legal to use to create a parent/child relationship between windows which belong to different processes. "If I remember correctly, the documentation for used to contain a stern warning that it is not supported, but that remark does not appear to be present any more. I have a customer who is reparenting windo...

Code
Apr 8, 2013
Post comments count0
Post likes count0

The managed way to retrieve text under the cursor (mouse pointer)

Raymond Chen
Raymond Chen

Today's Little Program is a managed version of the text-extraction program from several years ago. It turns out that it's pretty easy in managed code because the accessibility folks sat down and wrote a whole framework for you, known as UI Automation. (Some people are under the mistaken impression that UI Automation works only for extracting d...

Code
Apr 5, 2013
Post comments count0
Post likes count2

How do I wait until all processes in a job have exited?

Raymond Chen
Raymond Chen

A customer was having trouble with job objects, specifically, the customer found that a Wait­For­Single­Object on a job object was not completing even though all the processes in the job had exited. This is probably the most frustrating part of job objects: A job object does not become signaled when all processes have exited. The s...

Code
Apr 4, 2013
Post comments count0
Post likes count1

Don’t forget, the fourth parameter to ReadFile and WriteFile is sometimes mandatory

Raymond Chen
Raymond Chen

The Read­File and Write­File functions have a parameter called lp­Number­Of­Byte­Read, which is documented as __out_opt LPDWORD lpNumberOfBytesRead, // or __out_opt LPDWORD lpNumberOfBytesWritten, "Cool," you think. "That parameter is optional, and I can safely pass NULL." My program runs fine if standard output ...

Code
Apr 3, 2013
Post comments count0
Post likes count1

How can I move an HTREEITEM to a new parent?

Raymond Chen
Raymond Chen

Suppose you have a TreeView control, and you created an item in it, and you want to move the to a new parent. How do you do that? You can't, at least not all in one motion. You will have to delete the and then re-create it in its new location. If you want to move an within the same parent (say, to reorder it among its siblings), then you...

Code
Mar 29, 2013
Post comments count0
Post likes count1

How do I convert a method name to a method index for the purpose of INTERFACEINFO?

Raymond Chen
Raymond Chen

The IMessage­Filter::Handle­Incoming­Call method describes the incoming call by means of an INTERFACE­INFO structure: typedef struct tagINTERFACEINFO { LPUNKNOWN pUnk; IID iid; WORD wMethod; } INTERFACEINFO, *LPINTERFACEINFO; The wMethod is a zero-based index of the method within the interface. For example, IUnknown...

Code