Detect Excel Close Event

Hi.  I am trying to write an application that detects when an external Excel process closes, such as by using the SetWindowsHookEx() method to monitor WM_CLOSE events.  Could you please tell me how to do so?  (I will appreciate any sample code.)  Thank you.
deatcAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
If you have the HWND to the Excel process' window, it's quite straightfoward:
#include <windows.h>

HHOOK       g_hhk   =   NULL;
HWND        g_hwnd;

LRESULT CALLBACK ShellProc(
  int nCode,      // hook code
  WPARAM wParam,  // event-specific information
  LPARAM lParam   // event-specific information
)
{
    if  (   0   >   nCode)
        {
            return  (   CallNextHookEx  (   g_hhk,
                                            nCode,
                                            wParam,
                                            lParam
                                        )
                    );
        }

     if ( HSHELL_WINDOWDESTROYED == nCode)
        {
            if (g_hwnd == (HWND) wParam)
               {
                  // our Excel window is being closed
               }
        }
}

extern "C"
LONG __declspec(dllexport) ExcelMonitorInit ( HWND hwnd)
{

    if  (   g_hhk)  return  (   ERROR_ALREADY_EXISTS);

    g_hhk   =   SetWindowsHookEx    (   WH_SHELL,
                                        ( HOOKPROC) ShellProc,
                                        g_hThisDll,
                                        0
                                    );

    g_hwnd = hwnd; // store window handle

    return  (   0);
}

extern "C"
LONG __declspec(dllexport) ExcelMonitorTerm    ( void)
{

    UnhookWindowsHookEx (   g_hhk);

    g_hhk   =   NULL;

    return  (   0);
}

int APIENTRY    DllMain (   HINSTANCE   hInstance,
                            DWORD       dwReason,
                            LPVOID      lpReserved
                        )
{
    if  (   dwReason    ==  DLL_PROCESS_ATTACH)
        {

            g_hThisDll  =   hInstance;

            return  (   DisableThreadLibraryCalls   (   g_hThisDl
        }


    return( TRUE);
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deatcAuthor Commented:
Hi.  Thank you for your detailed and quick response.  I am, however, looking for a way to do this as part of a seperate application, not as a DLL.  Would it be easy to convert your DLL code into code for an EXE?  Thank you.
jkrCommented:
Well, if you want to use a Windows hook that hooks into an application other than your own, it has to reside in a DLL, see http://msdn.microsoft.com/en-us/library/ms644960%28v=VS.85%29.aspx ("Using Hooks"): "You must place a global hook procedure in a DLL separate from the application installing the hook procedure."
deatcAuthor Commented:
Outstanding, thorough solution!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.