关闭COM服务器时获取信号

时间:2018-09-16 10:32:08

标签: c++ windows com

假设,我们有Windows应用程序通过COM界面与Excel交互:

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

问题

当用户在关闭程序之前关闭Excel时,在关闭程序时出现错误,因为现在指针引用的是不存在的应用程序。是否有可能收到一条消息,指示该程序已关闭。在这种情况下,我应该如何释放指针?

0 个答案:

没有答案