Measure the test taker's knowledge about event communication between applications and drivers.

Source: Internet
Author: User
1. iocreatenotificationevent and keclearevent in the driver
Openevent (synchronize, false, event_name) in Application)
In this way, you can only waitforsingleobject in the application, but not setevent or resetevent.
In the driver, you can kesetevent (and it must be followed by keclearevent, because the state of the object created in the core State cannot be modified in the application, and the state can only be cleared at this time ), that is, only the notification application can be driven. It is sufficient for some applications to only wait for the notification. For example, if the application waits for the notification that the data is ready, an event signal can be set in the interrupt processing function.
Note: The first parameter of openevent cannot be event_all_access, because the application does not have such a high permission to operate events created in the System context.

2. Create an event in the driver and pass the time handle to the application. The application does not need openevent. Applications can set and reset events at will, so that they can be synchronized with the system thread in the driver.
The precondition for this method is that the event is created in the process context of the application (it cannot be created in the System context) and passed to the application, for example, you can create an event in an IOCTL branch and send it to the application.
Explanation: The handle created in the process context using event is in the handle table of the process. It is verified that there is no permission restriction and can be directly used by the application; the handle created in system context is of course in the system process. If you only pass the handle value to the application, and there is no corresponding handle in the handle table, of course it will not succeed.

The Code is as follows:
Driving:
Void ppppp (pvoid event)
{
Kewaitforsingleobject (pkevent) event, executive, usermode, 0, 0 );
//... Verification
}
......
Wchar weventnamebuf [] = l "\ basenamedobjects \ mongodevent ";
Unicode_string ueventname;
Pkevent pevent;
Handle hevent, hthread;
......
Case IOCTL _******:
Rtlinitunicodestring (& ueventname, weventnamebuf );
Pevent = iocreatenotificationevent (& ueventname, & hevent );
Keresetevent (pevent );
Rtlcopymemory (IRP-> associatedirp. systembuffer, & hevent, 4 );
Pscreatesystemthread (& hthread, thread_all_access, 0, 0, ppppp, pevent );

Application:
If (! Deviceiocontrol (hdevice, IOCTL _ ******, 0, 0, & handle, 4, & bytes, 0 ))
MessageBox ("deviceio error! ");
Esle {
Wsprintf (STR, "% x, % x, % x", hdevice, bytes, handle );
MessageBox (STR );
If (! Setevent (handle ))
......
}
You can see that ppppp continues after you click MessageBox OK.

3. Create an event in the application and pass it to the driver through IOCTL. The obreferenceobjectbyhandle in the driver references the event object.
In this way, the application and driver can check and modify the event status.
Application:
Handle m_hcommevent = createevent (null,
False,
False,
Null );
// Download event object to device driver,
// M_hcommdevice is the device object

Deviceiocontrol (m_hcommdevice,
Io_reference_event,
(Lpvoid) m_hcommevent,
0,
Null,
0,
Dwreturn,
Null );
Wait where necessary
While (true)
{
Waitforsingleobject (m_hcommevent, infinite );
// After this function, the event is set
// Non signaled. Get information and deal with it.
}

Driver:
Case io_reference_event:
Hevent = (handle) irpstack->
Parameters. deviceiocontrol. type3inputbuffer;

Status = obreferenceobjectbyhandle (hevent,
Generic_all,
Null,
Kernelmode,
& Gpeventobject,
& Objhandleinfo );
The gpeventobject is a prkevent object, so we can use keeventxxx and kewaitforxxx to operate it.

Confidence number when an event occurs

Kesetevent (gpeventobject, 0, false );

When the event object is no longer needed:

Case io_dereference_event:
If (gpeventobject)
Obdereferenceobject (gpeventobject );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.