The following code shows the use of all methods in ISystemDebugEventFire. In order to modify this code for your own use, you need to add error handling. Each method can raise errors, which you should make sure to trap. You can use the Windows® application Guidgen.exe to generate globally unique IDs (GUIDs) where required.
Before you can generate events from a component, you must register the component and the events you want it to generate with Visual Studio Analyzer. ISystemDebugEventInstall provides methods for accomplishing this registration.
// Include required files.
#include "vaevt.h" // ISystemDebugEventFire
#include "vaevt_i.c" // IID_ISystemDebugEventFire
// These files are required for generating valid GUIDs.
// Many of the VSA interface methods take a GUID as input.
#include <objbase.h> // Required by initguid.h
#include <initguid.h> // Ensure that GUIDs are generated by
// DEFINE_GUID
// This file contains constants for system-defined categories and events
#include "Vaids.h"
// Now generate GUIDs for the component that will generate VSA events, as
// well as a user-defined event,and a parameter that can hold a GUID if
// you wanted to pass a GUID as a parameter to FireEvent.
DEFINE_GUID(SAMPLE_SOURCE_GUID, 0x4ED47050, 0x64EF, 0x11D1, 0xA0, 0x6C, 0x00, 0xAA, 0x00, 0x6B, 0xBF, 0xAD);
DEFINE_GUID(SAMPLE_EVENT_GUID, 0x4ED47052, 0x64EF, 0x11D1, 0xA0, 0x6C, 0x00, 0xAA, 0x00, 0x6B, 0xBF, 0xAD);
DEFINE_GUID(SAMPLE_GUID_PARAMETER, 0x4ED47055, 0x64EF, 0x11D1, 0xA0, 0x6C, 0x00, 0xAA, 0x00, 0x6B, 0xBF, 0xAD);
// Declare a pointer to the event creator object.
ISystemDebugEventFire *pIEC = NULL;
// Create the event creator object as an in-process server object.
void SampleCreateIEC()
{
CoCreateInstance(CLSID_VSA_IEC, NULL, CLSCTX_INPROC_SERVER,
IID_ISystemDebugEventFire, (void**)&pIEC );
}
// Begin a session with the component.
// SAMPLE_SOURCE_GUID identifies the component.
// Specify a name (Sample Session Name) to uniquely identify the session
// to Visual Studio Analyzer. The session name must be unique for each
// time, each component, and each run of the component.
void SampleBeginSession()
{
pIEC->BeginSession( SAMPLE_SOURCE_GUID, L"Sample Session Name");
}
// Verify that the event creator object is ready to generate events
boolean SampleIsActive()
{
HRESULT hr = pIEC->IsActive();
return( S_OK == hr );
}
// Prepare to generate an event.
// Declare a byteArray to hold binary data; event data can be one of five
// different types, including binary data; all types are shown.
BYTE byteArray[] = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a";
// Event data is passed in arrays of parameter names (keys) and values.
// This array holds the parameter names. It is sized at 5 to show the
// five different types of event data you can pass.
// Standard event parameters (such as cVSAStandardParameterSourceHandle)
// are defined in vaevt.h.
#define MAXPARAMS 5
DWORD rgKeys[ MAXPARAMS ] = {
cVSAStandardParameterCorrelationID, // System-defined parameter
(DWORD) L"First custom parameter", // 3 custom parameters
(DWORD) L"Second custom parameter",
(DWORD) L"Third custom parameter",
cVSAStandardParameterSourceHandle }; // Another system-defined
// parameter
// This array holds the parameter values.
DWORD rgValues[ MAXPARAMS ] = {
(DWORD) L"This is a unicode string",
(DWORD) "This is an ANSI string",
(DWORD) &SAMPLE_GUID_PARAMETER,
0xFFFFFFFF,
(DWORD) byteArray };
// Remember to specify cVSAParameterKeyString for all custom parameters
// and to specify the length for all BYTE array data.
DWORD rgTypes[ MAXPARAMS ] = {
cVSAParameterValueUnicodeString,
cVSAParameterKeyString | cVSAParameterValueANSIString,
cVSAParameterKeyString | cVSAParameterValueGUID,
cVSAParameterKeyString | cVSAParameterValueDWORD,
cVSAParameterValueBYTEArray | ( cVSAParameterValueLengthMask &
sizeof(byteArray) ) };
// Generate the event.
// SAMPLE_EVENT_GUID is the GUID of the registered event to generate.
void SampleFireEvent()
{
pIEC->FireEvent( SAMPLE_EVENT_GUID, MAXPARAMS, rgKeys, rgValues,
rgTypes, 0, 0,
cVSAEventDefaultSource | cVSAEventDefaultTarget );
}
// End the session when you are finished generating events.
void SampleEndSession()
{
pIEC->EndSession();
}
// Release the event creator object.
void SampleDestroyIEC()
{
pIEC->Release();
pIEC = NULL;
}