ISystemDebugEventInstall Example (Visual C++)

   

The following code shows the use of all methods in ISystemDebugEventInstall. 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.

Use ISystemDebugEventInstall to register the component that will generate events and the events that component will generate. After you finish the registration process, you can generate events. ISystemDebugEventFire provides methods for generating events.

// Include required files.
#include "vaevt.h"         // ISystemDebugEventInstall #include "vaevt_i.c"      // IID_ISystemDebugEventInstall 
// 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
// 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 custom event category, a user-defined event, a dynamic
// component, and the CLSID of a COM component that implements the
// dynamic event interface. The CLSID is used when you want Visual Studio
// Analyzer to start the dynamic component automatically.
DEFINE_GUID(SAMPLE_SOURCE_GUID, 0x4ED47050, 0x64EF, 0x11D1, 0xA0, 0x6C, 0x00, 0xAA, 0x00, 0x6B, 0xBF, 0xAD);
DEFINE_GUID(SAMPLE_CATEGORY_GUID, 0x4ED47051, 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_DYNAMIC_SOURCE_GUID, 0x4ED47053, 0x64EF, 0x11D1, 0xA0, 0x6C, 0x00, 0xAA, 0x00, 0x6B, 0xBF, 0xAD);
DEFINE_GUID(CLSID_SAMPLE_DYNAMIC_SOURCE, 0x4ED47054, 0x64EF, 0x11D1, 0xA0, 0x6C, 0x00, 0xAA, 0x00, 0x6B, 0xBF, 0xAD);

// Declare a pointer to the event installer object.
ISystemDebugEventInstall   *pESI = NULL;

// Create the event installer object as an in-process server object. 
void SampleCreateESI()
{
   CoCreateInstance( CLSID_VSA_ESI, NULL, CLSCTX_INPROC_SERVER, IID_ISystemDebugEventInstall, (void **)&pESI );
}

// Check to see whether a component is already registered.
// SAMPLE_SOURCE_GUID is the GUID for the component (defined in 
// preceding code); substitute the GUID for your component.
boolean SampleIsSourceRegistered()
{
   HRESULT hr = pESI->IsSourceRegistered( SAMPLE_SOURCE_GUID );
   return( S_OK == hr );
}

// Check to see whether a dynamic component is already registered.
// SAMPLE_DYNAMIC_SOURCE_GUID is the GUID for the dynamic component
// (defined in preceding code); substitute the GUID for your dynamic 
// component.
boolean SampleIsDynamicSourceRegistered()
{
   HRESULT hr = pESI->IsDynamicSourceRegistered( SAMPLE_DYNAMIC_SOURCE_GUID );
   return( S_OK == hr );
}

// Register the component we checked for in preceding code.
// SAMPLE_SOURCE_GUID is defined in preceding code; substitute the 
// GUID for your component. Assign a name for the component 
// (Sample Event Source) using Unicode characters.
void SampleRegisterSource()
{   pESI->RegisterSource( L"Sample Event Source", SAMPLE_SOURCE_GUID );}
// Register the dynamic component we checked for in preceding code.
// SAMPLE_DYNAMIC_SOURCE_GUID is defined in preceding code; 
// substitute the GUID for your dynamic component. Assign a name 
// (Sample Dynamic Source) and a description (A sample dynamic 
// component) using Unicode characters.
// If the dynamic component is a COM object that you want VSA to start
// automatically, provide its CLSID (CLSID_SAMPLE_DYNAMIC_SOURCE).
// Also specify whether the COM object should be started in process (true
// or false). Note that dynamic components should usually be created
// out-of-process so that if they crash, Visual Studio Analyzer will not
// be affected.
void SampleRegisterDynamicSource()
{
   pESI->RegisterDynamicSource( L"Sample Dynamic Source", SAMPLE_DYNAMIC_SOURCE_GUID,

      L"A sample dynamic component", CLSID_SAMPLE_DYNAMIC_SOURCE, false );
}

// Register a system-defined event for the component to generate. 
// SAMPLE_SOURCE_GUID is the component that will generate the event
// being registered; DEBUG_EVENT_CALL is the event.
void SampleRegisterStockEvent()
{
   pESI->RegisterStockEvent( SAMPLE_SOURCE_GUID, DEBUG_EVENT_CALL );
}

// Register a custom event category for the component.
// SAMPLE_SOURCE_GUID is the component for the category being registered.
// SAMPLE_CATEGORY_GUID is a unique GUID for the category (defined
// in preceding code); substitute the GUID for your component.

// If you want the category to appear in a hierarchy, specify a parent
// (DEBUG_EVENT_CATEGORY_ALL will be the parent of this category).
// If you do not specify a parent, your category will appear at the top
// level in the Filter Editor.
// Assign a name (Sample Event Category) and description
// (Category for all sample events) using Unicode characters.
void SampleRegisterCategory()
{
   pESI->RegisterEventCategory( SAMPLE_SOURCE_GUID, SAMPLE_CATEGORY_GUID,
      DEBUG_EVENT_CATEGORY_ALL, L"Sample Event Category",
      L"Category for all sample events", NULL, 0 );
}

// Register a user-defined event for the component to generate. 
// SAMPLE_SOURCE_GUID is the component that will generate the event
// being registered.
// SAMPLE_EVENT_GUID is a unique GUID for the event (defined in 
// preceding code); substitute the GUID for your component.
// Assign a Name (Sample Event) and Description (Event generated by the
// component) using Unicode characters. 
void SampleRegisterCustomEvent()
{
   pESI->RegisterCustomEvent( SAMPLE_SOURCE_GUID, SAMPLE_EVENT_GUID,
      L"Sample Event", L"Event generated by the component ", 
      DEBUG_EVENT_TYPE_GENERIC, SAMPLE_CATEGORY_GUID, NULL, 0 );
}
// Release the event installer object.
void SampleDestroyESI()
{
   pESI->Release();
   pESI = NULL;
}

// Normally, you would unregister your components only during uninstall.
// The following code would be placed in your component's uninstall
// code.

// Unregister the dynamic component.
void SampleUnRegisterDynamicSource()

{
   pESI->UnRegisterDynamicSource( SAMPLE_DYNAMIC_SOURCE_GUID );
}

// Unregister the component.
void SampleUnRegisterSource()

{
   pESI->UnRegisterSource( SAMPLE_SOURCE_GUID );
}