Showing results for Code - The Old New Thing

Jan 1, 2010
Post comments count0
Post likes count0

Your program assumes that COM output pointers are initialized on failure; you just don't realize it yet

Raymond Chen
Raymond Chen

We saw last time that the COM rules for output pointers are that they must be initialized on return from a function, even if the function fails. The COM marshaller relies on this behavior, but then again, so do you; you just don't realize it yet. If you use a smart pointer library (be it ATL or boost or whatever), you are still relying on output...

Code
Dec 31, 2009
Post comments count0
Post likes count0

Why does COM require output pointers to be initialized even on failure?

Raymond Chen
Raymond Chen

One of the rules of COM is that if a parameter is marked as an output pointer, then you have to initialize the thing it points to, even if your function failed and you have nothing to return. For example, we saw the problems that can occur if you forget to set the output pointer to in the method. Why does COM have this rule? Doesn't it know that...

Code
Dec 24, 2009
Post comments count0
Post likes count0

Why don't we create a special class of programs which can break the normal rules?

Raymond Chen
Raymond Chen

In response to a discussion of why the window handle limit is 10,000, commenter Juan wondered why we don't create a special class of programs which can exceed the 10,000 handle limit and otherwise bypass the normal operation of the system. This is another case of the tragedy of special treatment: Eventually, nothing is special any more. If ther...

Code
Dec 23, 2009
Post comments count0
Post likes count0

Why is it possible to destroy a critical section while it is in use?

Raymond Chen
Raymond Chen

Some time back, Stu wondered why it is possible to destroy a critical section while it is in use. Well, there's nothing stopping you from creating a file that contains these lines: and then telling your compiler to turn it into a program. It's not like a bolt of lightning is going to come out of the sky and zap you before you hit the Enter k...

Code
Dec 18, 2009
Post comments count0
Post likes count0

I got an array with plenty of nuthin'

Raymond Chen
Raymond Chen

A customer reported a memory leak in the function : We found the following memory leak in the function . Please fix it immediately because it causes our program to run out of memory. If the 's type is , then the corresponding is leaked and not cleaned up. Right now, we are temporarily working around this in our program by inserting code b...

Code
Dec 11, 2009
Post comments count0
Post likes count0

The format of bitmap resources

Raymond Chen
Raymond Chen

Another in a sporadic series on the format of Win32 resources. Here's a question from a customer: I'm noticing some strange behavior: When I call then on an embedded bitmap, the data being returned by is not a properly formatted bitmap. The data is missing the , but the rest of the file is there. also states that the bitmap resource is 14 byt...

Code
Dec 8, 2009
Post comments count0
Post likes count0

How do I determine the processor's cache line size?

Raymond Chen
Raymond Chen

When doing high-performance computing, you need to worry about the CPU cache line size in order to avoid issues like false sharing. But how can you determine the processor's cache size? The function will give you characteristics of the logical processors in use by the system. You can walk the returned by the function looking for entries of type...

Code
Dec 2, 2009
Post comments count0
Post likes count0

A shell extension is a guest in someone else's house; don't go changing the carpet

Raymond Chen
Raymond Chen

A customer was running into this problem with a shell extension: I am writing a shell namespace extension. I need to get data from a COM server, which requires impersonation via with . As I am just writing an extension into , I am not able to call , anymore from my extension. Is there a way I can start by setting in its COM initialization? I w...

Code
Nov 25, 2009
Post comments count0
Post likes count0

How do I get the command line of another process?

Raymond Chen
Raymond Chen

Win32 doesn't expose a process's command line to other processes. From Win32's point of view, the command line is just a conveniently initialized parameter to the process's startup code, some data copied from the launching process to the new process and forgotten. We'll get back to the Win32 point of view a little later. If you look around in W...

Code
Nov 20, 2009
Post comments count0
Post likes count0

The difference between assignment and attachment with ATL smart pointers

Raymond Chen
Raymond Chen

Last time, I presented a puzzle regarding a memory leak. Here's the relevant code fragment: The problem here is assigning the return value of to a smart pointer instead of attaching it. The function creates a memory stream and returns a pointer to it. That pointer has a reference count of one, in accordance with COM rules that a function ...

Code