minifilter send message to r3

143 Views Asked by At

I'm writing a minifilter, which wants to notify the r3 application to popup a messagebox in some cases. I used fltsendmessage in minifilter and filtergetmessage in r3. In r3 application, I wrote like:

    while (INVALID_HANDLE_VALUE == s_portWorker.m_clientPort)
    {
        hResult = FilterConnectCommunicationPort(SERVER_PORTNAME_POPUP, 0, NULL, 0, NULL, &s_portWorker.m_clientPort);
        if (IS_ERROR(hResult)) {
            Sleep(1000);
        }
        while (true)
        {
            ZeroMemory(&getStruct, sizeof(GET_STRUCT));
            hResult = FilterGetMessage(s_portWorker.m_clientPort, (PFILTER_MESSAGE_HEADER)&getStruct, sizeof(GET_STRUCT), NULL);
        }
    }
    

It works fine. But when I stop my minifilter, calling FltCloseCommunicationPort() in driver unload. The port has been closed, but the connection is still in, my r3 process will blocks on FilterGetMessage and never return. I want to stop waiting the messagew when port close, and try to reconnect to my minifilter. What should I do? Since that FilterGetMessage() routine doesn't support a timeout mechanism, Do I have to create a event to notify the r3 when stop the filter?

1

There are 1 best solutions below

1
Michael Kim On BEST ANSWER

You can implement a timeout mechanism by using lpOverlapped parameter.

HANDLE hWait = CreateEvent(NULL, TRUE, FALSE, NULL);
OVERLAPPED op = {0};
op.hEvent = hWait;
HRESULT hResult = FilterGetMessage(s_portWorker.m_clientPort, (PFILTER_MESSAGE_HEADER)&getStruct, sizeof(GET_STRUCT), &op);
if (hResult == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
{
    HANDLE phHandles[2] = { hWait, g_hTerm };
    WaitForMultipleObjects(2, phHandles, TIME_OUT_VALUE);
}

And you can stop listenning by calling SetEvent(g_hTerm);