Using an event object in interprocess mode

I am trying to use an event object in a win32 environment to synchronize two processes. The following is a simplified code of two programs.

// process1
int main()
{
    HANDLE h = CreateEvent(NULL, FALSE, FALSE, TEXT("Hello"));
    WaitForSingleObject(h, INFINITE);
//  RunProcess(L"process2.exe", L"");
}

// process2
int main()
{
    HANDLE h = OpenEvent(EVENT_MODIFY_STATE, FALSE, TEXT("Hello"));
    SetEvent(h);    
}

This is pretty simple and works well when two processes start independently. However, this does not work when process 1 starts process 2 as a child process (which is commented out in the code above) - the SetEvent call fails. What is the reason and solution to this problem?

+3
source share
3 answers

. CreateEvent OpenEvent NULL, , , GetLastError.

WaitForSingleObject SetEvent MSDN.

, - :

  • CreateEvent
  • WaitForSingleObject.

, @Mark Tolonen.

- , , , .

, /, . , "" DoS- . . , bInheritHandle eventAttributes CreateEvent.

, , , . TRUE, .

+3

NULL, , , , :

If this parameter is NULL, the handle cannot be inherited by child processes

, ?

+2

Are you sure? As written, if process1 creates process2 at its current location, it will never create process2, because it will wait forever for the event to be fired. First create process2, then wait for the event to be set.

+1
source

Source: https://habr.com/ru/post/1773763/


All Articles