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:

Class Files Description

CSampleEvents

CSampleEvents.h

CSampleEvents.cpp

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:

  1. Copy all the project files to a new directory.

  2. Rename TimeSensor.rc, TimeSensor.ctl, TimeSensor idl, TimeSensor.def, and TimeSensor.inx to appropriate names for your driver.

  3. Update the sources file as follows:

    1. Change the TARGETNAME to the name of the new driver.
    2. In the SOURCES variable, change TimeSensor.rc to match the new RC file name and change TimeSensor.idl to match the new IDL file name.
    3. In the NTTARGETFILES variable, change the INF file name to match the new name.
    4. Change value for DLLDEF to match the new DEF file name.
  4. Update the strings in the RC file to match the new driver name.

  5. 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.

  6. In Internal.h:

    1. 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.
    2. Change the TimeSensorDriverTraceControl string to use the name of your driver.
    3. Change the driver tracing ID (choose a string that should be unique to your driver) at MYDRIVER_TRACING_ID.
  7. Update the IDL file as follows:

    1. Generate a new GUID and change the type library GUID.
    2. Generate a new GUID and change the driver class ID. You must use this class ID again (in registry format) in later steps.
    3. Change the name of the library.
    4. Change the name of the coclass.
    5. Change the help strings.
  8. In TimeSensor.ctl, change the WPP tracing control GUID and tracing ID to match the changes that you made in step 6.

  9. 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.

  10. Update the INX file, as follows:

    1. Change the hardware ID for the device in the [Microsoft.NT$ARCH$] section.
    2. Change the binary name from TimeSensor.dll to the name for your driver everywhere it appears in the file.
    3. Change the DriverCLSID value set in the UMDFTimeSensor_Install section to match the class ID for the new driver.
    4. Update the strings at the bottom of the file to contain descriptions of your new driver.
    5. 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.
    6. (Optional) Replace TimeSensor with the name of your driver in all of the INF file section names and strings names.
  11. Change the name of the driver module in DllSup.cpp.

  12. Update Driver.h as follows:

    1. Change 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.
    2. Change the first parameter to OBJECT_ENTRY_AUTO to contain the class name for the new driver.
  13. Update Driver.cpp to reference the new header name that is generated by the IDL instead of TimeSensor.h.

  14. 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

  1. At the command prompt, change to the directory that contains the device source code.

  2. 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).

Installation

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:

  1. Ensure that the driver builds without errors.

  2. Copy the DLL and INF files to a separate folder.

  3. 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.

  4. 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"

  5. Enable the driver in the Location and Other Sensors control panel.

Writing a Sensor Device Driver

 

 

Send comments about this topic to Microsoft

Build date: 9/7/2011