📄 eventshare.c
字号:
///////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright 1995 - 2002 OSR Open Systems Resources, Inc.
// All Rights Reserved
//
// This sofware is supplied for instructional purposes only.
//
// OSR Open Systems Resources, Inc. (OSR) expressly disclaims any warranty
// for this software. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
// THE IMPLIED WARRANTIES OF MECHANTABILITY OR FITNESS FOR A PARTICULAR
// PURPOSE. THE ENTIRE RISK ARISING FROM THE USE OF THIS SOFTWARE REMAINS
// WITH YOU. OSR's entire liability and your exclusive remedy shall not
// exceed the price paid for this material. In no event shall OSR or its
// suppliers be liable for any damages whatsoever (including, without
// limitation, damages for loss of business profit, business interruption,
// loss of business information, or any other pecuniary loss) arising out
// of the use or inability to use this software, even if OSR has been
// advised of the possibility of such damages. Because some states/
// jurisdictions do not allow the exclusion or limitation of liability for
// consequential or incidental damages, the above limitation may not apply
// to you.
//
// OSR Open Systems Resources, Inc.
// 105 Route 101A Suite 19
// Amherst, NH 03031 (603) 595-6500 FAX: (603) 595-6503
// email bugs to: bugs@osr.com
//
//
// MODULE:
//
// EventShare.C -- Event Share example user mode component
//
// ABSTRACT:
//
// This is a console application that simply creates a named event,
// sends an IOCTL to the event share driver to open a handle
// to the event, and waits for the driver to signal the event
//
// AUTHOR(S):
//
// OSR Open Systems Resources, Inc.
//
// REVISION:
//
//
///////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <winioctl.h>
#include "..\inc\eventioctl.h"
#include <stdio.h>
int main(void)
{
DWORD bytesReturned;
DWORD WaitStatus;
HANDLE DeviceDriver;
HANDLE SharedEvent;
//
// Open up a handle to the device driver
//
DeviceDriver = CreateFile(
"\\\\.\\EventShare", // lpFileName
GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode
NULL, // lpSecurityAttributes
OPEN_EXISTING, // dwCreationDistribution
0, // dwFlagsAndAttributes
NULL // hTemplateFile
);
if (DeviceDriver == INVALID_HANDLE_VALUE) {
printf("Unable to open handle to the device driver\n");
return 1;
}
//
// Create named event that will be shared between the driver and
// application. Note that this event is actually created under
// \BaseNamedObjects\ by the object manager so we will need to add
// that to the event name when we try and open this event from kernel mode
//
SharedEvent = CreateEvent(NULL, TRUE, FALSE, "SharedEvent");
if (SharedEvent == NULL) {
printf("Cannot create named event!\n");
return 1;
}
//
// Send an IOCTL to the driver to signal that the named event
// has been created and that it can now open a handle to it
//
if (!DeviceIoControl(DeviceDriver,
IOCTL_OPEN_EVENT,
NULL, 0,
NULL, 0,
&bytesReturned,
NULL)) {
printf("The driver failed to open the named event!\n");
return 1;
}
//
// Now we can just sit back and wait for the driver to signal our event.
// It should really happen well before 5 seconds...
//
WaitStatus = WaitForSingleObject(SharedEvent, 5000);
if (WaitStatus != WAIT_OBJECT_0) {
printf("Driver failed to signal event! WaitForSingleObject returned 0x%8.8x\n",
WaitStatus);
} else {
//
// Voila! We've successfully awoken a user mode application by signalling
// an event in kernel mode!
//
printf("The driver has successfully signaled our named event!\n");
}
//
// Clean up any and all resources allocated
//
CloseHandle(SharedEvent);
CloseHandle(DeviceDriver);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -