The Old New Thing

Converting a byte[] to a System.String

For some reason, this question gets asked a lot. How do I convert a byte[] to a System.String? (Yes, this is a CLR question. Sorry.) You can use String System.Text.UnicodeEncoding.GetString() which takes a byte[] array and produces a string. Note that this is not the same as just blindly copying the bytes from the byte[] array into a ...

Even in computing, simultaneity is relative

Einstein discovered that simultaneity is relative. This is also true of computing. People will ask, "Is it okay to do X on one thread and Y on another thread simultaneously?" Here are some examples: You can answer this question knowing nothing about the internal behavior of those operations. All you need to know are some physics and...

How to find the Internet Explorer binary

For some reason, some people go to enormous lengths to locate the Internet Explorer binary so they can launch it with some options. The way to do this is not to do it. If you just pass "IEXPLORE.EXE" to the ShellExecute function [link fixed 9:41am], it will go find Internet Explorer and run it. ShellExecute(NULL, "open", "iexplore.exe...

Reading a contract from the other side: Application publishers

In an earlier article, I gave an example of reading a contract from the other side. Here's another example of how you can read a specification and play the role of the operating system. I chose this particular example because somebody wanted to do this and didn't realize that everything they needed was already documented; they just needed to...

Why do some structures end with an array of size 1?

Some Windows structures are variable-sized, beginning with a fixed header, followed by a variable-sized array. When these structures are declared, they often declare an array of size 1 where the variable-sized array should be. For example: typedef struct _TOKEN_GROUPS { DWORD GroupCount; SID_AND_ATTRIBUTES Groups[ANYSIZE_ARRAY]; } ...

Why can’t you treat a FILETIME as an __int64?

The FILETIME structure represents a 64-bit value in two parts: typedef struct _FILETIME { DWORD dwLowDateTime; DWORD dwHighDateTime; } FILETIME, *PFILETIME; You may be tempted to take the entire FILETIME structure and access it directly as if it were an __int64. After all, its memory layout exactly matches that of a 64-bit (little-...

Beware of non-null-terminated registry strings

Even though a value is stored in the registry as REG_SZ, this doesn't mean that the value actually ends with a proper null terminator. At the bottom, the registry is just a hierarchically-organized name/value database. And you can lie and get away with it. Lots of people lie about their registry data. You'll find lots of things that ...

Writing your own menu-like window

Hereby incorporating by reference the "FakeMenu" sample in the Platform SDK. It's in the winui\shell\fakemenu directory. For those who don't have the Platform SDK, what are you doing writing Win32 programs without the Platform SDK? Download it if it didn't come with your development tools. If for some reason you don't want the Platform SDK...

Never leave focus on a disabled control

One of the big no-no's in dialog box management is disabling the control that has focus without first moving focus somewhere else. When you do this, the keyboard becomes dead to the dialog box, since disabled windows do not receive input. For users who don't have a mouse (say, because they have physical limitations that confine them to the ...

Why .shared sections are a security hole

Many people will recommend using shared data sections as a way to share data between multiple instances of an application. This sounds like a great idea, but in fact it's a security hole. Proper shared memory objects created by the CreateFileMapping function can be secured. They have security descriptors that let you specify which users ...