在寫入數據的時候,其中包括“lpStrings”這個參數,是在日誌裏面表現成文字“說明”的部分,但我給出一個字符串時。寫入成功後,發現我給的信息前面增加了一串文字,大意是:
你寫入的信息,未在遠程機器上註冊,或者本地DLL文件中不存在,之後再加上我給的“文字列”。請高手解決,多謝了。

解决方案 »

  1.   

    Private Declare Function RegisterEventSource Lib "advapi32.dll" Alias "RegisterEventSourceA" (ByVal lpUNCServerName As String, ByVal lpSourceName As String) As Long
    Private Declare Function DeregisterEventSource Lib "advapi32.dll" (ByVal hEventLog As Long) As Long
    Private Declare Function ReportEvent Lib "advapi32.dll" Alias "ReportEventA" (ByVal hEventLog As Long, ByVal wType As Long, ByVal wCategory As Long, ByVal dwEventID As Long, lpUserSid As Any, ByVal wNumStrings As Long, ByVal dwDataSize As Long, ByVal lpStrings As Long, lpRawData As Any) As Long
    Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As LongPrivate Const EVENTLOG_SUCCESS = &H0
    Private Const EVENTLOG_ERROR_TYPE = &H1
    Private Const EVENTLOG_WARNING_TYPE = &H2
    Private Const EVENTLOG_INFORMATION_TYPE = &H4
    Private Const EVENTLOG_AUDIT_SUCCESS = &H8
    Private Const EVENTLOG_AUDIT_FAILURE = &H10Private Sub Command1_Click()
    '    Dim hEvent As Long
    '    hEvent = RegisterEventSource("", "ddddd")
    '    Debug.Print hEvent
    '
    '    Dim strLogString As String
    '    strLogString = "abcdefghijklmnopqrs"
    '    Dim cbStringSize As Long
    '    cbStringSize = Len(strLogString) + 1
    '    Dim hMsgs As Long
    '    hMsgs = GlobalAlloc(&H40, cbStringSize)
    '    CopyMemory ByVal hMsgs, ByVal strLogString, cbStringSize
    '    Dim bReturnVal As Boolean
    '    bReturnVal = ReportEvent(hEvent, EVENTLOG_INFORMATION_TYPE, 0&, 1001&, 0&, 1&, cbStringSize, hMsgs, hMsgs)
    '    Debug.Print bReturnVal
    '    GlobalFree hMsgs
    '    DeregisterEventSource hEvent
        Debug.Print WriteToEventLog("Warning, file exceeded recommended limit.", _
        "Test App", _
        EVENTLOG_WARNING_TYPE, 1003)End Sub
    Public Function WriteToEventLog(sMessage As String, _
        sSource As String, _
        iLogType As Integer, _
        vEventID As Integer) As Boolean
        
        Dim bRC As Boolean
        Dim iNumStrings As Integer
        Dim hEventLog As Long
        Dim hMsgs As Long
        Dim cbStringSize As Long
        Dim iEventID As Integer
        
        hEventLog = RegisterEventSource("", sSource)
        cbStringSize = Len(sMessage) + 1
        hMsgs = GlobalAlloc(&H40, cbStringSize)
        CopyMemory ByVal hMsgs, ByVal sMessage, cbStringSize
        iNumStrings = 1
        
        '-- ReportEvent returns 0 if failed,
        '-- Any other number indicates success
        If ReportEvent(hEventLog, _
            iLogType, 0, _
            iEventID, 0&, _
            iNumStrings, cbStringSize, _
            hMsgs, hMsgs) = 0 Then
            '-- Failed
            WriteToEventLog = False
        Else
            '-- Sucessful
            WriteToEventLog = True
        End If
        
        Call GlobalFree(hMsgs)
        DeregisterEventSource (hEventLog)
    End Function