当前位置:编程学习 > C#/ASP.NET >>

求助 VB.net 写内存问题

这是我把VB6升级成Vb.net 2008的代码,窗体是搜索到了,可是写不入内存。本人初学。请问如何变换,望赐教!

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Integer, lpdwProcessId As Integer) As Integer
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As Integer
Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Object, ByVal lpBuffer As Object, ByVal nSize As Integer, lpNumberOfBytesWritten As Integer) As Integer
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer

Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Integer, lpBaseAddress As Object, lpBuffer As Object, ByVal nSize As Integer, lpNumberOfBytesWritten As Integer) As Integer



Dim hWnd As Integer

Const STANDARD_RIGHTS_REQUIRED As UInteger = &HF0000
Const SYNCHRONIZE As UInteger = &H100000
Const PROCESS_ALL_ACCESS As Boolean = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF)
hWnd = FindWindow(vbNullString, "圣魔传说")
If hWnd = 0 Then
Label10.Caption = "提示:游戏未运行。"
Check1.Enabled = False
Check2.Enabled = False
Check3.Enabled = False
Option1.Enabled = True
Option2.Enabled = True
Option4.Enabled = True
Option5.Enabled = True
Option6.Enabled = True
Option7.Enabled = True
Option8.Enabled = True
Option9.Enabled = True
Else
GetWindowThreadProcessId hWnd, pid
pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
Check1.Enabled = True
Check2.Enabled = True
Check3.Enabled = True
Label10.Caption = "提示:游戏正在运行。"
Option1.Enabled = False
Option2.Enabled = False
Option4.Enabled = False
Option5.Enabled = False
Option6.Enabled = False
Option7.Enabled = False
Option8.Enabled = False
Option9.Enabled = False
End If

If Check3.Value = 1 Then
WriteProcessMemory pHandle, ByVal &H6FE840, &H44, 1, 0&
WriteProcessMemory pHandle, ByVal &H6FE854, &H42, 1, 0&
Else
WriteProcessMemory pHandle, ByVal &H6FE840, &H42, 1, 0&
WriteProcessMemory pHandle, ByVal &H6FE854, &H44, 1, 0&
End If

If Check2.Value = 1 Then
WriteProcessMemory pHandle, ByVal &H59894B, &HEB, 1, 0&
WriteProcessMemory pHandle, ByVal &H59894C, &H18, 1, 0&
Else
WriteProcessMemory pHandle, ByVal &H59894B, &HEB, 1, 0&
WriteProcessMemory pHandle, ByVal &H59894C, &H18, 1, 0&
End If


CloseHandle pHandle --------------------编程问答-------------------- VB6的Long变量到了.net就是int32 --------------------编程问答-------------------- 哪里的 --------------------编程问答-------------------- 俺也在研究内存读写..

看你写的代码有点繁琐.给段这个给你.自己做一个模块.很方便的

把这段放置到模板里 声明也要放 记得要换成 公有的

然后以 

if FindGame then
'运行中...
else
'未运行
end if


来判断游戏是否运行即可


    '获取窗口函数
    Public Function FindGame() As Boolean
        Dim GameHwnd As Integer
        Dim PID As Integer
        hProcess = 0
        GameHwnd = FindWindow(vbNullString, "圣魔传说")
        If GameHwnd = 0 Then Exit Function
        GetWindowThreadProcessId(GameHwnd, PID)
        hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, PID)
        If hProcess = 0 Then Exit Function
        FindGame = True
    End Function
--------------------编程问答--------------------

Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
    Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Integer) As Integer
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
        Public Sub subIsRun()
        Dim hwnd As Long = FindWindow(vbNullString, "圣魔传说")
        '检查是否圣魔传说程序正在运行   
        If hwnd <> 0 Then
            ShowWindow(hwnd, 1)                                                    '显示已经运行的圣魔传说   
            SetForegroundWindow(hwnd)                                         '并激活为活动窗口 
        Else
            MsgBox("圣魔传说没有运行!")
        End If




--------------------编程问答-------------------- 我有用 Label10.text = "提示:游戏未运行。"
来检查是否搜素到窗体了。
就是内存写不进入 是哪里的问题? --------------------编程问答-------------------- 这个参数我在vb6可以运行的 --------------------编程问答-------------------- 我有用 Label10.text 来检查是否搜素到窗体 --------------------编程问答-------------------- 大概是 WriteProcessMemory 声明有问题吧 --------------------编程问答-------------------- http://msdn.microsoft.com/zh-cn/library/ms681674(v=VS.85).aspx

