HWND Hunter

When using Windows API functions such as SendMessage, it is often necessary to locate the HWND of an arbitrary control on an arbitrary window at runtime. HWND Hunter, designed to speed up this process, is a program that generates code to programmatically determine HWNDs using a Spy++ style window finding method. The following function, FindHWND_AIM(), was generated using HWND Hunter and locates the HWND of an open AOL Instant Messanger 5.9 window’s text box:

HWND FindHWND_AIM()
{
   // class type: Ate32Class
   HWND base = FindWindowS(“AIM_IMessage”, “- Instant Message”);
   if ( base == NULL ) return NULL;
   int scan2 = {8, 0};
   return IterateHWNDs(base, 2, scan);
}

This function can then be used in code such as the following:

#include <windows.h>
int main()
{
   HWND aim_hwnd = FindHWND_AIM();
   SendMessage(aim_hwnd, WM_SETTEXT, NULL, (LPARAM)“Voodoo!”);
   return 0;
}

All FindHWND_*() functions use a custom function (provided within HWND Hunter) called FindWindowS, which works much like FindWindow, except searches for a substring match instead of the exact string. Thus, “- Instant Message” matches the window caption “thekrispykremlin: thekrispykremlin – Instant Message”, which would be a window of me sending an IM myself. A parameter of NULL can be used for either (or both) class name and window caption, and will result in that parameter matching for any value, much like using an empty search string.

I wrote most of the code with a sinus headache, and thus it’s not exactly the cleanest code I’ve ever written. If you plan to venture into the source, you have been warned.

Download HWND Hunter

Download Source

Using HWND Hunter to locate the Notepad textbox.

1 Comment to HWND Hunter

  1. October 15, 2008 at 4:07 pm | Permalink

    add links! thanks!