Hi
To know if Word is running, one solution is to ask Windows to enumerate all windows and check
if Word is in the enumeration list by checking the window title.
To enum the Windows, use the function EnumWindows. This function needs two parameters; the first one is a pointer to an aplication-defined callback function and the second is an application-defined data passed to the callback function.
The callback function is called by the function EnumWindows for every window found in the machine, giving the opportunity to the called function to do something. The fisrt parameter of the callback function is the handle of the window found by EnumWindows.
From this handle, we'll get the pointer to that window and then the window title. Then a simple check for the words 'Microsoft Word' will tell you thet Word is open.
CWnd* pWnd = CWnd::FromHandle( hwnd);
CString str;
if ( pWnd != ( CWnd*) NULL) // Safety Check
{
// Get Window Title
pWnd->GetWindowText( str);
// Check if this is Word
if ( str.Find( "Microsoft Word", 0) >= 0)
{
YES this is Word running !!!!!!!!!!
......
For C++ rules, a callback function must be declared static. Static data members in a class are shared among all of the instances of that class. As Static methods in a class can only access to static variables in the class, one way to pass data between caller and call back is the second parameter.
In the code below, this is the handle of the window that called the EnumWindows function.
This handle will be passed as the second parameter (LPARAM) of the callback. Retrieving back a pointer to the window from the handle, will allow the update of a 'normal' member of the class. In this case, this is the member m_bFound.
CTestDlgDlg* pParentWnd =
( CTestDlgDlg* ) CWnd::FromHandle( ( HWND) lParam);
pParentWnd->m_bFound = TRUE;
DO Not forget to declare the callback as follow inside the class:
public:
static BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam);
See the whole picture now ...
HTH
Thierry
EMail: Thierry.Marneffe@swing.be
void CTestDlgDlg::OnFind() // Handler of a 'Find' button
{
HWND hwnd = this->GetSafeHwnd();
// Reset Member tracing Effective Word found
m_bFound = FALSE;
// Call the Enum Function
if ( ! EnumWindows( EnumWindowsProc, ( LPARAM) hwnd))
AfxMessageBox( "Problem getting Enum "

;
AfxMessageBox( m_bFound ? "Got it " : " No Word Running"

;
}
BOOL CALLBACK CTestDlgDlg::EnumWindowsProc( HWND hwnd, LPARAM lParam)
{
// Get a Pointer to the window found by EnumWindows function (first param)
CWnd* pWnd = CWnd::FromHandle( hwnd);
CString str;
if ( pWnd != ( CWnd*) NULL) // Safety Check
{
// Get window Title
pWnd->GetWindowText( str);
// Check for 'Word'
if ( str.Find( "Microsoft Word", 0) >= 0)
{
// Get Pointer to caller class from the Second Parameter
CTestDlgDlg* pParentWnd =
( CTestDlgDlg* ) CWnd::FromHandle( ( HWND) lParam);
// Update Member m_bFound
pParentWnd->m_bFound = TRUE;
}
return TRUE;
}
return FALSE;
}