Platform SDK: Debugging and Error Handling

Reporting an Event

After you have added a source name to the registry, use the RegisterEventSource function to get a handle to the Application event log. The following example obtains the handle and then adds an event to the log using the ReportEvent function.

void MyReportEvent(LPSTR szMsg)
{
    HANDLE h; 
 
    h = RegisterEventSource(NULL,  // uses local computer 
             "SamplApp");          // source name 
    if (h == NULL) 
        ErrorExit("Could not register the event source."); 
 
    if (!ReportEvent(h,           // event log handle 
            EVENTLOG_ERROR_TYPE,  // event type 
            0,                    // category zero 
            MSG_ERR_EXIST,        // event identifier 
            NULL,                 // no user security identifier 
            1,                    // one substitution string 
            0,                    // no data 
            (LPTSTR *) szMsg,     // pointer to string array 
            NULL))                // pointer to data 
        ErrorExit("Could not report the event."); 
 
    DeregisterEventSource(h); 
} 

Recall that your header file contains the event identifiers. For this example, the following event identifier was used:

//
// MessageId: MSG_ERR_EXIST
// MessageText:
//  File %1 does not exist.
//
#define MSG_ERR_EXIST                         ((DWORD)0xC0000004L)