Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VC++ write Event Log

Posted on 2003-11-03
3
841 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

840 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