#include <Windows.h>
#include <stdio.h>

#define IOCTL_RECV_EVENT CTL_CODE(FILE_DEVICE_UNKNOWN,0x801,METHOD_BUFFERED,FILE_ALL_ACCESS)
#define IOCTL_START_MONITOR CTL_CODE(FILE_DEVICE_UNKNOWN,0x802,METHOD_BUFFERED,FILE_ALL_ACCESS)

int main(void)
{
	HANDLE hEvent = NULL;
	HANDLE hEvent2 = NULL;
	HANDLE hDriver = INVALID_HANDLE_VALUE;
	DWORD dwRet = 0;
	int i = 0;


	hDriver = CreateFileW(L"\\\\.\\XXEvent", GENERIC_READ |GENERIC_WRITE,
		FILE_SHARE_READ |FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hDriver == INVALID_HANDLE_VALUE)
	{
		printf("Error open driver\n");
		return -1;
	}


	hEvent = CreateEventW(NULL, TRUE, FALSE, L"XXTESTEVENT");
	if (hEvent == NULL)
	{
		printf("Error CreateEvent\n");
		return -1;
	}

	hEvent2 = OpenEventW(SYNCHRONIZE, FALSE, L"Global\\XXTESTEVENT2");
	if (hEvent2 == NULL)
	{
		printf("Error open event2");
		return -1;
	}


	DeviceIoControl(hDriver, IOCTL_RECV_EVENT, (PVOID)&hEvent, sizeof(HANDLE), NULL, 0, &dwRet, NULL);

	DeviceIoControl(hDriver, IOCTL_START_MONITOR, NULL, 0, NULL, 0, &dwRet, NULL);

	while (1)
	{
		WaitForSingleObject(hEvent2, INFINITE);
		i++;
		Sleep(5000);
		printf("Process Created:%d\n",i);
		SetEvent(hEvent);
		ResetEvent(hEvent);
	}


}