Click here to Skip to main content
15,445,757 members
Articles / Programming Languages / C++
Posted 17 Sep 2006


76 bookmarked

Receiving Device Event Notification in Windows Service(Detecting Hardware Insertion and/or Removal in Service)

Rate me:
Please Sign up or sign in to vote.
3.69/5 (24 votes)
27 Nov 2006CPOL4 min read
Receiving Device Event Notification in Windows Service


Receiving Device Event Notification is one of the tricky and interesting things while programming in windows. We may have done this in our MFC or SDK applications.

This articles intention is to give a generic idea of receiving Device Event Notification in a windows service. This article is intended for the audience who has already got some knowledge of writing Windows Services.

Windows Services

As we already know, a Windows service is basically a win32 program that is loaded by the Service Control Manager. They are loaded before the login screen of windows appears. The windows service can be manually started, or at bootup time of windows.

Service Skeleton

The _tmain - General Entry Point

This Service is created by using Service option of ATL Application wizard with Visual C++ 6.0 IDE.The generic entrypoint of this service is _tWinMain which takes care of Registering the service,Unregistering and Installing the service in the system. Apart from this it is doing an important job by calling StartServiceCtrlDispatcher function and inturn makes the SCM to call our ServiceMain Function. I just filled up the SERVICE_TABLE_ENTRY structure with the Service Name and called StartServiceCtrlDispatcher passing the SERVICE_TABLE_ENTRY structure as a parameter.

The ServiceMain function – Entry point of Our Service

The ServiceMain is the entry point function for our service. When the SCM starts our service it creates a new thread for executing our ServiceMain function. The first thing a ServiceMain does is to call RegisterServiceCtrlHandler to register a handler function. The service uses this handler function as it's Service control handler which receives control codes including codes to start, stop, pause and continue the service.

m_hServiceStatus = RegisterServiceCtrlHandlerEx(m_szServiceName, 

Once we have registered our service control handler, we can update all our status with regard to our service in our _Handler routine. We can do this using the SetServiceStatus API call. We will need to do this several times during the course of our program and each time it involves filling up a SERVICE_STATUS structure.

Here one Important thing that needs to be noticed is the Second Parameter of the RegisterServiceCtrlHandlerEx which is Normally be typecasted with LPHANDLER_FUNCTION but here I did the same with LPHANDLER_FUNCTION_EX.

The main reason is to get the extended Information in our service control handler.The syntax of the HandlerEX Function looks like as follows,_Handler(DWORD dwOpcode,DWORD evtype, PVOID evdata, PVOID Context);

Here’s our key to receive the DEVICE_EVENT_NOTIFICATION comes into picture. The evtype and evdata or our keys concentration and will be discussed in the following session.

Apart from registering the Control Handler, we have to do another main thing in our Service Main function. This step will enable us to receive the DEVICE_EVENT_NOTIFICATION.

ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) ); 
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE); 
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; 

m_hDevNotify = RegisterDeviceNotification(m_hServiceStatus, 
               &NotificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE|

DEV_BROADCAST_DEVICEINTERFACE is a Windows defined structure in which we can specify the Type of interface for which we want to receive notification. DBT_DEVTYP_DEVICEINTERFACE specifies our device is of Interface type(Ex:USB/CDROM Interface).

RegisterDeviceNotification is the function to register the our service to receive the DeviceNotificationEvent.We are passing our service handle to this function to specify the SCM to send the DeviceNotificationEvent to our service.Remember that, this handle we have got with our RegisterServiceCtrlHandlerEx function.

One more thing here to note is the flags that are passed to RegisterDeviceNotification.

  1. DEVICE_NOTIFY_SERVICE_HANDLE: This flag is to specify that we are calling this function from a service.
  2. DEVICE_NOTIFY_ALL_INTERFACE_CLASSES:This is to tell the SCM that we want to receive the notification independent of interfaces.

This function will return m_hDevNotify to us to proceed further.

The ServiceCtrlHandler function

This is our service's control handler function. All service control requests like starting a service, stopping a service etc. are handled by the control handler.

_Handler(DWORD dwOpcode,DWORD evtype, PVOID evdata, PVOID Context);

As I already told, here’s our key to receive the DEVICE_EVENT_NOTIFICATION comes into picture. The evtype and evdata or our keys areas of our consideration.

Basically we put a switch statement on the dwOpcode variable and we have case blocks for each control code that we intend to handle.

In the DEVICE_EVENT_NOTIFICATION will be received in our switch case SERVICE_CONTROL_DEVICEEVENT.In that we calling a function DeviceEventNotify() where in we are passing the evtype and evdata as a parameter for our use.

switch (dwOpcode)
        PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
        DeviceEventNotify(evtype, evdata);
        LogEvent(_T("Bad service request"));

Our whole aim of this article will not be finished if I forgot to explain our main function which is,

DeviceEventNotify(evtype, evdata);

The body of this function looks like as follows:

unsigned long __stdcall DeviceEventNotify(DWORD evtype, PVOID evdata)
    switch (evtype)
            _Module.LogEvent(TEXT("Device Removal"));
            OutputDebugString("Device Removal");
            _Module.LogEvent(TEXT("Device Arrival"));
            OutputDebugString("Device Arrival");
    return 0;

evtype specifies the DEVICE_EVENT type weather it is DEVICE_ARRIVAL or REMOVAL.This may be Media Insertion/Removal in case of CD/DVD.

Using Evdata we can get details Like, for which Interface we are getting this Notification.

Sample output when Using DebugView is as follows,

Sample screenshot

Building the Sample Code

Building the sample code requires Visual C++ 6.0 or Visual Studio.NET 2003 compiler.If you build the code it will be automatically registered in the system.For using this application as service Goto command prompt and goto the executable location and type ,

Devservice -/Service

If you do this the application will be reconised as service and we can start the service in the usual manner

1.Either by typing net start DevService

2.Or goto Control Panel Services and Rightclick our service and then start.

Once we do this the DeviceEvent Notifications are received by the service.We can see the output using DbgView.exe.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionHow to get usb hid&pid Pin
Member 1197236529-Oct-15 0:03
MemberMember 1197236529-Oct-15 0:03 
QuestionCD/DVD detection in Windows7 Session0 Pin
Nidheesh V N12-May-14 3:56
MemberNidheesh V N12-May-14 3:56 
GeneralPurpose of code below while loop in winMain Pin
zCharles4-Feb-11 4:17
MemberzCharles4-Feb-11 4:17 
GeneralRe: Purpose of code below while loop in winMain Pin
tianguangjiangary28-Aug-12 21:07
Membertianguangjiangary28-Aug-12 21:07 
GeneralGetting an "System.BadImageFormatException" error upon installing Pin
Akash Nemani25-Apr-10 17:52
MemberAkash Nemani25-Apr-10 17:52 
GeneralRegisterDeviceNotification() failed, Help. Pin
jordy_Yun16-Aug-09 16:17
Memberjordy_Yun16-Aug-09 16:17 
GeneralSending WM_DEVICECHANGE from a service Pin
payman27-Jan-09 5:31
Memberpayman27-Jan-09 5:31 
GeneralRe: Sending WM_DEVICECHANGE from a service Pin
svsundar (Vairavan)28-Jan-09 3:20
Membersvsundar (Vairavan)28-Jan-09 3:20 
Generalwhere do i get the interface id Pin
madddi19-Nov-08 0:55
Membermadddi19-Nov-08 0:55 
GeneralRe: where do i get the interface id Pin
svsundar (Vairavan)19-Nov-08 6:37
Membersvsundar (Vairavan)19-Nov-08 6:37 
GeneralRe: where do i get the interface id Pin
madddi19-Nov-08 18:28
Membermadddi19-Nov-08 18:28 
GeneralRe: where do i get the interface id Pin
svsundar (Vairavan)20-Nov-08 2:20
Membersvsundar (Vairavan)20-Nov-08 2:20 
QuestionReceiving Device Event Notification in C# windoes service Pin
launchck20-Jul-08 21:50
Memberlaunchck20-Jul-08 21:50 
AnswerRe: Receiving Device Event Notification in C# windoes service Pin
svsundar (Vairavan)22-Jul-08 11:07
Membersvsundar (Vairavan)22-Jul-08 11:07 
GeneralReceive events in a DLL Pin
pfraca5-Dec-07 0:15
Memberpfraca5-Dec-07 0:15 
GeneralRe: Receive events in a DLL Pin
launchck20-Jul-08 21:52
Memberlaunchck20-Jul-08 21:52 
GeneralRe: Receive events in a DLL Pin
svsundar (Vairavan)22-Jul-08 11:06
Membersvsundar (Vairavan)22-Jul-08 11:06 
GeneralThe console version for the same is needed Pin
Aps VC Coder22-Aug-07 19:50
MemberAps VC Coder22-Aug-07 19:50 
QuestionGetting compile-time errors whicle executing in Vc 6.0 on Windows XP platform Pin
Aps VC Coder21-Aug-07 18:30
MemberAps VC Coder21-Aug-07 18:30 
AnswerRe: Getting compile-time errors whicle executing in Vc 6.0 on Windows XP platform Pin
svsundar (Vairavan)22-Aug-07 0:58
Membersvsundar (Vairavan)22-Aug-07 0:58 
GeneralRe: Getting compile-time errors whicle executing in Vc 6.0 on Windows XP platform Pin
Aps VC Coder22-Aug-07 19:46
MemberAps VC Coder22-Aug-07 19:46 
QuestionWant to detect when hardware gets attached/detached from USB/LPT port [modified] Pin
Aps VC Coder21-Aug-07 17:33
MemberAps VC Coder21-Aug-07 17:33 
QuestionRetrieving the Start Paramaters in a Windows service Pin
N Murali13-Apr-07 2:46
MemberN Murali13-Apr-07 2:46 
AnswerRe: Retrieving the Start Paramaters in a Windows service Pin
N Murali24-Apr-07 2:10
MemberN Murali24-Apr-07 2:10 
GeneralI want to use this as college project Pin
Banmidou24-Feb-07 22:29
MemberBanmidou24-Feb-07 22:29 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.