Solved

VC++ write Event Log

Posted on 2003-11-03
3
837 Views
Last Modified: 2010-08-05
Does anybody know the API, sample code to write msg to the 2000's event log since I am debugging an .EXE invoked from the ASP.

THanks
Pat
0
Comment
Question by:pwang1973
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
Comment Utility
Check out http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/event_logging_operations.asp ("Using Event Logging"). It boils down to

void AddEventSource()
{
    HKEY hk;
    DWORD dwData;
    UCHAR szBuf[80];
 
    // Add your source name as a subkey under the Application
    // key in the EventLog registry key.
 
    if (RegCreateKey(HKEY_LOCAL_MACHINE,
            "SYSTEM\\CurrentControlSet\\Services\
            \\EventLog\\Application\\SamplApp", &hk))
        ErrorExit("Could not create the registry key.");
 
    // Set the name of the message file.
 
    strcpy(szBuf, "%SystemRoot%\\System\\SamplApp.dll");
 
    // Add the name to the EventMessageFile subkey.
 
    if (RegSetValueEx(hk,             // subkey handle
            "EventMessageFile",       // value name
            0,                        // must be zero
            REG_EXPAND_SZ,            // value type
            (LPBYTE) szBuf,           // pointer to value data
            strlen(szBuf) + 1))       // length of value data
        ErrorExit("Could not set the event message file.");
 
    // Set the supported event types in the TypesSupported subkey.
 
    dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
        EVENTLOG_INFORMATION_TYPE;
 
    if (RegSetValueEx(hk,      // subkey handle
            "TypesSupported",  // value name
            0,                 // must be zero
            REG_DWORD,         // value type
            (LPBYTE) &dwData,  // pointer to value data
            sizeof(DWORD)))    // length of value data
        ErrorExit("Could not set the supported types.");
 
    RegCloseKey(hk);
}

void MyReportEvent(LPCTSTR *szMsg)
{
    HANDLE h;
 
    h = RegisterEventSource(NULL,  // uses local computer
             TEXT("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
            szMsg,                // pointer to string array
            NULL))                // pointer to data
        ErrorExit(TEXT("Could not report the event."));
 
    DeregisterEventSource(h);
}
0
 

Expert Comment

by:sunwell
Comment Utility
you can do in another way that I used to do.
you can download a debugview form www.sysinternals.com, it is a free ware.
then in your program:

TCHAR myBuffer[1024];
..........
_stprintf( myBuffer, _T(" if it is printed, something happened. %u"), uintValue);
::OutputDebugString( myBuffer);
( TRACE macro called this api OutputDebugString too)
then in your release / debug run, you can see the output message in the debugView.
it is a very good tool.


0
 
LVL 1

Expert Comment

by:jploeg
Comment Utility
Create a function to write info to the log:

void CTest1Dlg::LogEvent(const TCHAR* psz, bool bError)
{
   const TCHAR* rgsz[] = { psz };

   HANDLE hes = RegisterEventSource(0, "My event source");
   if(hes)
   {
      WORD dwEventType = bError ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE;
        DWORD dwEventID = bError ? (DWORD)0xE0000001L : (DWORD)0x20000001L;
      ReportEvent(hes, dwEventType, 0, dwEventID, 0, 1, 0, rgsz, 0);
      DeregisterEventSource(hes);
   }
}

and call that function:

      LogEvent("test", true);      
      LogEvent("test", false);      


HTH,

Jack
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now