Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <stdio.h>
- int main(int argc, char** argv)
- {
- if (argc < 2)
- {
- fprintf(stderr, "Incorrect process start\n");
- return 1;
- }
- HANDLE hEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, argv[1]);
- if (hEvent == NULL)
- {
- fprintf(stderr, "Can't open event, error: %d\n", GetLastError());
- system("pause");
- return 1;
- }
- HANDLE hSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, "Semaphore");
- if (!hSemaphore)
- {
- fprintf(stderr, "Can't open a semaphore, error: %d", GetLastError());
- CloseHandle(hEvent);
- return 1;
- }
- HANDLE hHandles[2] = { hEvent, hSemaphore };
- int running = 1;
- while (running)
- {
- DWORD dWaitResult = WaitForMultipleObjects(2, hHandles, FALSE, INFINITE);
- switch (dWaitResult)
- {
- case WAIT_OBJECT_0:
- ResetEvent(hEvent);
- running = 0;
- break;
- case WAIT_OBJECT_0 + 1:
- fprintf(stdout, "%s\n", argv[1]);
- ReleaseSemaphore(hSemaphore, 1, NULL);
- break;
- default:
- fprintf(stderr, "Waiting error: %d\n", GetLastError());
- running = 0;
- break;
- }
- }
- CloseHandle(hEvent);
- CloseHandle(hSemaphore);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement