Time Sensor Sample
The Time Sensor sample demonstrates how to write a sensor driver that raises events. You can explore this code to learn how to keep track of connected client applications, manage report intervals, and send event notifications through the sensor class extension. You can find the Time Sensor sample in the folder named SensorsAndLocation\TimeSensor.
Theory of Operation
The Time Sensor sample is based on the Sensor Driver Skeleton Sample. The sample uses Active Template Library (ATL) to provide COM functionality. In addition to the classes provided by the Skeleton sample, the Time Sensor sample uses the following classes:
Creates a worker thread that periodically raises events through the sensor class extension.
Customizing the Sample
To create a new driver that is based on the TimeSensor sample, complete the following steps:
Copy all the project files to a new directory.
Rename TimeSensor.rc, TimeSensor.ctl, TimeSensor idl, TimeSensor.def, and TimeSensor.inx to appropriate names for your driver.
Update the sources file as follows:
- Change the TARGETNAME to the name of the new driver.
- In the SOURCES variable, change
TimeSensor.rcto match the new RC file name and change
TimeSensor.idlto match the new IDL file name.
- In the NTTARGETFILES variable, change the INF file name to match the new name.
- Change value for DLLDEF to match the new DEF file name.
Update the strings in the RC file to match the new driver name.
In the DEF file, change the LIBRARY value to the new driver library name. This name must match the value that you provided for TARGETNAME.
- Update the WPP tracing control GUID at WPP_DEFINE_CONTROL_GUID. You can generate a new GUID by using the uuidgen or guidgen tools in the Microsoft Windows SDK. Be sure to match the format for the new GUID to the existing one.
- Change the TimeSensorDriverTraceControl string to use the name of your driver.
- Change the driver tracing ID (choose a string that should be unique to your driver) at MYDRIVER_TRACING_ID.
Update the IDL file as follows:
- Generate a new GUID and change the type library GUID.
- Generate a new GUID and change the driver class ID. You must use this class ID again (in registry format) in later steps.
- Change the name of the library.
- Change the name of the coclass.
- Change the help strings.
In TimeSensor.ctl, change the WPP tracing control GUID and tracing ID to match the changes that you made in step 6.
Update the constant definitions in DeviceConst.h to contain the correct values for your device. Update the code in SensorDdi.cpp to handle the correct values for your device.
Update the INX file, as follows:
- Change the hardware ID for the device in the
- Change the binary name from
TimeSensor.dllto the name for your driver everywhere it appears in the file.
- Change the DriverCLSID value set in the
UMDFTimeSensor_Installsection to match the class ID for the new driver.
- Update the strings at the bottom of the file to contain descriptions of your new driver.
- Change the provider name in the
[Version]section to match your company, and update the class ID for the device to the installation class of your device.
- (Optional) Replace TimeSensor with the name of your driver in all of the INF file section names and strings names.
- Change the hardware ID for the device in the
Change the name of the driver module in DllSup.cpp.
Update Driver.h as follows:
public CComCoClass<CMyDriver, &CLSID_TimeSensorSampleDriver>to contain the correct constant for your driver's class. The MIDL compiler generates this constant from the coclass name you provided in the IDL file.
- Change the first parameter to OBJECT_ENTRY_AUTO to contain the class name for the new driver.
Update Driver.cpp to reference the new header name that is generated by the IDL instead of TimeSensor.h.
The Time Sensor sample includes a custom icon. If you base your driver on this sample, be sure to replace the sample icon with your icon or a platform-defined icon. For more information about icons in sensor drivers, see Specifying an Icon.
Building the Sample
At the command prompt, change to the directory that contains the device source code.
Run build -ceZ, or use the BLD macro.
If the build succeeds, you will find the driver DLL and INF files in a subdirectory of your project directory. For example, if you use an x86fre build, the DLL and INF files will be placed in projectDirectory\.objfre_win7_x86\i386.
If the build fails, you can find errors and warnings in the log file (for example, buildfre_win7_x86.log).
You can install the Time Sensor sample for testing in the same way that you installed the sensor Skeleton sample driver.
To install the Time Sensor sample driver:
Ensure that the driver builds without errors.
Copy the DLL and INF files to a separate folder.
Locate the co-installer DLL file (either checked or free) from the redist/wdf/processor_type folder where you installed the WDK. Copy this file to the folder you created in step 2. For example, if you installed the WDK on your C: drive, you might copy WUDFUpdate_01009.dll from C:\WinDDK\build#\redist\wdf\x86.
Run Devcon.exe. You can find this program in the tools\devcon folder where you installed the WDK. Type the following command in the command window:
devcon.exe install TimeSensor.inf "Sensors\TimeSensor"
Enable the driver in the Location and Other Sensors control panel.
Build date: 9/7/2011