简体   繁体   English

流程仍在应用退出

[英]Process remains on app exit

After I close the main window of my app, process remains listed in windows task manager's processes list. 关闭应用程序的主窗口后,进程仍列在Windows任务管理器的进程列表中。

Here's the code below, anyone has an idea what to modify to successfully exit process on app exit (or main window close). 这是下面的代码,任何人都知道要进行哪些修改才能在应用程序退出(或关闭主窗口)时成功退出进程。

int WINAPI WinMain(HINSTANCE inst,HINSTANCE prev,LPSTR cmd,int show)
{
    HRESULT hr = CoInitialize(0); MSG msg={0}; DWORD no;

    IGraphBuilder*  graph= 0;  hr = CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,IID_IGraphBuilder, (void **)&graph );
    IMediaControl*  ctrl = 0;  hr = graph->QueryInterface( IID_IMediaControl, (void **)&ctrl );

    ICreateDevEnum* devs = 0;  hr = CoCreateInstance (CLSID_SystemDeviceEnum, 0, CLSCTX_INPROC, IID_ICreateDevEnum, (void **) &devs);
    IEnumMoniker*   cams = 0;  hr = devs?devs->CreateClassEnumerator (CLSID_VideoInputDeviceCategory, &cams, 0):0;  
    IMoniker*       mon  = 0;  hr = cams->Next (1,&mon,0);  // get first found capture device (webcam?)    
    IBaseFilter*    cam  = 0;  hr = mon->BindToObject(0,0,IID_IBaseFilter, (void**)&cam);
                               hr = graph->AddFilter(cam, L"Capture Source"); // add web cam to graph as source
    IEnumPins*      pins = 0;  hr = cam?cam->EnumPins(&pins):0;   // we need output pin to autogenerate rest of the graph
    IPin*           pin  = 0;  hr = pins?pins->Next(1,&pin, 0):0; // via graph->Render
                               hr = graph->Render(pin); // graph builder now builds whole filter chain including MJPG decompression on some webcams
    IEnumFilters*   fil  = 0;  hr = graph->EnumFilters(&fil); // from all newly added filters
    IBaseFilter*    rnd  = 0;  hr = fil->Next(1,&rnd,0); // we find last one (renderer)
                               hr = rnd->EnumPins(&pins);  // because data we are intersted in are pumped to renderers input pin 
                               hr = pins->Next(1,&pin, 0); // via Receive member of IMemInputPin interface
    IMemInputPin*   mem  = 0;  hr = pin->QueryInterface(IID_IMemInputPin,(void**)&mem);

    DsHook(mem,6,Receive); // so we redirect it to our own proc to grab image data

    hr = ctrl->Run();   

    while ( GetMessage(   &msg, 0, 0, 0 ) ) {  
        TranslateMessage( &msg );   
        DispatchMessage(  &msg ); 
    }
};

Disclaimer: I made no attempt to make this look pretty or do error checking. 免责声明:我没有试图使其看起来很漂亮或进行错误检查。 It works as far as I can tell (when I close the window, the application ends), but it's not exemplary code at all. 据我所知,它可以工作(当我关闭窗口时,应用程序结束),但这根本不是示例代码。

The window does not post a WM_QUIT message on its own; 该窗口不会自行发布WM_QUIT消息; you have to do that yourself. 你必须自己做。 You can do it as follows: 您可以按照以下步骤进行操作:

Create a window for messages: 创建一个消息窗口:

WNDCLASSEX wx = {};
wx.cbSize = sizeof(WNDCLASSEX);
wx.lpfnWndProc = proc;        // function which will handle messages
wx.hInstance = GetModuleHandle(0);
wx.lpszClassName = "some class";
RegisterClassEx(&wx);
HWND hwnd = CreateWindowEx(0, "some class", "some name", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);

Make a IMediaEventEx * and use it to direct notifications to the window: 制作一个IMediaEventEx *并将其用于将通知定向到窗口:

I assume a global IMediaEventEx *event; 我假设有一个全局IMediaEventEx *event; . Please don't do it the quick dirty way. 请不要以这种快速肮脏的方式进行操作。

graph->QueryInterface(IID_IMediaEventEx, (void **) &event);
event->SetNotifyWindow((OAHWND) hwnd, WM_APP + 1, 0);

Make the window procedure handle the case of the user aborting: 使窗口过程处理用户中止的情况:

LRESULT CALLBACK proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    if (msg == WM_APP + 1) {
        long evt;
        LONG_PTR param1, param2;

        while (SUCCEEDED(event->GetEvent(&evt, &param1, &param2, 0))) {
            event->FreeEventParams(evt, param1, param2);

            if (evt == EC_USERABORT) {
                PostQuitMessage(0);
            }
        }
    }

    return DefWindowProc(hwnd, msg, wParam, lParam);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM