Write Log in VB

Private Const GENERIC_WRITE = &H40000000
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const INVALID_HANDLE_VALUE As Long = -1
Private Const FILE_BEGIN = 0

Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, _
                    ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Long, _
                    ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, _
                    ByVal hTemplateFile As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, _
                    lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, _
                    ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, _
                    ByVal lpOverlapped As Any) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public Function WriteLog(ByVal strLogFile As String, _
                         ByVal strMsg As String, _
                         Optional blnForceCreate As Boolean = False) As Boolean

    Dim hFile As Long
    Dim lngSize As Long
    Dim lngRet As Long
    Dim bytBuffer() As Byte
    Dim lngCreateDis As Long
   
    lngCreateDis = 4
    hFile = CreateFile(strLogFile, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, lngCreateDis, 0, 0)
    If hFile <> INVALID_HANDLE_VALUE Then
        lngSize = GetFileSize(hFile, 0)
        If lngSize > 0 Then
            SetFilePointer hFile, lngSize, 0, FILE_BEGIN
        End If
        bytBuffer = StrConv(strMsg, vbFromUnicode)
        If UBound(bytBuffer) > 0 Then
            WriteFile hFile, bytBuffer(0), UBound(bytBuffer) + 1, lngRet, ByVal 0&
        End If
        WriteFile hFile, 13, 1, lngRet, ByVal 0&
        WriteFile hFile, 10, 1, lngRet, ByVal 0&
        CloseHandle hFile
        WriteLog = True
    End If
End Function

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值