I just wrote a quick, dirty little app for you that is a more general purpose method, and probably a little bit more than you're seeking, but you might find it quite useful. Instead of just searching for a single window by it's title (which is an extremely limiting process), this enumerates all windows. Then, given an HWND, you can use GetClassName() or GetWindowText(), et cetra to see if it's an application window you want to deal with. If so, it's children are enumerated until your target is acquired. Then it's just a matter of what you want to do to it. Enjoy.
Code:
#include <windows.h>
#include <tchar.h>
// use some default text if none is supplied on the command line
static LPCTSTR DEFAULT_MSG = _T("This text was automatically inserted.");
// a container to pass to arguments - the string and it's length
struct StringMsg {
int nLength;
LPCTSTR pszText;
};
// iteratively process each child window of the pass HWND
BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam) {
// first we determine the classname
TCHAR szClassName[32];
GetClassName(hwnd, szClassName, 32);
// if it is not an edit window, we'll just continue with
// the next child window
if (_tcsncmpi(szClassName, _T("EDIT"), 4) != 0)
return TRUE;
// otherwise we auto-type something into the edit window and
// cease the enumeration
StringMsg *pMsg = (StringMsg*)lParam;
for (int i = 0; i < pMsg->nLength; ++i)
SendMessage(hwnd, WM_CHAR, pMsg->pszText[i], 0);
return FALSE;
}
// this is called for every top-most HWND
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
TCHAR szClassName[32];
GetClassName(hwnd, szClassName, 32);
// determine if it is our target application window
if (_tcsncmpi(szClassName, _T("NOTEPAD"), 7) == 0)
// if it is, we'll enumerate it's children and let that
// enumeration handle finding the target control and doing
// our dirty work, then stop looking
if (!EnumChildWindows(hwnd, EnumChildProc, lParam))
return FALSE;
// otherwise we just keep looking
return TRUE;
}
// program entry point
// pass a string on the command line or let the default text
// be used
int _tmain(int argc, TCHAR **argv) {
StringMsg sm;
// argument? use it
if (argc > 1) {
sm.nLength = _tcslen(argv[1]);
sm.pszText = argv[1];
}
// none? use ours
else {
sm.nLength = _tcslen(DEFAULT_MSG);
sm.pszText = DEFAULT_MSG;
}
// enumerate all the top-level windows
EnumWindows(EnumWindowsProc, (LPARAM)&sm);
return 0;
}