熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

解決VB.net中ReadProcessMemory的問題

2022-06-13   來源: .NET編程 

  最近學校上VBNET的課VB以前學過一點點只會看不會寫不過沒有辦法學校開的所以幾個人就組了個小組寫一個下的殺毒軟件要用到ReadProcessMemory來讀取其它進程的內存可以怎麼調用也不行goole和baidu找了N個小時提這個問題的人倒是一大堆可以是沒有人解決過但是其中也給我一些其實後來在好友freecat的幫助下找到了問題的所在主要就是ReadProcessMemory聲明的問題我們應該這樣聲明
  
  Public Declare Function ReadProcessMemory Lib kernel (ByVal hProcess As Integer ByVal lpBaseAddress As Integer ByVal lpBuffer() As Byte ByVal nSize As Integer ByRef lpNumberOfBytesWritten As Integer) As Integer
  
  主要是其中的ByVal lpBaseAddress As Integer ByVal lpBuffer() As Byte
  
  要設為byval而不是byref
  
  這樣調用就可以得到返回值了
  
  [code]
  Private Declare Function OpenProcess Lib kerneldll _
  (ByVal dwDesiredAccess As Integer ByVal bInheritHandle As Integer ByVal dwProcessId As Integer) As Integer
  Public Declare Function ReadProcessMemory Lib kernel (ByVal hProcess As Integer ByVal lpBaseAddress As Integer ByVal lpBuffer() As Byte ByVal nSize As Integer ByRef lpNumberOfBytesWritten As Integer) As Integer
  Private Const PROCESS_VM_READ As Long = &H
  Dim ph As Integer
  Dim i As Integer
  Dim h As Integer
  Dim buffer() As Byte
  Dim bytesRead As Integer
  
  獲得進程
  
  ph = OpenProcess(PROCESS_ALL_ACCESS False )
  If ph Then
  MessageBoxShow(true)
  h = ReadProcessMemory(ph &H buffer bufferLength bytesRead)
  For i = To bufferLength
  TempMem += buffer(i)ToString
  Next
  Else
  MessageBoxShow(false)
  End If
  [/code]
From:http://tw.wingwit.com/Article/program/net/201311/12639.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.