Click here to Skip to main content
6,632,253 members and growing! (21,473 online)
Email Password   helpLost your password?
General Reading » Hardware & System » Device Drivers     Intermediate

Communication between GUI Application and Device Driver

By lizhiwei

An article on Communication between GUI Application and Device Driver
VC6, VC7Win2K, WinXP, MFC, Dev
Posted:26 Mar 2002
Views:153,914
Bookmarked:86 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
26 votes for this article.
Popularity: 6.47 Rating: 4.57 out of 5
1 vote, 4.5%
1

2
2 votes, 9.1%
3
4 votes, 18.2%
4
15 votes, 68.2%
5

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


Member

Location: United States United States

Other popular Hardware & System articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 51 (Total in Forum: 51) (Refresh)FirstPrevNext
GeneralCommunication between Driver an Application Pinmemberdereck78_zg6:01 10 Dec '08  
Generalhow to install driver Pinmemberromon2:27 13 May '07  
AnswerRe: how to install driver PinmemberMaruf Maniruzzaman3:26 9 Jul '07  
Generalhow to build Pinmemberdhirendra_198019:35 3 Nov '05  
GeneralUNLOADED_WITHOUT_CANCELLING_PENDING_OPERATIONS Pinmemberekzot5:53 6 Sep '05  
GeneralUsing DeviceIoControl PinsussAnonymous14:26 13 Jul '05  
GeneralRe: Using DeviceIoControl Pinmembereuacela6:24 25 Aug '05  
GeneralDriver installation Pinmembercbt382:13 2 Mar '05  
GeneralHow about using WMI? Pinmembersohailkadiwala20:30 19 Oct '04  
Generaluser mode to control device driver PinmemberHorsy12:19 20 Sep '04  
GeneralIs there .inf for the driver? PinmemberGordon Smith1:43 27 Aug '04  
GeneralHelp Required PinsussKamran Shakir21:41 23 Aug '04  
GeneralIf u don't answers question don't start articles Chris must be aware to on this Pinmemberalexwinx1:51 14 Jun '04  
GeneralRe: If u don't answers question don't start articles Chris must be aware to on this Pinmembertomithecat23:00 13 Dec '04  
GeneralCan anybody help me Pinmemberfirstxx2:11 30 Apr '04  
General Comm. between usermode Application and Device Driver PinmemberSETI518:36 27 Mar '04  
Generaldynamic loading of dlls in kernel mode PinsussAnonymous23:19 1 Dec '03  
GeneralRe: dynamic loading of dlls in kernel mode PinsussKetil Jensen22:28 23 May '05  
GeneralI create Event in driver Pinmemberxenic21:44 9 Nov '03  
GeneralIsn't it really slow? Pinmembertomithecat4:30 19 Aug '03  
GeneralRe: Isn't it really slow? PinmemberDavidR_r10:04 7 Jul '04  
GeneralRe: Isn't it really slow? Pinmembertomithecat10:33 7 Jul '04  
GeneralRe: Isn't it really slow? PinsussAnonymous12:37 5 Nov '04  
Generalabout Miniport Driver! Pinmemberdewen11:13 18 Aug '03  
Generalhow to control the power of the display monitor Pinmembersteven cao0:15 18 Aug '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Mar 2002
Editor: Chris Maunder
Copyright 2002 by lizhiwei
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project