1

我希望能得到您的帮助,因为我在使用 VB.net 的项目中遇到错误。我在使用 ByVal 修饰符时出错。7个错误都说“预期表达”。

我在第 97、103、103、112、112、146 和 146 行得到错误。总共 7 个错误。

他们来了:

97:

ret = VirtualQueryEx(hProcess, ByVal lpMem, mbi, lLenMBI)

103:

ReadProcessMemory(hProcess, ByVal mbi.BaseAddress, ByVal sBuffer, mbi.RegionSize, lWritten)

112:

Call WriteProcessMemory(hProcess, ByVal CalcAddress - 1, ByVal sReplaceString, Len(sReplaceString), lWritten)

146:

test_hwnd = FindWindow(ByVal 0&, ByVal 0&)

这是完整的代码:

Option Explicit On
Imports System.Text
Imports System
Imports Microsoft.VisualBasic

Public Class Form1

Public Structure OSVERSIONINFO
    Dim dwOSVersionInfoSize As Long
    Dim dwMajorVersion As Long
    Dim dwMinorVersion As Long
    Dim dwBuildNumber As Long
    Dim dwPlatformId As Long
    Dim szCSDVersion As String ' 128
End Structure

Public Structure MEMORY_BASIC_INFORMATION ' 28 bytes
    Dim BaseAddress As Long
    Dim AllocationBase As Long
    Dim AllocationProtect As Long
    Dim RegionSize As Long
    Dim State As Long
    Dim Protect As Long
    Dim lType As Long
End Structure

Public Structure SYSTEM_INFO ' 36 Bytes
    Dim dwOemID As Long
    Dim dwPageSize As Long
    Dim lpMinimumApplicationAddress As Long
    Dim lpMaximumApplicationAddress As Long
    Dim dwActiveProcessorMask As Long
    Dim dwNumberOrfProcessors As Long
    Dim dwProcessorType As Long
    Dim dwAllocationGranularity As Long
    Dim wProcessorLevel As Integer
    Dim wProcessorRevision As Integer
End Structure

Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByVal LpVersionInformation As OSVERSIONINFO) As Long
Private Declare Function VirtualQueryEx& Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Short, <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.AsAny)> ByVal o As Object, ByVal lpBuffer As MEMORY_BASIC_INFORMATION, ByVal dwLength As Long)
Private Declare Sub GetSystemInfo Lib "kernel32" (ByVal lpSystemInfo As SYSTEM_INFO)
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Short, <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.AsAny)> ByVal o As Object, ByVal lpBuffer As Short, <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.AsAny)> ByVal o As Object, ByVal nSize As Long, ByVal lpNumberOfBytesWritten As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Short, <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.AsAny)> ByVal o As Object, ByVal lpBuffer As Short, <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.AsAny)> ByVal o As Object, ByVal nSize As Long, ByVal lpNumberOfBytesWritten As Long) As Long

Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, ByVal lpdwProcessId As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Const GW_HWNDNEXT = 2

Private Declare Function InvalidateRect Lib "user32" (ByVal hWnd As Long, ByVal lpRect As Long, ByVal bErase As Long) As Long
Const PROCESS_VM_READ = (&H10)
Const PROCESS_VM_WRITE = (&H20)
Const PROCESS_VM_OPERATION = (&H8)
Const PROCESS_QUERY_INFORMATION = (&H400)
Const PROCESS_READ_WRITE_QUERY = PROCESS_VM_READ + PROCESS_VM_WRITE + PROCESS_VM_OPERATION + PROCESS_QUERY_INFORMATION

Const MEM_PRIVATE& = &H20000
Const MEM_COMMIT& = &H1000

'================================================================================================================================================================='
'===============================================================Start of FUNCTION CODES==========================================================================='
'================================================================================================================================================================='

