Export (0) Print
Expand All

ISensorClassExtension::PostEvent method

The ISensorClassExtension::PostEvent method raises one or more driver events in the sensor class extension.

Syntax


HRESULT PostEvent(
  [in]  LPWSTR pwszSensorID,
  [in]  IPortableDeviceValuesCollection *pEventCollection
);

Parameters

pwszSensorID [in]

LPWSTR that contains the ID for the sensor for which the driver is raising the event.

pEventCollection [in]

Pointer to an IPortableDeviceValuesCollection interface that contains the list of events being posted and their associated data.

Return value

This method returns an HRESULT. Possible values include, but are not limited to, one of the following values.

Return codeDescription
S_OK

The method succeeded.

S_FALSE

The method succeeded, but no client programs are currently subscribed to events. Do not post events when no clients are subscribed.

E_INVALIDARG

The event collection was empty.

E_POINTER

A required pointer argument was NULL.

HRESULT_FROM_WIN32(ERROR_CAN_NOT_COMPLETE)

The class extension is not initialized.

HRESULT_FROM_WIN32(ERROR_INTERNAL_DB_CORRUPTION

A serialization error occurred.

HRESULT_FROM_WIN32(ERROR_OBJECT_NOT_FOUND)

No client programs are currently subscribed to events. Do not post events when no clients are subscribed.

 

Remarks

The sensor class extension forwards these events to the Sensor API and Location API, which in turn, raise events in client programs.

The collection passed through pEventCollection can contain one or more events. Represent each event and its associated data by using one IPortableDeviceValues object. Platform-defined PROPERTYKEYs for events and data types are defined in sensors.h.

Each IPortableDeviceValues object returned by this method must contain a time stamp, as described in SENSOR_CATEGORY_ALL.

The sensor class extension is responsibile for freeing any PROPVARIANT structures provided by this method.

To specify the event type, use the SENSOR_EVENT_PARAMETER_EVENT_ID PROPERTYKEY with the appropriate GUID value.

For an example of a class that creates an event thread, see Raising sensor events

IPortableDeviceValues and IPortableDeviceValuesCollection are documented in Windows Portable Devices.

Note  The sensor class extension will call PropVariantClear for PROPVARIANTs that you pass to the sensor class extension.

Raise state change events by calling ISensorClassExtension::PostStateChange.

Examples

In the following code snippet pEventParams is a pointer to IPortableDeviceValues and the call to SetGuidValue sets the event type by ID for the data-updated event. pClassExtension is a pointer to ISensorClassExtension.


 
HRESULT hr = pEventParams->SetGuidValue(SENSOR_EVENT_PARAMETER_EVENT_ID, SENSOR_EVENT_DATA_UPDATED);

Along with setting the event ID, you can specify any data values for the event. For example, the following code sample adds a value for X-axis acceleration from an accelerometer.


 
hr = pEventParams->SetFloatValue(SENSOR_DATA_TYPE_ACCELERATION_X_G, (FLOAT)accelerationX);

The following example code adds a time stamp value.


//Get the current time in SYSTEMTIME format
SYSTEMTIME st;
::GetSystemTime(&st);

// Convert the SYSTEMTIME into FILETIME
FILETIME ft;
if(FALSE == ::SystemTimeToFileTime(&st, &ft))
{
    hr = HRESULT_FROM_WIN32(GetLastError());
}

if(SUCCEEDED(hr))
{
    var.vt = VT_FILETIME;
    var.filetime.dwLowDateTime  = ft.dwLowDateTime;
    var.filetime.dwHighDateTime = ft.dwHighDateTime;

    hr = pValues->SetValue(SENSOR_DATA_TYPE_TIMESTAMP, &var);
}           

The following example code demonstrates a function that adds the IPortableDeviceValues object to a collection and posts the event.


 
HRESULT PostDataEvent(IPortableDeviceValues* pValues)
{
    // Validate that we have a pointer to the class extension.
    HRESULT hr = (NULL == m_spSensorCXT) ? E_UNEXPECTED : S_OK ;

    if (SUCCEEDED(hr))
    {
        CComPtr<IPortableDeviceValuesCollection> spValuesCollection;
        hr = spValuesCollection.CoCreateInstance(CLSID_PortableDeviceValuesCollection);

        if (SUCCEEDED(hr))
        {
            hr = spValuesCollection->Add(pValues);

            if (SUCCEEDED(hr))
            {
                hr = m_spSensorCXT->PostEvent(g_wszSensorID, spValuesCollection);
            }
        }
    }

    return hr;
}

Requirements

Minimum supported client

Windows 7

Minimum supported server

None supported

Version

Available in Windows 7.

Header

Sensorsclassextension.h

Library

SensorsClassExtension.lib

See also

Event Constants
ISensorDriver::OnClientSubscribeToEvents
ISensorDriver::OnClientUnsubscribeFromEvents

 

 

Send comments about this topic to Microsoft

Show:
© 2014 Microsoft