Showing tag results for Code

Sep 5, 2014
Post comments count0
Post likes count1

You can use a file as a synchronization object, too

Raymond Chen
Raymond Chen

A customer was looking for a synchronization object that had the following properties: Can be placed in a memory-mapped file. Can be used by multiple processes simultaneously. Bonus if it can even be used by different machines simultaneously. Does not leak resources if the file is deleted. It turns out there is already a synchronization ob...

Code
Sep 4, 2014
Post comments count0
Post likes count1

Aha, I have found a flaw in the logic to detect whether my program is running on 64-bit Windows

Raymond Chen
Raymond Chen

Some time ago, I described how to detect programmatically whether you are running on 64-bit Windows, and one of the steps of the algorithm was "If you are a 64-bit program, then you are running on 64-bit Windows, because 32-bit Windows cannot run 64-bit programs." Every so often, somebody will claim that they found a flaw in this logic: "This alg...

Code
Aug 29, 2014
Post comments count0
Post likes count2

How can I detect that a user's SID has changed and recover their old data?

Raymond Chen
Raymond Chen

A customer maintained a database which recorded information per user. The information in the database is keyed by the user's SID. This works out great most of the time, but there are cases in which a user's SID can change. "Wait, I thought SIDs don't change." While it's true that SIDs don't change, it is also true that the SID associated with a u...

Code
Aug 28, 2014
Post comments count0
Post likes count2

Taking advantage of the fact that the handle returned when you create a kernel synchronization object has full access regardless of the actual ACL

Raymond Chen
Raymond Chen

A customer wanted some help deciding what security attributes to place on an event object intended to be used by multiple security contexts. We have two processes, call them A and B, running in different security contexts. I have an event that process A creates and shares with process B. The only thing process A does with the event is signal it,...

Code
Aug 25, 2014
Post comments count0
Post likes count1

How do I read the "Double-click to open an item (single-click to select)" setting in Folder Options?

Raymond Chen
Raymond Chen

Today's Little Program reports whether the Double-click to open an item (single-click to select) option is selected in the Folder Options dialog. A customer wanted to know how to do this, presumably so that their program would respect the setting and adjust its user interface to match. The flag and member name is kind of weird. The ability to s...

Code
Aug 22, 2014
Post comments count0
Post likes count1

Dispelling the myths, rumors, and innuendo surrounding the QueryPerformanceCounter function

Raymond Chen
Raymond Chen

The function has been the subject of much rumor and innuendo. In response to all the confusion, the kernel folks put together a page which tries to settle the controversy once and for all. It discusses the history of QPC over the ages, the problems it had on earlier versions of Windows or older firmware (which is probably where a lot of the myth...

Code
Aug 22, 2014
Post comments count0
Post likes count1

Why does the OpenThread function behave differently when the target thread belongs to another process?

Raymond Chen
Raymond Chen

A customer discovered strange behavior in the function and wondered whether it was expected. We use the function to obtain a thread handle with , passing in a valid thread ID. We later pass this handle to to get the thread exit code. We have found that the function succeeds if the thread in question belongs to another process, provided the thre...

Code
Aug 21, 2014
Post comments count0
Post likes count1

Some reasons not to do anything scary in your DllMain, part 3

Raymond Chen
Raymond Chen

In the same week, the shell team was asked to investigate two failures. The first one was a deadlock in Explorer. The participating threads look like this: The shell extension caused this problem because it ignored the rule against calling shell and COM functions from the entry point, as specifically called out in the documentation as example...

Code
Aug 18, 2014
Post comments count0
Post likes count1

Deleting elements from an JavaScript array and closing up the gaps

Raymond Chen
Raymond Chen

Today's Little Program is an exercise that solves the following problem: Given a JavaScript array and an unsorted array (possibly containing duplicates), calculate the result of deleting all of the elements from the original array as specified by the indices. For example, suppose and . The indices specify that elements 2 (charles), 4 (eve...

Code
Aug 15, 2014
Post comments count0
Post likes count1

The scope of the C# checked/unchecked keyword is static, not dynamic

Raymond Chen
Raymond Chen

C# has operators and to control the behavior of the language in the face of integer overflow. There are also and statements which apply the behavior to blocks of statements rather than single expressions. Why, then, doesn't this code below raise an overflow exception? (Mini-exercise: Why couldn't I have just written ?) The answer is th...

Code