KLOG.DLL access from Visual Basic
Previous  Top  Next

An alternative to the KLOGCTL ActiveX control in the next section, is to access syslog functionality directly from KLOG.DLL. The Visual Basic sample shows the function declarations. Code is provided to send a simple message, then check for an error.

'
' Kiwi Logger DLL declarations
'
Public Declare Function KLogInitialise Lib "klog" () As Boolean
Public Declare Function KLogSetPortByService Lib "klog" (ByVal UDPPortOut As String) As Boolean
Public Declare Function KLogSetPortByNum Lib "klog" (ByVal UDPPortOut As Long) As Boolean
Public Declare Function KLogSetHostAddress Lib "klog" (ByVal SysHostOut As String) As Boolean
Public Declare Function KLogSetMessagePriority Lib "klog" (ByVal PriorityOut As Long) As Boolean
Public Declare Function KLogSetMessageLevelNum Lib "klog" (ByVal LevNoOut As Long) As Boolean
Public Declare Function KLogSetMessageFacilityNum Lib "klog" (ByVal FacNoOut As Long) As Boolean
Public Declare Function KLogSetMessageLevelName Lib "klog" (ByVal LevNameOut As String) As Boolean
Public Declare Function KLogSetMessageFacilityName Lib "klog" (ByVal FacNameOut As String) As Boolean
Public Declare Function KLogSetProcessName Lib "klog" (ByVal UseRFCOut As Long, ByVal PIDNameOut As String) As Boolean
Public Declare Function KLogUseTCP Lib "klog" (ByVal UseTCPOut As Long) As Boolean
Public Declare Function KLogUseSilentErrors Lib "klog" (ByVal SilentErrOut As Long) As Boolean
Public Declare Function KLogSetMessage Lib "klog" (ByVal MessageOut As String) As Boolean
Public Declare Function KLogSendMessage Lib "klog" (Rcode As Long) As Boolean
Public Declare Function KLogReturnError Lib "klog" (ByVal ErrorIn As Long, ByVal ErrLen As Long) As Boolean
Public Declare Function KLogGetUsageHelp Lib "klog" (ByVal HelpIn As Long, ByVal HelpInLen As Long) As Boolean
Public Declare Function KLogGetUsageExample Lib "klog" (ByVal HelpExIn As Long, ByVal HelpExInLen As Long) As Boolean
Public Declare Function KLogGetSoftwareInfo Lib "klog" (ByVal SwInfoIn As Long, ByVal SWInfoInLen As Long) As Boolean
Public Declare Function KLogGetPriorityNames Lib "klog" (ByVal PriNameIn As Long, ByVal PriNameLen As Long) As Boolean

' ---------------------------------------------------------------------

' Uses the KLog DLL functions to set the syslog options, as per
' klog -h sysloghost -u 2110 -r myproc -p 122 –m "Local event occurred"
' The DLL handles all error checking, except for VB empty strings or values.

Private Sub SendSimpleMessage()

Dim Rcode As Long

KLogInitialise
KLogSetHostAddress "Sysloghost"
KLogSetPortByNum 2110
KLogSetMessagePriority 122
KLogSetProcessName 1, "myproc"
KLogUseSilentErrors 0
KLogSetMessage "Local event occurred"

KLogSendMessage Rcode

' Exit on success
If Rcode = 0 Then
  Exit Sub
End If

' Returns the last error from KLOG.DLL.
' Pass a pointer to a byte array.
' Convert the result into a string.

Dim ByteArray(201) As Byte
Dim BytePtr As Long
Dim ReturnString As String

BytePtr = VarPtr(ByteArray(0))
KLogReturnError BytePtr, CLng(UBound(ByteArray) - 1)

' If null, then no string returned, since silent errors is enabled
If Not ByteArray(0) = 0 Then
  ReturnString = StrConv(ByteArray, vbUnicode)
  MsgBox ReturnString, vbInformation, "KLOG.DLL error " & Rcode
End If

End Sub