Kernel Driver
- 實作一個 IOCTL 處理函式
- 從 input buffer 取得 user mode app 傳來的 event handle
PVOID ptr; HANDLE hEvent; WdfRequestRetrieveInputBuffer(req, sizeof(HANDLE), &ptr, NULL); hEvent = *(HANDLE *)ptr;
- 呼叫 ObReferenceObjectByHandle() 將 event handle 與 KEVENT 做關聯
KEVENT kEvent; ObReferenceObjectByHandle(hEvent, EVENT_MODIFY_STATE, *ExEventObjectType, UserMode, &kEvent, NULL);
- 呼叫 KeSetEvent() 通知 user mode app 有事件發生
- 在 driver/device cleanup 時呼叫 ObDereferenceObject() 切斷與 event handle 的關聯
User Mode App
- 在 user mode app 中呼叫 CreateEvent() 建立一個 event
- 將此 event handle 透過 DeviceIoControl 傳入 kernel driver
DeviceIoControl(hDevice, dwIoControlCode, &hEvent, sizeof(HANDLE), lpOutBuffer, nOutBufferSize, lpBytesReturned, lpOverlapped);
- 呼叫 WaitForSignalObject() 等待事件
- 程式離開前呼叫 CloseEvent() 將 event handle 關閉
更多資訊:
- Sharing Is Caring - Sharing Events Between Kernel-User Mode
- Communication between GUI Application and Device Driver
沒有留言:
張貼留言