Kbase 21929: How to Send Messages to the Event Viewer Through Windows API
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  17/04/2002 |
|
SUMMARY:
This Knowledge Base Solution shows you how to send application messages to the Event Viewer (Application log file) through Windows APIs.
EXPLANATION:
Messages can be sent to the Application log (Event Viewer), not only to the local machine where the application is running, but also to any other machine within the network (assuming the necessary permissions). It is even possible to send messages on "behalf" of other applications.
SOLUTION:
The following example uses 3 APIs: RegisterEventSourceA, ReportEventA, and DeregisterEventSource. But there are others useful APIs, such as ReadEventLog, to read the Event log entries; the GetLastError API is useful for debugging purposes.
-- RegisterEventSourceA
You need to use RegisterEventSourceA to tell the Event Viewer that you
are an application and that you will send it some messages. Here you
have to put the name of the application and the name of the machine
on which you want to log the messages (by default localhost).
In the name of the application, you can put the application name you want, you can send messages on "behalf" of another application as well (even Progress).
-- ReportEventA
The ReportEventA sends the message to the Event Viewer.
-- DeregisterEventSource
The DeregisterEventSource just tells the Event Viewer that your
application is not going to send any further messages.
ReportEventA parameters:
lhEventLog->the Eventregistration number.
wType->Type of message (information=4, error,...)
wCategory -> Category.
dwEventID-> Event ID.
lpUserSid->User ID (0=no user)
wNumStrings->number of strings that you will send in the message.
dwDataSize->length of the raw data that you will send in the message.
lpStrings->String that you want to send.
lpRawData->Raw data that you want to send (normally a string)
/********************Event Logging Example***************************/
PROCEDURE RegisterEventSourceA EXTERNAL "advapi32.dll":
DEF INPUT PARAMETER lpUNCServerName AS CHARACTER.
DEF INPUT PARAMETER lpSourceName AS CHARACTER.
DEF RETURN PARAMETER RESA AS long.
END PROCEDURE.
PROCEDURE DeregisterEventSource EXTERNAL "advapi32.dll":
DEF INPUT PARAMETER lhEventLog AS long.
DEF RETURN PARAMETER DESA AS long.
END PROCEDURE.
PROCEDURE ReportEventA EXTERNAL "advapi32.dll":
DEF INPUT PARAMETER lhEventLog AS long.
DEF INPUT PARAMETER wType AS long.
DEF INPUT PARAMETER wCategory AS long.
DEF INPUT PARAMETER dwEventID AS long.
DEF INPUT PARAMETER lpUserSid AS long.
DEF INPUT PARAMETER wNumStrings AS long.
DEF INPUT PARAMETER dwDataSize AS long.
DEF INPUT PARAMETER lpStrings AS memptr .
DEF INPUT PARAMETER lpRawData AS LONG.
DEF RETURN PARAMETER REA AS long.
END PROCEDURE.
DEF VAR var1 AS INTEGER NO-UNDO.
DEF VAR var2 AS INTEGER NO-UNDO.
DEF VAR var3 AS INTEGER NO-UNDO.
DEF VAR sourcename AS MEMPTR NO-UNDO.
PROCEDURE GetLastError EXTERNAL "Kernel32":
DEF RETURN PARAMETER kk AS LONG.
END PROCEDURE.
DEFINE BUTTON BtnDone DEFAULT
LABEL "&Done"
SIZE 15 BY 1.14
BGCOLOR 8 .
DEFINE BUTTON BUTTON-2
LABEL "Event Viewer 1"
SIZE 25 BY 2.38.
DEFINE FRAME DEFAULT-FRAME
BUTTON-2 AT ROW 4.81 COL 25
BtnDone AT ROW 14.81 COL 62
" EVENT VIEWER" VIEW-AS TEXT
SIZE 19 BY 2.14 AT ROW 1.71 COL 28
WITH 1 DOWN NO-BOX KEEP-TAB-ORDER OVERLAY
SIDE-LABELS NO-UNDERLINE THREE-D
AT COL 1 ROW 1
SIZE 80 BY 16
DEFAULT-BUTTON BtnDone.
ON CHOOSE OF BtnDone IN FRAME DEFAULT-FRAME /* Done */
DO:
APPLY "CLOSE":U TO THIS-PROCEDURE.
END.
ON CHOOSE OF BUTTON-2 IN FRAME DEFAULT-FRAME /* Event Viewer 1 */
DO:
SET-SIZE(sourcename)= LENGTH("message") + 1.
var2=LENGTH("message") + 1.
PUT-STRING(sourcename,1)="message" .
MESSAGE GET-STRING(sourcename,1) VIEW-AS ALERT-BOX.
RUN RegisterEventSourceA (input "" ,INPUT "Application1" ,OUTPUT
var1).
RUN ReportEventA(INPUT integer(var1), INPUT 4, INPUT 12, INPUT 9336,
INPUT 0, INPUT 0 , INPUT 8, INPUT sourcename, INPUT
get-pointer-value(sourcename), OUTPUT var2).
IF var2 = 0 THEN
DO:
RUN GetLastError( OUTPUT var2).
MESSAGE STRING(var2) VIEW-AS ALERT-BOX.
END.
RUN DeRegisterEventSource (INPUT integer(var1), OUTPUT var3).
MESSAGE STRING(var3) VIEW-AS ALERT-BOX.
SET-SIZE(sourcename)=0.
END.
ENABLE ALL WITH FRAME DEFAULT-FRAME.
WAIT-FOR CLOSE OF THIS-PROCEDURE.
/********************************************************************/
References to Written Documentation:
Progress Knowledge Base Solution 21931, "How to Send Messages to the Event Viewer with Logevent.exe"