none
用VB调用API函数OpenProcess的返回值是不固定的吗? RRS feed

  • 问题

  • 我是学C#的,并且是自己学着玩不是很精通,在网上看了一个写外挂的文章,我自己试着写了一个 可就是读不出来数据?!!

      并且发现 OpenProcess的返回值总是在变化??!!OpenProcess 返回值不是一个进程的句柄吗?进程句柄对于同一个程序来说应该是固定的吧?我的这个这么会总变化呢? 并且读出来的数据总是0!
       
      (为了使问题简单一些,我自己写了个模拟游戏,用的是Visual Basic 6.0精简版)代码和现在地址如下:
    http://d.namipan.com/d/9cf212f12c8497fa18102a7dccf7a29a61a41c67a7120000 

     

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Const PROCESS_ALL_ACCESS = &H1F0FFF '全权打开进程
    Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByVal lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
    'Dim rea As Long
    Private Sub Timer1_Timer()
    'Dim fw, pid, openp, readPM As Long
    Dim fw As Long
    Dim pid As Long
    Dim ope As Long
    Dim rea As Long
    fw = FindWindow(vbNullString, "一个模拟游戏")
    If fw = 0 Then
    Label3.BackColor = RGB(255, 0, 0)
    Label3.Caption = "FindWindow失败"
    Else
    Label3.Caption = "FindWindow成功!" & fw
    End If
    GetWindowThreadProcessId fw, pid '返回程序进程ID
    If pid = 0 Then
    Label4.BackColor = RGB(255, 0, 0)
    Label4.Caption = "pid失败"
    Else
    Label4.Caption = "pid成功!" & pid
    End If
    ope = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
    If ope = 0 Then
    Label5.BackColor = RGB(255, 0, 0)
    Label5.Caption = "ope失败"
    Else
    Label5.Caption = "ope成功!" & ope
    End If
    'ReadProcessMemory 进程, ByVal 地址, 输出值, 字节, 0&
    ReadProcessMemory ope, ByVal &H14FD74, rea, 4, 0&       '&h表示是16进制的数
    '蓝  0014FD7C
    '红 0014FD74
    Label1.Caption = "w" & rea
    End Sub


    www.haodd123.com 我是半路出家,而且是自学 学的是C#语言,希望回帖的高手们留意一下,谢谢大家的帮助
    2010年9月30日 18:19

答案

  • 可以想象成,这个句柄是打开一个文件后得到的文件句柄.

    每次打开,都是临时分配,用后释放.

    • 已标记为答案 lkf18 2010年11月21日 13:49
    2010年10月11日 0:34