Monitor Windows event logs with managed C ++

Source: Internet
Author: User


Hosting C ++Monitoring WindowsEvent Log
 
 
With the increasing number of viruses, rogue software, advertising software, and so on, many people are beginning to use registry monitoring programs, which are usually installed when the software tries to modify the registry, A warning window is displayed, prompting you. However, Windows event logs, especially security logs, are often ignored for security issues. Security logs usually record the operations of Windows operating systems and key system applications, such as illegal logon attempts, port scans, and other security-related events.
This article describes how to monitor Windows event logs in applications. Of course, you can also expand the program, such as sending an email to notify users when Event Logs are recorded to a specific event type.
 
 
Use. NET EventLogFor monitoring
The code in this article uses. NET 1.0/1.1 host C ++ syntax, if you are using a later version. NET, you need to set the/clr: oldSyntax compilation option in the Project Properties dialog box, or adjust the following code to conform to the new managed syntax.
The key. NET Type Used for Windows event logs is the Diagnostics: EventLog class.
 
 
1. Define a hosting class and implement the event log notification Handler
The handler (OnNewLogEntry) will be called when the "new event log item" event is triggered. Note the EntryWrittenEventHandler in this example. The following is the sample code:
 
// Sample code used to monitor log entries of new events
_ Gc class NewLogEntryEventHandler
{
Public:
NewLogEntryEventHandler (){}
 
Public:
Void OnNewLogEntry (Object * sender, EntryWrittenEventArgs * e)
{
// Obtain and process recently created items
EventLogEntry * entry = e-> Entry;
}
};
 
 
2. instantiate an EventLog object and set its EnableRaisingEvents attribute to true.
The EventLog: EnableRaisingEvents attribute is a boolean type. It controls whether to trigger an event when the project is added to the log specified by the EventLog object:
 
EventLog * log = new EventLog ("Application ");
Log-> EnableRaisingEvents = true;
 
 
3. Connect the event handler to the "new event log item" Event
First, instantiate the object that defines the event handler (NewLogEntryEventHandler in this example), and then add the event Method (OnNewLogEntry) to the EventLog: EntryWritten event handler list:
 
NewLogEntryEventHandler * handler = new NewLogEntryEventHandler ();
Log-> EntryWritten + =
New EntryWrittenEventHandler (handler, & NewLogEntryEventHandler: OnNewLogEntry );
 
 
4. write code for handling specific events
Let's look at an OnNewLogEntry method,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.