Menu

Eventlog example from MSDN malfunction

2004-12-08
2012-09-26
  • Nobody/Anonymous

    Hi all,

    I'm trying to compile an example from MSDN showing how to read from the eventlog.

    The compilation fails.

    Here's the source code:

    include <windows.h>

    include <stdio.h>

    define BUFFER_SIZE 1024*64

    void DisplayEntries( )
    {
    HANDLE h;
    EVENTLOGRECORD *pevlr;
    BYTE bBuffer[BUFFER_SIZE];
    DWORD dwRead, dwNeeded, dwThisRecord;

    // Open the Application event log.
    
    h = OpenEventLog( NULL,    // use local computer
             &quot;Application&quot;);   // source name
    if (h == NULL) 
    {
        printf(&quot;Could not open the Application event log.&quot;); 
        return;
    }
    
    pevlr = (EVENTLOGRECORD *) &amp;bBuffer;
    
    // Get the record number of the oldest event log record.
    
    GetOldestEventLogRecord(h, &amp;dwThisRecord);
    
    // Opening the event log positions the file pointer for this 
    // handle at the beginning of the log. Read the event log records 
    // sequentially until the last record has been read.
    
    while (ReadEventLog(h,                // event log handle 
                EVENTLOG_FORWARDS_READ |  // reads forward 
                EVENTLOG_SEQUENTIAL_READ, // sequential read 
                0,            // ignored for sequential reads 
                pevlr,        // pointer to buffer 
                BUFFER_SIZE,  // size of buffer 
                &amp;dwRead,      // number of bytes read 
                &amp;dwNeeded))   // bytes in next record 
    {
        while (dwRead &gt; 0) 
        { 
            // Print the record number, event identifier, type, 
            // and source name.
    
            printf(&quot;%03d  Event ID: 0x%08X  Event type: &quot;, 
                dwThisRecord++, pevlr-&gt;EventID);
    
            switch(pevlr-&gt;EventType)
            {
                case EVENTLOG_ERROR_TYPE:
                    printf(&quot;EVENTLOG_ERROR_TYPE\t  &quot;);
                    break;
                case EVENTLOG_WARNING_TYPE:
                    printf(&quot;EVENTLOG_WARNING_TYPE\t  &quot;);
                    break;
                case EVENTLOG_INFORMATION_TYPE:
                    printf(&quot;EVENTLOG_INFORMATION_TYPE  &quot;);
                    break;
                case EVENTLOG_AUDIT_SUCCESS:
                    printf(&quot;EVENTLOG_AUDIT_SUCCESS\t  &quot;);
                    break;
                case EVENTLOG_AUDIT_FAILURE:
                    printf(&quot;EVENTLOG_AUDIT_FAILURE\t  &quot;);
                    break;
                default:
                    printf(&quot;Unknown &quot;);
                    break;
            }
    
            printf(&quot;Event source: %s\n&quot;, 
                (LPSTR) ((LPBYTE) pevlr + sizeof(EVENTLOGRECORD)));
    
            dwRead -= pevlr-&gt;Length; 
            pevlr = (EVENTLOGRECORD *) 
                ((LPBYTE) pevlr + pevlr-&gt;Length); 
        }
    
        pevlr = (EVENTLOGRECORD *) &amp;bBuffer; 
    }
    
    CloseEventLog(h);
    

    }

    The compile log:

    Compiler: Default compiler
    Building Makefile: "H:\work\event\Makefile.win"
    Executing make clean
    rm -f main.o eventtest.exe

    gcc.exe -c main.c -o main.o -I"C:/Dev-Cpp/include"

    gcc.exe main.o -o "eventtest.exe" -L"C:/Dev-Cpp/lib" -mwindows -lndis -ladvapi32 -lapi32

    C:\Dev-Cpp\Bin..\lib\gcc-lib\mingw32\3.3.1........\mingw32\bin\ld.exe: cannot find -lapi32

    make.exe: *** [eventtest.exe] Error 1

    Execution terminated

    The inclusion of the ndis, advapi32 and api32 is a vain attempt on my part to negotiate the error.

    Help is welcomed.

    Best regards
    /Lars.

     
    • Lars Lyngby

      Lars Lyngby - 2004-12-08

      Compiler: Default compiler
      Building Makefile: "H:\work\event\Makefile.win"
      Executing make clean
      rm -f main.o eventtest.exe

      gcc.exe -c main.c -o main.o -I"C:/Dev-Cpp/include"

      gcc.exe main.o -o "eventtest.exe" -L"C:/Dev-Cpp/lib" -mwindows -ladvapi32

      C:/Dev-Cpp/lib/libmingw32.a(main.o)(.text+0x106):main.c: undefined reference to `WinMain@16'

      make.exe: *** [eventtest.exe] Error 1

      Execution terminated

      /Lars.

       
    • Lars Lyngby

      Lars Lyngby - 2004-12-08

      And in the FAQ ... sorry ...

      It is a console application and as such should have a main().

      Windows applications must have a WinMain().

      My cut&pasted app had neither.

      (With very red ears)
      /Lars.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.