Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6972151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:55:52+00:00 2026-05-27T16:55:52+00:00

I found an example in C# how to add new Event to the Event

  • 0

I found an example in C# how to add new Event to the Event Viewer.
But, I need an example written in C++ (not .NET) that create new Event to the Event Viewer under the “Application” part.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:55:52+00:00Added an answer on May 27, 2026 at 4:55 pm

    You can use these three functions from the WINAPI:

    • RegisterEventSource
    • ReportEvent
    • DeregisterEventSource

    Here is a quick example of how to use these and to display messages correctly in the event log (error handling mostly ignored for brevity).

    Create a resource containg message information from the following Event_log.mc file:

    ;#ifndef _EXAMPLE_EVENT_LOG_MESSAGE_FILE_H_
    ;#define _EXAMPLE_EVENT_LOG_MESSAGE_FILE_H_
    
    MessageIdTypeDef=DWORD
    
    
    SeverityNames=(Success=0x0:STATUS_SEVERITY_SUCCESS
                   Informational=0x1:STATUS_SEVERITY_INFORMATIONAL
                   Warning=0x2:STATUS_SEVERITY_WARNING
                   Error=0x3:STATUS_SEVERITY_ERROR
                   )
    
    LanguageNames=(EnglishUS=0x401:MSG00401
                   Dutch=0x113:MSG00113
                   Neutral=0x0000:MSG00000
                   )
    
    MessageId=0x0   SymbolicName=MSG_INFO_1
    Severity=Informational
    Facility=Application
    Language=Neutral
    %1
    .
    
    MessageId=0x1   SymbolicName=MSG_WARNING_1
    Severity=Warning
    Facility=Application
    Language=Neutral
    %1
    .
    
    MessageId=0x2   SymbolicName=MSG_ERROR_1
    Severity=Error
    Facility=Application
    Language=Neutral
    %1
    .
    
    MessageId=0x3   SymbolicName=MSG_SUCCESS_1
    Severity=Success
    Facility=Application
    Language=Neutral
    %1
    .
    
    
    ;#endif
    

    To build the .mc file and .res resource file I executed the following:

    mc.exe -A -b -c -h . -r resources Event_log.mc
    rc.exe -foresources/Event_log.res resources/Event_log.rc
    

    This will create a header file called Event_log.h in the current directory and a resources directory containing a file named Event_log.res which you must link in to your application binary.

    Example main.cpp:

    #include <windows.h>
    #include "Event_log.h"
    
    void install_event_log_source(const std::string& a_name)
    {
        const std::string key_path("SYSTEM\\CurrentControlSet\\Services\\"
                                   "EventLog\\Application\\" + a_name);
    
        HKEY key;
    
        DWORD last_error = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
                                          key_path.c_str(),
                                          0,
                                          0,
                                          REG_OPTION_NON_VOLATILE,
                                          KEY_SET_VALUE,
                                          0,
                                          &key,
                                          0);
    
        if (ERROR_SUCCESS == last_error)
        {
            BYTE exe_path[] = "C:\\path\\to\\your\\application.exe";
            DWORD last_error;
            const DWORD types_supported = EVENTLOG_ERROR_TYPE   |
                                          EVENTLOG_WARNING_TYPE |
                                          EVENTLOG_INFORMATION_TYPE;
    
            last_error = RegSetValueEx(key,
                                       "EventMessageFile",
                                       0,
                                       REG_SZ,
                                       exe_path,
                                       sizeof(exe_path));
    
            if (ERROR_SUCCESS == last_error)
            {
                last_error = RegSetValueEx(key,
                                           "TypesSupported",
                                           0,
                                           REG_DWORD,
                                           (LPBYTE) &types_supported,
                                           sizeof(types_supported));
            }
    
            if (ERROR_SUCCESS != last_error)
            {
                std::cerr << "Failed to install source values: "
                    << last_error << "\n";
            }
    
            RegCloseKey(key);
        }
        else
        {
            std::cerr << "Failed to install source: " << last_error << "\n";
        }
    }
    
    void log_event_log_message(const std::string& a_msg,
                               const WORD         a_type,
                               const std::string& a_name)
    {
        DWORD event_id;
    
        switch (a_type)
        {
            case EVENTLOG_ERROR_TYPE:
                event_id = MSG_ERROR_1;
                break;
            case EVENTLOG_WARNING_TYPE:
                event_id = MSG_WARNING_1;
                break;
            case EVENTLOG_INFORMATION_TYPE:
                event_id = MSG_INFO_1;
                break;
            default:
                std::cerr << "Unrecognised type: " << a_type << "\n";
                event_id = MSG_INFO_1;
                break;
        }
    
        HANDLE h_event_log = RegisterEventSource(0, a_name.c_str());
    
        if (0 == h_event_log)
        {
            std::cerr << "Failed open source '" << a_name << "': " <<
                GetLastError() << "\n";
        }
        else
        {
            LPCTSTR message = a_msg.c_str();
    
            if (FALSE == ReportEvent(h_event_log,
                                     a_type,
                                     0,
                                     event_id,
                                     0,
                                     1,
                                     0,
                                     &message,
                                     0))
            {
                std::cerr << "Failed to write message: " <<
                    GetLastError() << "\n";
            }
    
            DeregisterEventSource(h_event_log);
        }
    }
    
    void uninstall_event_log_source(const std::string& a_name)
    {
        const std::string key_path("SYSTEM\\CurrentControlSet\\Services\\"
                                   "EventLog\\Application\\" + a_name);
    
        DWORD last_error = RegDeleteKey(HKEY_LOCAL_MACHINE,
                                        key_path.c_str());
    
        if (ERROR_SUCCESS != last_error)
        {
            std::cerr << "Failed to uninstall source: " << last_error << "\n";
        }
    }
    
    int main(int a_argc, char** a_argv)
    {
        const std::string event_log_source_name("my-test-event-log-source");
    
        install_event_log_source(event_log_source_name);
    
        log_event_log_message("hello, information",
                              EVENTLOG_INFORMATION_TYPE,
                              event_log_source_name);
    
        log_event_log_message("hello, error",
                              EVENTLOG_ERROR_TYPE,
                              event_log_source_name);
    
        log_event_log_message("hello, warning",
                              EVENTLOG_WARNING_TYPE,
                              event_log_source_name);
    
        // Uninstall when your application is being uninstalled.
        //uninstall_event_log_source(event_log_source_name);
    
        return 0;
    }
    

    Hope this helps but consider that this approach has been deprecated as stated by @Cody Gray.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to extend TextBox control to add watermarking functionality. The example I've found
I found an example in the VS2008 Examples for Dynamic LINQ that allows you
I found this example and used it How do you resize a Bitmap under
I'm playing with the incomplete example found at http://www.w3.org/TR/offline-webapps/ But I'm distressed to see
I've found out that when I use jQuery to add something to a page
I need to handle the onclick event over a Gauge. I don't have found
I need a way to add an application to the Login Items from a
I'm new to C# but not to OOP. I'd like to make a canvas
I found an example of implementing the repository pattern in NHibernate on the web,
I found an example on registering DLLs, Registering an Assembly for COM Interop in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.