Solved

VC++ write Event Log

Posted on 2003-11-03
3
840 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
ID: 9673076
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
ID: 9675147
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
ID: 9685624
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

770 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