1. Steen Jakobsen
  2. PowerBuilder
  3. Monday, 13 June 2022 10:13 AM UTC

Hi,

How can I add en entry to the windows application event-log from a 64 bit PB2019R3 application?

Any feedback is most appreciate :-)

 

// Steen

 

Roland Smith Accepted Answer Pending Moderation
  1. Monday, 13 June 2022 13:36 PM UTC
  2. PowerBuilder
  3. # 1

Here is the GetLastErrorMsg function from my answer:


Function ulong GetLastError( &
   ) Library "kernel32.dll"

Function ulong FormatMessage( &
   ulong dwFlags, &
   ulong lpSource, &
   ulong dwMessageId, &
   ulong dwLanguageId, &
   Ref string lpBuffer, &
   ulong nSize, &
   ulong Arguments &
) Library "kernel32.dll" Alias For "FormatMessageW"


public function string getlasterrormsg ();// -----------------------------------------------------------------------------
// SCRIPT: GetLastErrorMsg
//
// PURPOSE: This function gets the most recent system error.
//
// RETURN: The error message
//
// DATE       PROG/ID  DESCRIPTION OF CHANGE / REASON
// ---------- -------- -----------------------------------------------------
// 12/31/2021 RolandS  Initial coding
// -----------------------------------------------------------------------------

Constant ULong FORMAT_MESSAGE_FROM_SYSTEM = 4096
Constant ULong LANG_NEUTRAL = 0
String ls_buffer, ls_errmsg
ULong lul_error

lul_error = GetLastError()

ls_buffer = Space(MAX_PATH)

FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, &
   lul_error, LANG_NEUTRAL, ls_buffer, MAX_PATH, 0)

ls_errmsg = "Error# " + String(lul_error) + ": " + ls_buffer

Return ls_errmsg

end function

Comment
There are no comments made yet.
Roland Smith Accepted Answer Pending Moderation
  1. Monday, 13 June 2022 13:30 PM UTC
  2. PowerBuilder
  3. # 2

Register your app :

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application\MyAppName

EventMessageFile REG_EXPAND_SZ %SystemRoot%\System32\wshext.dll
TypesSupported   REG_DWORD     0x0000001f (31)

External Functions:

Function longptr RegisterEventSource ( &
   longptr lpUNCServerName, &
   string lpSourceName &
   ) Library "advapi32.dll" Alias For "RegisterEventSourceW"

Function boolean ReportEvent ( &
   longptr hEventLog, &
   uint wType, &
   uint wCategory, &
   ulong dwEventID, &
   longptr lpUserSid, &
   uint wNumStrings, &
   ulong dwDataSize, &
   string lpStrings[], &
   ulong lpRawData &
   ) Library "advapi32.dll" Alias For "ReportEventW"

Function boolean DeregisterEventSource ( &
   longptr hEventLog &
   ) Library "advapi32.dll"

Instance Variables:

Constant UInt EVENTLOG_SUCCESS = 0 // Success event
Constant UInt EVENTLOG_ERROR_TYPE = 1 // Error event
Constant UInt EVENTLOG_WARNING_TYPE = 2 // Warning event
Constant UInt EVENTLOG_INFORMATION_TYPE = 4 // Information event

Function:

public function boolean writetoeventlog (unsignedinteger aui_type, string as_message);// -----------------------------------------------------------------------------
// SCRIPT: WriteToEventLog
//
// PURPOSE: This function writes a message to the event log.
//
// ARGUMENTS: aui_type   - Use one of the instance constants:
//                         EVENTLOG_SUCCESS
//                         EVENTLOG_ERROR_TYPE
//                         EVENTLOG_WARNING_TYPE
//                         EVENTLOG_INFORMATION_TYPE
//            as_message - The message text
//
// DATE       PROG/ID  DESCRIPTION OF CHANGE / REASON
// ---------- -------- -----------------------------------------------------
// 12/31/2021 RolandS  Initial coding
// -----------------------------------------------------------------------------

Longptr ll_EventLog
String ls_msgtext[], ls_ErrorMsg

// log the message
ls_msgtext[1] = as_message

ll_EventLog = RegisterEventSource(NULL, "MyAppName")
If ll_EventLog > 0 Then
   ReportEvent(ll_EventLog, aui_type, &
      0, 0, 0, 1, 0, ls_msgtext, 0)
   DeregisterEventSource(ll_EventLog)
Else
   ls_ErrorMsg = GetLastErrorMsg()
   WriteToLogFile("RegisterEventSource Failed: " + ls_ErrorMsg)
   Return False
End If

Return True

end function

 

Comment
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Monday, 13 June 2022 12:34 PM UTC
  2. PowerBuilder
  3. # 3

Hi, 

I think this would be a good place to start: https://docs.microsoft.com/en-us/windows/win32/eventlog/event-logging-reference 

but ... with a little bit of luck, Chris has already build in similar functionality in his STD classes?

regards.

 

Comment
There are no comments made yet.
René Ullrich Accepted Answer Pending Moderation
  1. Monday, 13 June 2022 12:33 PM UTC
  2. PowerBuilder
  3. # 4
Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.