'Add 3 labels, 3 textboxes and 1 commandbutton on form
Private Sub Command1_Click()

    Dim srcEncoding As Encoding
    Dim dstEncoding As Encoding
    Dim bytes As Byte()
    Dim returnValue As Byte()

    Dim pid As Long, hProcess As Long, hWin As Long
    Dim lpMem As Long, ret As Long, lLenMBI As Long
    Dim lWritten As Long, CalcAddress As Long, lPos As Long
    Dim sBuffer As String
    Dim sSearchString As String, sReplaceString As String
    Dim si As SYSTEM_INFO
    Dim mbi As MEMORY_BASIC_INFORMATION

    sSearchString = Text2.Text
    sReplaceString = Text3.Text & Chr(0)
    pid = Shell(Text1.Text) 'launch application (calc.exe in this sample)
    hWin = InstanceToWnd(pid) 'get handle of launched window - only to repaint it after changes
    'Open process with required access
    hProcess = OpenProcess(PROCESS_READ_WRITE_QUERY, False, pid)
    lLenMBI = Len(mbi)
    'Determine applications memory addresses range
    Call GetSystemInfo(si)
    lpMem = si.lpMinimumApplicationAddress
    'Scan memory
    Do While lpMem < si.lpMaximumApplicationAddress
        mbi.RegionSize = 0
    ret = VirtualQueryEx(hProcess, ByVal lpMem, mbi, lLenMBI)
        If ret = lLenMBI Then
            If ((mbi.lType = MEM_PRIVATE) And (mbi.State = MEM_COMMIT)) Then ' this block is In use by this process
                If mbi.RegionSize > 0 Then
                    sBuffer = String.Equals(mbi.RegionSize, 0)
                    'Read region into string
               ReadProcessMemory(hProcess, ByVal mbi.BaseAddress, ByVal sBuffer, mbi.RegionSize, lWritten)
                    'Check if region contain search string
                    lPos = InStr(1, sBuffer, sSearchString, vbTextCompare)
                    If lPos Then
                        CalcAddress = mbi.BaseAddress + lPos
                        Me.Show()
                        ret = MsgBox("Search string was found at address " & CalcAddress & "." & vbCrLf & "Do you want to replace it?", vbInformation + vbYesNo, "VB-O-Matic")
                        If ret = vbYes Then
                            'Replace string in virtual memory
                     Call WriteProcessMemory(hProcess, ByVal CalcAddress - 1, ByVal sReplaceString, Len(sReplaceString), lWritten)
                            'Redraw window
                            InvalidateRect(hWin, 0, 1)
                        End If
                        Exit Do
                    End If
                End If
            End If
            'Increase base address for next searching cicle. Last address may overhead max Long value (Windows use 2GB memory, which is near max long value), so add Error checking
            On Error GoTo Finished
            lpMem = mbi.BaseAddress + mbi.RegionSize
            On Error GoTo 0
        Else
            Exit Do
        End If
    Loop
Finished:
    CloseHandle(hProcess)
End Sub
Private Sub Form_Load()
    'Caption = "VB-O-Matic"
    Label1.Text = "Start application:"
    Label2.Text = "String to find:"
    Label3.Text = "Replace with:"
    Text1.Text = "Calc.exe"
    Text2.Text = "Backspace"
    Text3.Text = "VB-O-Matic"
    Command1.Text = "&Launch It!"
End Sub

Private Function InstanceToWnd(ByVal target_pid As Long) As Long
    Dim test_hwnd As Long
    Dim test_pid As Long
    Dim test_thread_id As Long
    test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
    Do While test_hwnd <> 0
        If GetParent(test_hwnd) = 0 Then
            test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
            If test_pid = target_pid Then
                InstanceToWnd = test_hwnd
                Exit Do
            End If
        End If
        test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
    Loop
End Function
End Class
4

1 回答 1

2

删除ByVal函数调用中的 s。这些函数已经将它们的参数声明为ByVal- 您也不需要对ByVal这些函数进行调用。

于 2013-08-03T16:24:42.010 回答