Showing results for Code - The Old New Thing

Dec 7, 2004
Post comments count0
Post likes count0

Dragging a shell object, part 2: Enabling the Move operation

Raymond Chen
Raymond Chen

Let's say that we did want to support Move in our drag/drop program, for whatever reason. Let's do it with some scratch file instead of clock.avi, though. Create a file somewhere that you don't mind losing; let's say it's C:\throwaway.txt. Change the function OnLButtonDown as follows: void OnLButtonDown(HWND hwnd, BOOL fDoubleClick, ...

Code
Nov 30, 2004
Post comments count0
Post likes count0

What’s the difference between GetKeyState and GetAsyncKeyState?

Raymond Chen
Raymond Chen

I've seen some confusion over the difference between the function and the function. returns the virtual key state. In other words, reports the state of the keyboard based on the messages you have retrieved from your input queue. This is not the same as the physical keyboard state: When should you use and when should you use ? For ...

Code
Nov 26, 2004
Post comments count0
Post likes count0

Simple things you can do with the ShellExecuteEx function

Raymond Chen
Raymond Chen

Here's a tiny little program: #include <windows.h> #include <shellapi.h> int __cdecl main(int argc, char **argv) { if (argc == 3) { SHELLEXECUTEINFO sei = { sizeof(sei) }; sei.fMask = SEE_MASK_FLAG_DDEWAIT; sei.nShow = SW_SHOWNORMAL; // added 27 Nov sei.lpVerb = argv[1]; sei.lpFile = argv[2]; ShellExecuteEx...

Code
Nov 23, 2004
Post comments count0
Post likes count0

Why do folders like “My Pictures” come back after I delete them?

Raymond Chen
Raymond Chen

Some people are offended by the special folders like "My Pictures" and "My Music" and delete them, only to find them being re-created. What's going on? Windows itself is okay with you deleting those folders. Some corporations, for example, remove those folders from their employees' machines because they don't want the employees looking at picture...

Code
Nov 19, 2004
Post comments count0
Post likes count0

The various ways of sending a message

Raymond Chen
Raymond Chen

There are several variations on the SendMessage function, but some are special cases of others. The simplest version is SendMessage itself, which sends a message and waits indefinitely for the response. The next level up is SendMessageTimeout which sends a message and waits for the response or until a certain amount of time has elapsed. Send...

Code
Nov 17, 2004
Post comments count0
Post likes count0

How do I break an integer into its component bytes?

Raymond Chen
Raymond Chen

Warning: .NET content ahead. For some reason, this gets asked a lot. To break an integer into its component bytes, you can use the BitConverter.GetBytes method: int i = 123456; byte[] bytes = BitConverter.GetBytes(i); After this code fragment, the byte array contains { 0x40, 0xE2, 0x01, 0x00 }. Update 11am: The endian-ness of th...

Code
Nov 11, 2004
Post comments count0
Post likes count0

Advantages of knowing your x86 machine code

Raymond Chen
Raymond Chen

Next time you find yourself debugging in assembly language (which for some of us is the only way we debug), here are some machine code tricks you may wish to try out: 90 This is the single-byte NOP opcode. If you want to patch out code and don't want to think about it, just whack some 90's over it. To undo it, you have to patch the or...

Code
Oct 25, 2004
Post comments count0
Post likes count1

Accessing the current module’s HINSTANCE from a static library

Raymond Chen
Raymond Chen

If you're writing a static library, you may have need to access the HINSTANCE of the module that you have been linked into. You could require that the module that links you in pass the HINSTANCE to a special initialization function, but odds are that people will forget to do this. If you are using a Microsoft linker, you can take advantage of a ...

Code
Oct 21, 2004
Post comments count0
Post likes count0

Let WMI do the heavy lifting of determining system information

Raymond Chen
Raymond Chen

Windows Management Instrumentation is a scriptable interface to configuration information. This saves you the trouble of having to figure it out yourself. For example, here's a little program that enumerates all the CPUs in your system and prints some basic information about them. var locator = WScript.CreateObject("WbemScripting.SWbemLocator...

Code