Click here to Skip to main content
Click here to Skip to main content

Communication between GUI Application and Device Driver

By , 26 Mar 2002
 
<!-- Download Links -->

Background

Recently I read a good article in which the author wrote a device driver and a GUI program, and he wanted to notify the GUI App when some event happened in the device driver. In his article, when a new process was created, a callback function in his driver would be called and it would notify the GUI App and display some information about the new process immediately. In the device driver, the author create a named event and opened it in GUI App. Then when an event happened, he handled it like this:

KeSetEvent(Event, 0, FALSE);
KeClearEvent(Event);

At this time, the GUI App was waiting for the event. Between the interval of the two functions, the App must got information of the new process. It works, but I don't think it's a good solution.

Why does the author code like this? Because, if we create an event in kernel mode, we can't modify its state in user mode, we only can check its state. But normally, we want the GUI App to modify the event to to non signaled, and it can wait the event again. I think this is the eligible IPC model to deal with this question.

About my solution

Kernel mode has higher priority than user mode. In kernel mode we can modify data in user mode conveniently. So I create an event object in user mode, and refer it in kernel mode. Now both device driver and GUI App can check and modify the state of the event object.

How to integrate it into your application

In your App, you must open the device object firstly, then code like this:

  1. Create an event object and down it into driver.
    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);
  2. Wait the event object to be signaled.
    while(true)
    {
    	WaitForSingleObject(m_hCommEvent, INFINITE);
    	//After this function, the event is set to non signaled. 
    	//Get information and deal with it.
    }     

In the device driver the code like this:

  1. In the IRP_MJ_DEVICE_CONTROL major routine:
    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.

  2. When the object event happened
    KeSetEvent(gpEventObject, 0, FALSE);
  3. When we don't need it, we should dereference it:
    case IO_DEREFERENCE_EVENT:
    	if(gpEventObject)
                  ObDereferenceObject(gpEventObject); 

I have tested my solution in a personal firewall product. It works well. When my IP Filter Driver got an IP packet, it checks the IP header as my security rules. If the packet will be intercepted, it reports to my GUI App.

The sample download include two projects: a GUI app and a device driver. The GUI App is a dialog based MFC application. It has a log window which can register the operations on event object. The device driver is only a very simple kernel mode device driver. It can operate the event object as the GUI App requries. I you want to run this demo, you must install and start the driver firstly.

Because there are so many tools can install and start device deriver, so I don't include the code to do this work.

If you have any problems and advices, Please notify me.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

lizhiwei

United States United States
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membergndnet22-Aug-12 1:52 
GeneralMy vote of 5memberCHRISTINE GREEN29-Mar-12 3:26 
QuestionPersonnal Firewallmembertigzy3-Jul-11 19:56 
GeneralCommunication between Driver an Applicationmemberdereck78_zg10-Dec-08 5:01 
Questionhow to install drivermemberromon13-May-07 1:27 
AnswerRe: how to install drivermemberMaruf Maniruzzaman9-Jul-07 2:26 
Questionhow to buildmemberdhirendra_19803-Nov-05 18:35 
GeneralUNLOADED_WITHOUT_CANCELLING_PENDING_OPERATIONSmemberekzot6-Sep-05 4:53 
GeneralUsing DeviceIoControlsussAnonymous13-Jul-05 13:26 
GeneralRe: Using DeviceIoControlmembereuacela25-Aug-05 5:24 
GeneralDriver installationmembercbt382-Mar-05 1:13 
GeneralRe: Driver installationmemberBoostedR36MK36-Sep-12 8:03 
QuestionHow about using WMI?membersohailkadiwala19-Oct-04 19:30 
Generaluser mode to control device drivermemberHorsy20-Sep-04 11:19 
I am trying to create a user mode application to talk to the devices from other vender.

What is the easiest way to find out the GUID interface for a PnP device? I tried the regedt32 but couldn't find it.

Also, I connect several of this outside vender's devices to several virtual COM port (COM3, COM5, COM7, etc). How do I talk to those devices separately (mainly stop the device so I can safely unplug it)?

Thank you for your advice.

QuestionIs there .inf for the driver?memberGordon Smith27-Aug-04 0:43 
GeneralHelp RequiredsussKamran Shakir23-Aug-04 20:41 
GeneralIf u don't answers question don't start articles Chris must be aware to on thismemberalexwinx14-Jun-04 0:51 
GeneralRe: If u don't answers question don't start articles Chris must be aware to on thismembertomithecat13-Dec-04 22:00 
QuestionCan anybody help mememberfirstxx30-Apr-04 1:11 
General Comm. between usermode Application and Device DrivermemberSETI5127-Mar-04 7:36 
Generaldynamic loading of dlls in kernel modesussAnonymous1-Dec-03 22:19 
GeneralRe: dynamic loading of dlls in kernel modesussKetil Jensen23-May-05 21:28 
GeneralI create Event in drivermemberxenic9-Nov-03 20:44 
QuestionIsn't it really slow?membertomithecat19-Aug-03 3:30 
AnswerRe: Isn't it really slow?memberDavidR_r7-Jul-04 9:04 
GeneralRe: Isn't it really slow?membertomithecat7-Jul-04 9:33 
GeneralRe: Isn't it really slow?sussAnonymous5-Nov-04 11:37 
Generalabout Miniport Driver!memberdewen18-Aug-03 10:13 
Questionhow to control the power of the display monitormembersteven cao17-Aug-03 23:15 
AnswerRe: how to control the power of the display monitorsussDeenaA22-Mar-04 18:09 
GeneralHelp required for Communication b/w an application and kernel mode drivermemberMuhammad Asif Khan25-Jun-03 22:47 
QuestionHow can I receive data from Gui ApplicationmemberBeyond11-Jun-03 17:59 
Questionhow to startsussrakeshniranjan15-May-03 22:10 
AnswerRe: how to startsussAnonymous12-Jun-03 4:08 
GeneralMore information on NDIS driversmembermachiav28-Apr-03 7:51 
QuestionHow to use acmFormatChoosememberBlkGoat1-Apr-03 1:03 
GeneralModems in VC++membervenkat_vs_12326-Feb-03 21:05 
GeneralDisplay Device Driversmembervikrams24-Feb-03 20:20 
GeneralRe: Display Device Driversmembermamata12-Mar-03 9:09 
Questionhow call dll function in device driver?memberhrp9-Feb-03 18:42 
AnswerRe: how call dll function in device driver?membervjairam27-Feb-03 16:20 
GeneralOther Security IssuessussAnonymous26-Aug-02 7:11 
Generalcompilation errorsusssuguna30-Jul-02 19:11 
GeneralRe: compilation errorsussLeeSangMo19-Aug-02 23:33 
Generalcompilation errorsussAnonymous30-Jul-02 19:03 
GeneralRe: compilation errormemberMsftone23-Sep-03 14:15 
QuestionHow can I read data from GUI in this driver?sussAnonymous22-Jul-02 2:56 
Generalcannot compile! please helpmemberJohn Wong2-Jul-02 4:57 
GeneralRe: cannot compile! please helpsussAnonymous22-Jul-02 5:19 
GeneralIt's just a thing of security...memberDaniel Lohmann27-Mar-02 22:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 27 Mar 2002
Article Copyright 2002 by lizhiwei
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid