Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read/Write to eventlog

Status
Not open for further replies.

Kongsjs

Programmer
Jul 20, 2007
17
NO
Hi

Anyone that know how you read from the Eventlog in PowerBuilder 7 and write to the eventlog.

Kongsjs
 
check out this link:

-------------------------------------
Accessing the NT Event Logging functions from PowerScript

Occasionally it is handy for a PowerBuilder application to store information in the Windows NT "event logging" database. These strings are then usable by the NT system "Event Viewer" (typically under the "Administrative Tools" folder). Share


Introduction Occasionally it is handy for a PowerBuilder application to store information in the Windows NT "event logging" database. These strings are then usable by the NT system "Event Viewer" (typically under the "Administrative Tools" folder). Using the event logging facility is not well documented, but is simple once you see an example.
This document provides example code to access the Win32 API's for these functions.
For details about the various parameters, refer to the Win32 API under "event logging".
A note for PowerBuilder 7 and later
If your component is using PowerBuilder 7 or later, then there is the "ErrorLogging" class which is much simpler to use.
This class provides easy access to the same Win32 event log with all data going into the "application" event log.
Additionally, if the component is running in Sybase "Enterprise Application Server" (aka Jaguar-CTS) then the log file is automatically redirected to the Jaguar log rather than the Win32 event log.
This class is not covered in this document, refer to your PowerBuilder online help.

Keywords
Event Logging, Event Viewer, RegisterEventSource, DeregisterEventSource, ReportEvent, ErrorLogging

Declare the global external functions
function long RegisterEventSource( string pszServerName, string pszSourceName ) &
LIBRARY "ADVAPI32.DLL" &
alias for "RegisterEventSourceA"
function boolean DeregisterEventSource( long hEventLog ) &
LIBRARY "ADVAPI32.DLL" &
alias for "DeregisterEventSource"
function boolean 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 the constants

// The types of events that can be logged.
// Copied out of WINNT.H
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

Sample Usage

string sTmp[3]
string sServer
long hEventSrc // handle to WIN32 event logging system
boolean bResult
// I'm using 3 strings only to show it need not be one string...
sTmp[1] = "1: Hello Sailor from PowerBuilder"
sTmp[2] = "2: meow meow"
sTmp[3] = "3: woof woof"
//-----------------------------------
// Notice: the "sServer" being un-initialized means this computer
// the "fubar.app" can be anything you want.
hEventSrc = RegisterEventSource( sServer, "fubar.app" )
IF hEventSrc = 0 THEN
// whoops - something failed
Return 0
END IF
MessageBox( "RegisterEventSource", "returns: " + string(hEventSrc) )
//-----------------------------------
bResult = ReportEvent( hEventSrc, &
EVENTLOG_INFORMATION_TYPE, &
123, /* category (anything) */ &
0, /* msg file entry */ &
0, /* SID */ &
3, /* qty strings */ &
0, /* qty raw data */ &
sTmp, /* strings */ &
0 /* raw data */ &
)
MessageBox( "ReportEvent", "returns: " + string(bResult) )
//-----------------------------------
DeregisterEventSource( hEventSrc )

Availability
Windows NT4.0 or later


regards,
Miguel L.
 
from version 7 and up you can use the errorlogging object (as stated in the previous answer):

if you check pb help file 'errorlogging object':
-------------------------
there's a function Log() that you can use to write to the
log file maintained by the object's container.


regards,
Miguel L.
 
Hi miguelleeuwe

Thanks a lot. This was great.
Do you know of a way to read the eventlog aswell ?

Regards
Kongsjs
 
Hello Kongsjs,

I suppose you'd have to use API functions 'OpenEventLog' and 'ReadEventLog', but I can't seem to find a powerbuilder example. If you look for "Visual Basic openeventlog readeventlog', you'll probably find some usefull information and can get more information. To declare the API's you'll have to use structure object to pass as variables.

Another thing I tried was to check from the registry where the files are located:

for example there's 'appenvent.evt', 'secevent.evt' and 'sysevent.evt', but they're not text files and might be blocked for reading. (though you can probably copy them first to other files and then read them ...)
The problem still, is the format.
They are located in C:\WINDOWS\system32\config
but in any way ... you can't suppose your user's windows will be installed in that directory: you'd have to read the values from the registry, using pb's RegistryGet and look for 'hkey_local_machine\system\controlset001\services\eventlog + security, application and the key 'file'.

so all together, I think you'll have to use the api's since I haven't seen anyway there's a function for powerbuilder's ErrorLog object to read from them ... (correct me anyone if I'm wrong, because it would solve the whole problem here).

this site is about how to use the api functions:
(no powerbuilder code though)..

this site has some code in C++:



regards,
Miguel L.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top