Advanced PowerBuilder

HomePrevious Lesson: Finding the Volume Information
Next Lesson: Displaying a URL in the Default Browser

Writing to the Windows NT Event Log

Declare a local external function as shown below.
FUNCTION long ufx_RegisterEventSource( string &
         pszServerName, string pszSourceName ) LIBRARY &
         "ADVAPI32.DLL" ALIAS FOR "RegisterEventSourceA"
FUNCTION boolean ufx_DeregisterEventSource( &
         long hEventLog ) LIBRARY "ADVAPI32.DLL" ALIAS &
         FOR "DeregisterEventSource"
FUNCTION boolean ufx_ReportEvent( long hEventLog, &
         integer wType, integer wCategory, long &
         dwEventID, long pUserSID, integer wNumStrings, &
         long dwDataSize, string pStringArray[], long &
         pRawData ) LIBRARY "ADVAPI32.DLL" &
         ALIAS FOR "ReportEventA"

Declare an instance string array variable as shown below.
String is_Msg[]

The following code adds message to the instance array variable declared above. The message you want to log need not be a single line. You can send an array as argument. However, NT concatenates all those subscripts into one string and logs it.
// Object: cb_addline 
// Event: Clicked
int li_MaxSubscript
li_MaxSubscript = Upperbound( is_msg )
li_MaxSubscript++
is_Msg[li_MaxSubscript] = sle_Message.Text

The following code first registers the source, logs the event, un-registers the source and sets the message array to nothing. You will see the event logged under 'Application' category and it has no effect on Windows 95 systems. To see the log, select Start > Programs > Administrator Tools > Event Viewer' and select Log > Applications menu option.
// Object: cb_write_to_nt_event_log
// Event: Clicked
CONSTANT integer EVENTLOG_SUCCESS = 0 // 0X0000
CONSTANT integer EVENTLOG_ERROR_TYPE = 1 // 0x0001
CONSTANT integer EVENTLOG_WARNING_TYPE = 2 // 0x0002
CONSTANT integer EVENTLOG_INFORMATION_TYPE = 4 // 0x0004
CONSTANT integer EVENTLOG_AUDIT_SUCCESS = 8 // 0x0008
CONSTANT integer EVENTLOG_AUDIT_FAILURE = 16 // 0x0010 
String ls_Server 
Long ll_EventLogHandle
String ls_NothingInIt[]
ll_EventLogHandle = ufx_RegisterEventSource( &
                           ls_Server, "PMS" )
if ll_EventLogHandle = 0 then
   MessageBox( "Error", "Unable to register the source!", &
                           StopSign!, OK!, 1 )
   return 0
end if
if ufx_ReportEvent( ll_EventLogHandle, &
            EVENTLOG_INFORMATION_TYPE, &
            123,            /* category (anything) */ &
            0,              /* msg file entry */ &
            0,              /* SID */ &
            UpperBound(is_Msg), /* qty strings */ &
            0,              /* qty raw data */ &
            is_Msg,         /* strings */ &
            0               /* raw data */ ) then
    MessageBox( "ReportEvent", &
                "Message logged successfully!" )
else
   MessageBox( "Error", &
      "Unable to write to event log!", StopSign!, OK!, 1 )
end if
ufx_DeregisterEventSource( ll_EventLogHandle )
// Reset the Message array to nothing.
is_Msg[] = ls_NothingInIt[]
HomePrevious Lesson: Finding the Volume Information
Next Lesson: Displaying a URL in the Default Browser