views:

849

answers:

8

I have noticed that there are quite a few community wikis about "Tips & Tricks" or "Hidden Features" in programming languages and APIs here at Stack Overflow. But I could not find any about my own personal favourites: Win32 API and Delphi. Therefore I start my "own" CW about Win32 API.

There are (at least) two kinds of Win API users: those that have been brought up using Windows API in C/C++, and those that have been brought up using some level of abstraction above the Windows API. I belong to the latter category, being brought up using Delphi's VCL. But over the last five years, I have become increasingly interested in the underlaying API of the Windows operating system, and today I work a lot with it.

Depending on which category a programmer belongs to, he (or possibly she) will think that different things are "cool" in the Windows API. For instance, whereas a VCL-brought up developer might think it it very cool to

var
  errIcon: HICON;
begin
  errIcon := LoadIcon(0, IDI_ERROR);
  DrawIcon(Canvas.Handle, 10, 10, errIcon),

a programmer brought up using Windows API in C will not be as impressed.

But no matter how you are "brought up": what are the coolest "tricks" in Windows API?

I start by listing a few of my own favourites, some of which are more "cool" than "useful", though:

  • LoadIcon and MessageBeep can load/play system default icons and sounds.
  • Open the CD tray: mciSendString('Set cdaudio door open wait', nil, 0, 0);
  • Fade out the screen (Windows Vista and later) and turn off the monitor: SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
  • GetWindowDC(GetDesktopWindow) returns the DC of the desktop.
  • Using GetSystemMenu one can add/change/remove menu items on the system menu of a window. Sometimes very nice.
  • DrawThemeBackground and the other theme service functions, which lets you draw details of the current theme whereever you want them. Candy: http://msdn.microsoft.com/en-us/library/bb773210(VS.85).aspx
+4  A: 
SendMessage(HWND_BROADCAST, WM_CLOSE, 0, 0);

Pretty much shuts down all the apps running on your machine. Fun to witness in action, but deploy with caution... Maybe this isn't a trick. Except on your (soon-to-be-former) friends.

quixoto
Would have been more useful 4 days ago (I'm writing this on April 5).
Mark Ransom
+1  A: 

Reparse points. Almost feels like Linux.

kenny
Unfortunately (for this question anyway), those are a filesystem feature, not a Win32 API feature.
Ben Voigt
+8  A: 

The fact that there is a Windows API.

We just take for granted that there is this API system that effectively allows us to do anything in the Windows environment. What all it has enabled into being and the fact that it is so well documented (esp. thanks to Charles Petzold) is nothing short of miraculous.

I do my personal day-to-day coding in the .NET framework and I love how easy it is. But I also know that if I can think of something Windows-based that isn't possible in managed .NET code, I can dig through the WinAPI and still find a way to make my mind-child possible.

Dinah
The windows API is actually not very well documented at all compared to other large APIs. Here's a great quote from the WINE project:"Don't make the mistake of trusting MSDN too much. It contains incorrect information - it's usually not written by the same people who wrote the code." -- <http://www.winehq.org/developer-cheatsheet>
bartsimpson
+15  A: 

Raymond Chen.

quixoto
CHEEEEEEEN! Lame jokes aside, I agree wholeheartedly. I have The Old New Thing on my RSS reader and it's often enough I get all impressed from what he writes.
Kawa
Raymond Chen is such a beast.
Pierreten
A: 

Did you ever want to check if an thread is alive? Use

DWORD ExitCode;
GetExitCodeThread (Thread, &ExitCode);
if (ExitCode == STILL_ALIVE)
{
    TerminateThread (Thread)
}
evilpie
Correct way to see if a thread is alive is to test whether the handle is signaled. For example, WaitForSingleObject with a zero timeout.
Ben Voigt
+2  A: 

Spy++ and SendMessage enables you to do all sorts of cool things.

You can find out all sorts of information on how Windows and any other application is built. And you can tie into them by manually sending keystrokes. You can find windows hierarchies and send messages to them. You can do things like close the start button.

Another cool aspect is windows hooks.

Brian R. Bondy
windows hooks? Is it possible for a feature to be pure evil and still be cool at the same time? If so, I propose shatter attacks.
Ben Voigt
@Ben: There are some shady uses but also they provide some very useful features.
Brian R. Bondy
@Brian: such as detecting when a window gets focus, or is moved. I use these hooks in my mascot framework to detect when the character has to be repositioned. It beats a timed loop in epic ways.
Kawa
Windows hooks are phased out, no? I think they were documented only in the archieve edition of MSDN lib as early as 1999.
TheBlastOne
No they are not phased out.
Brian R. Bondy
+3  A: 

WSAAsyncSelect, so you can have efficient interactive applications that do network stuff in the background without either losing responsiveness or putting up with multithreading headaches.

And MsgWaitForMultipleObjectsEx. Using anything else (GetMessage, PeekMessage) for the basis of one's main event loop is insane. And building a framework using anything else should be illegal.

Ben Voigt
+1  A: 

You can find some very useful & time saving stuff if you rummage about in the Shell Lightweight Utility API, particularly path, string & stream functions.

Alex K.
Very good post, although the link provided was not very useful (all dead links on it). However, the MSDN TOC is here: http://msdn.microsoft.com/en-us/library/bb759844(VS.85).aspx. We have a great number of cool functions here, indeed. http://msdn.microsoft.com/en-us/library/bb773569(VS.85).aspx is only one of them. Thanks again!
Andreas Rejbrand
http://msdn.microsoft.com/en-us/library/bb773836(VS.85).aspx Cool!
Andreas Rejbrand