<DllImport("kernel32.dll", SetLastError:=True)> _
Public Shared Function WriteProcessMemory(ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer As Byte(), ByVal nSize As System.UInt32, <Out()> ByRef lpNumberOfBytesWritten As Int32) As Boolean
End Function


lpNumberOfBytesWritten [out] 
A pointer to a variable that receives the number of bytes transferred into the specified process. This parameter is optional. If lpNumberOfBytesWritten is NULL, the parameter is ignored.

最后一个参数要是byref传递的
--------------------编程问答-------------------- 你们肯写一份例子给我们 --------------------编程问答-------------------- 你们肯写一份例子给我吗 --------------------编程问答-------------------- 救命啊啊啊……………… 我不懂……………………… --------------------编程问答-------------------- http://hi.baidu.com/xdragon/blog/item/9917826eb66a94de80cb4a11.html

http://www.warhax.com/forum/showthread.php?t=611

http://goodkid126.freebsdhost.org/?p=333

http://forum.zomgstuff.net/showthread.php?t=1831 --------------------编程问答-------------------- 在VB.NET 2005 测试成功

'内存读取声明
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Public Function ReadProcessMemory( _
        ByVal hProcess As Integer, _
        ByVal lpBaseAddress As Integer, _
        ByRef lpBuffer As Integer, _
        ByVal nSize As Integer, _
        ByRef lpNumberOfBytesWritten As Integer) As Integer
    End Function
'内存写入声明
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Public Function WriteProcessMemory( _
        ByVal hProcess As Integer, _
        ByVal lpBaseAddress As Integer, _
        ByRef lpBuffer As Integer, _
        ByVal nSize As Integer, _
        ByRef lpNumberOfBytesWritten As Integer) As Integer
    End Function
--------------------编程问答-------------------- 果然是声明的问题..呵呵

--------------------编程问答-------------------- 你们有 像我这样修改游戏内存数据的 源码 让我参照下吗 --------------------编程问答-------------------- <DllImport("kernel32.dll", SetLastError:=True)> _

DllImport 未被定义 是什么意思 --------------------编程问答-------------------- 为什么我将code放进去的时候,导入了System.Runtime.InteropServices.DllImportAttribute
报错:
System.Runtime.InteropServices.DllImportAttribute”不能应用于实例方法。

需要将 <DllImport("kernel32.dll", SetLastError:=True)> _
删除? --------------------编程问答--------------------
Imports System.Runtime.InteropServices


这个.. --------------------编程问答-------------------- 仍然不行……………… --------------------编程问答-------------------- 能行的....

俺也是尝试游戏的内存修改.成功的..

--------------------编程问答-------------------- 你把你的源码发给我看看可以吗 --------------------编程问答--------------------
引用 14 楼 a445274427 的回复:
在VB.NET 2005 测试成功

VB.NET code

'内存读取声明
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Public Function ReadProcessMemory( _
        ByVal hProcess As Integer, _
        ByVal lpBase……



这个是放在Module里的,放在class里要像下面加Shared

'内存读取声明
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Public Shared Function ReadProcessMemory( _
        ByVal hProcess As Integer, _
        ByVal lpBaseAddress As Integer, _
        ByRef lpBuffer As Integer, _
        ByVal nSize As Integer, _
        ByRef lpNumberOfBytesWritten As Integer) As Integer
    End Function
'内存写入声明
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Public Shared Function WriteProcessMemory( _
        ByVal hProcess As Integer, _
        ByVal lpBaseAddress As Integer, _
        ByRef lpBuffer As Integer, _
        ByVal nSize As Integer, _
        ByRef lpNumberOfBytesWritten As Integer) As Integer
    End Function

--------------------编程问答-------------------- 为什么我像你们那样加了声明还是只搜素到窗体而写不进内存。
是不是我连至检查窗体的id也有问题, 正确的获取窗体函数至写入内存的源码该是怎么样的 --------------------编程问答-------------------- 无法在 DLL“kernel32”中找到名为“OPENProcess”的入口点。

这个该怎么做 --------------------编程问答-------------------- API声明的类型不对,VB中的INTEGER,LONG在VB.NET分别是SHORT,INTEGER --------------------编程问答-------------------- 不,并不是这个问题。
仍然写不进,求整个流程… 
补充:.NET技术 ,  VB.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,