在vb.net中挂钩另一个程序对winapi函数的调用

时间:2010-10-22 09:30:11

标签: vb.net winapi hook

我一直在尝试在vb.net中构建游戏机器人。其中一个主要问题是访问屏幕上打印的文本,所以我一直试图将游戏调用挂钩到windows api drawtext和textout函数。我一直在寻找如何在vb.net中设置一个钩子很长时间没有运气的例子。我发现在旧学校vb,C ++和C#中翻译示例是不可能的。为方便起见,我想使用免费提供的deviare和/或easyhook库。有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

我发现工作的vb.net代码基于deviare挂在deviare论坛中的dio。

请记得在安装deviare之后添加在visual studio的add references页面的com选项卡下找到的所有6个deviare引用。

Public Class Form1

'To print to a textbox in the gui you will have to call textbox.invoke
Private _mgr As Deviare.SpyMgr
Private WithEvents _hook As Deviare.Hook
Private _proc As DeviareTools.IProcess

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    _mgr = New Deviare.SpyMgr()
    _hook = _mgr.CreateHook("user32.dll!ShowWindow")
    _hook.Attach(_mgr.Processes)
    _hook.AddProcessFilter(0, "notepad", 1)
    _hook.Hook()

Private Sub OnFunctionCalled(ByVal proc As DeviareTools.IProcess, ByVal callInfo As DeviareParams.ICallInfo, ByVal rCall As Deviare.IRemoteCall) Handles _hook.OnFunctionCalled

Debug.Print("Caught function call in " & proc.Name) 'view in imediate window

End Sub

end class

答案 1 :(得分:1)

我正在将Easyhook c#代码转换为vb.net。目前我收到错误代码5的消息,并且被关闭的应用程序被Windows关闭,例如,以帮助保护您的计算机等。如果有人可以帮助解决这个问题,那么我将不胜感激。到目前为止我所拥有的是......

Imports EasyHook
Imports System
Imports System.Diagnostics
Imports System.Runtime.Remoting
Imports System.Windows.Forms
Imports System.Security
Imports System.Security.Principal

Namespace FileMon
Friend Class Program
    ' Methods
    Public Shared Sub Main(ByVal args As String())

        Dim result As Integer = 0
        If ((args.Length <> 1) OrElse Not Integer.TryParse(args(0), result)) Then
            Console.WriteLine()
            Console.WriteLine("Usage: FileMon %PID%")
            Console.WriteLine()
        Else
            Try
                Try
                    Console.WriteLine("Registering Application")
                    Console.WriteLine()
                    Config.Register("A FileMon like demo application.", "FileMon.exe", "FileMonInject.dll")

                Catch exception1 As ApplicationException
                    Console.WriteLine("This is an administrative task! " & exception1.ToString)
                    Console.WriteLine()
                    Process.GetCurrentProcess.Kill()
                End Try

                Console.WriteLine("Creating IPC Server")
                Console.WriteLine()
                RemoteHooking.IpcCreateServer(Of FileMonInterface)(Program.ChannelName, WellKnownObjectMode.SingleCall)
                RemoteHooking.Inject(result, "FileMonInject.dll", "FileMonInject.dll", New Object() {Program.ChannelName})
                Console.WriteLine("Injected")
                Console.WriteLine()
                Console.ReadLine()

            Catch exception As Exception
                Console.WriteLine("There was an error while connecting to target:" & exception.ToString)
            End Try
        End If
    End Sub

    ' Fields
    Private Shared ChannelName As String
End Class
End Namespace

Imports System

Namespace FileMon
Public Class FileMonInterface
    Inherits MarshalByRefObject
    ' Methods
    Public Sub IsInstalled(ByVal InClientPID As Integer)
        Console.WriteLine("FileMon has been installed in target " & InClientPID)
    End Sub

    Public Sub OnCreateFile(ByVal InClientPID As Integer, ByVal InFileNames As String())
        Dim i As Integer
        For i = 0 To InFileNames.Length - 1
            Console.WriteLine(InFileNames(i))
        Next i
    End Sub

    Public Sub Ping()
    End Sub

    Public Sub ReportException(ByVal InInfo As Exception)
        Console.WriteLine(("The target process has reported an error:" & InInfo.ToString))
    End Sub

End Class
End Namespace

答案 2 :(得分:0)

  

easyhook库

尝试使用the EasyHook library