Suppose, we have Windows app that interacts with Excel through COM interface:
int APIENTRY wWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// Initialize COM for this thread...
CoInitialize(NULL);
<< Create Excel application >>
<< Create Workbooks collection >>
<< Create Workbook >>
<< Create worksheet >>
...
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
<< Release pointers >>
CoUninitialize();
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
...
switch (message)
{
<< Process message >>
}
}
The problem
When the user closes Excel before closing the program, I get an error when I close the program because now the pointers are referencing non-existing app. Is it possible to get a message, indicating that the program is closed. And how should I release the pointers in this case?