Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

determine: is application running

Status
Not open for further replies.

ZakiMaksyutov

Programmer
Feb 28, 2001
87
RU
I need to determine is MS Office applications is runnig (Word, PowerPoint, Outlook, Excel). How can I do it?

Thanks for any help
 

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;
}



 
Greate thanks TGM for your answer!

Your code looks good.
But there are some other APIs:
FindWindow
FindWindowEx

So, I solved my problem by following way:
-------------------------------------------
// Determine are Word, PowerPoint, Outlook, Excel running,
// and if yes, then display warning message.
BOOL bWord = ::FindWindow(_T("OpusApp"), NULL) == NULL ? FALSE : TRUE;
BOOL bPowerPoint = (:):FindWindow(_T("PP9FrameClass"), NULL) == NULL) &&
:):FindWindow(_T("PP97FrameClass"), NULL) == NULL)) ? FALSE : TRUE;
BOOL bOutlook = ::FindWindow(_T("rctrl_renwnd32"), NULL) == NULL ? FALSE : TRUE;
BOOL bExcel = ::FindWindow(_T("XLMAIN"), NULL) == NULL ? FALSE : TRUE;
-------------------------------------------
OpusApp
PP9FrameClass / PP97FrameClass
rctrl_renwnd32
XLMAIN
these are class names of Word(97/2000), PowerPoint2000, PowerPoint97, Outlook(97/2000), Excel(97/2000) respectively.
-------------------------------------------

Thanks for you help again!

With best regards,
Zaki Maksyutov
zaki-maksyutov@yandex.ru
Auktion24 Development Ltd.
wetellyou.com - Global experts portal!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top