Solved

VC++ write Event Log

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

863 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

25 Experts available now in Live!

Get 1:1 Help Now