Вопрос

can someone give me a piece of code or API on how I can monitor any copy event on window base OS using vb classic (vb 6.0).

I want to trap the copy event let say a user is copying a file on a computer, how can I get the name of the file that is being copied and write a log that file was copied on this date. I want to create a free program about file tracking. I have googled and I can't find code to detect copy event on windows. Please help.

Это было полезно?

Решение

Create a Timer control (or have it in a loop with DoEvents()) and use GetClipboardData() like so:

Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Long, ByVal ByteLen As Long)
Private Sub Timer1_Timer()
    Dim ptr As Long
    OpenClipboard hwnd
    ptr = GetClipboardData(49158)
    If ptr Then
        Dim size As Long
        size = lstrlen(ptr)
        If size > 0 Then
            Dim data As String
            data = Space$(size)
            CopyMemory ByVal data, ByVal ptr, size
            MsgBox data
        End If
    End If
    CloseClipboard
End Sub

FYI: 49158 is a private clipboard format called "FileName" and unfortunately cannot be used with Clipboard.GetData() as the number is too high for an Integer.

When you copy something, the data variable will contain the file name copied.

Другие советы

You can monitor the clipboard: ClipBoard Monitor C# This will cover more than files and only tells you that a file name or file contents have been copied, not when it actually is. You'll need to do more to catch the actual write.

I believe the term you are missing is "hook", in which case this question/answer should get you what you need: Windows XP/7 copy FILE hook

1) You can hook the WinAPi functions like CopyFile and CopyFileEx creating a system wide hook, writing the hook yourself or using a API hook library like madCodeHook or Deviare API hook (I've used both libraries with great results.)

2) Writing a File System Filter Driver.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top