用VB如读取内存地址

来源:互联网 发布:网络交友新时空怎么画 编辑:程序博客网 时间:2024/05/16 13:39
导读:
  Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
  Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
  Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
  
  
  Private Const MAX_PATH As Long = 260
  
  Private Type PROCESSENTRY32
  dwSize As Long
  cntUsage As Long
  th32ProcessID As Long
  th32DefaultHeapID As Long
  th32ModuleID As Long
  cntThreads As Long
  th32ParentProcessID As Long
  pcPriClassBase As Long
  dwFlags As Long
  szExeFile As String * MAX_PATH
  End Type
  
  Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode 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 Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  Private Const TH32CS_SNAPPROCESS = &H2
  Private Const TH32CS_SNAPheaplist = &H1
  Private Const TH32CS_SNAPthread = &H4
  Private Const TH32CS_SNAPmodule = &H8
  Private Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
  Private Const PROCESS_TERMINATE As Long = (&H1)
  
  
  
  Private Sub Command1_Click()
  Dim proc As PROCESSENTRY32
  Dim snap As Long
  Dim exename As String
  Dim theloop As Long
  Dim ret As ListItem
  
  snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
  proc.dwSize = Len(proc)
  theloop = ProcessFirst(snap, proc) '获取第一个进程,并得到其返回值
  While theloop <>0 '当返回值非零时继续获取下一个进程
  exename = proc.szExeFile
  Debug.Print exename &" ID:" &proc.th32ParentProcessID
  theloop = ProcessNext(snap, proc)
  Wend
  CloseHandle snap '关闭进程“快照”句柄
  
  End Sub
  Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
  
  Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
  
  Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
  
  Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode 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 Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  
  Const MAX_PATH As Integer = 260
  
  Private Type PROCESSENTRY32
  
  dwSize As Long
  
  cntUsage As Long
  
  th32ProcessID As Long
  
  th32DefaultHeapID As Long
  
  th32ModuleID As Long
  
  cntThreads As Long
  
  th32ParentProcessID As Long
  
  pcPriClassBase As Long
  
  dwFlags As Long
  
  szExeFile As String * MAX_PATH
  
  End Type
  
  Const TH32CS_SNAPheaplist = &H1
  
  Const TH32CS_SNAPPROCESS = &H2
  
  Const TH32CS_SNAPthread = &H4
  
  Const TH32CS_SNAPmodule = &H8
  
  Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
  
  Private Sub Command1_Click()
  
  Dim i As Long, lPid As Long
  
  Dim Proc As PROCESSENTRY32
  
  Dim hSnapShot As Long
  
  ListView1.ListItems.Clear '清空ListView
  
  hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
  
  Proc.dwSize = Len(Proc)
  
  lPid = ProcessFirst(hSnapShot, Proc) '获取第一个进程的PROCESSENTRY32结构信息数据
  
  i = 0
  
  Do While lPid <>0 '当返回值非零时继续获取下一个进程
  
  ListView1.ListItems.Add , "a" &i, Hex(Proc.th32ProcessID) '将进程ID添加到ListView1第一列
  
  ListView1.ListItems("a" &i).SubItems(1) = Proc.szExeFile '将进程名添加到ListView1第二列
  
  i = i + 1
  
  lPid = ProcessNext(hSnapShot, Proc) '循环获取下一个进程的PROCESSENTRY32结构信息数据
  
  Loop
  
  CloseHandle hSnapShot '关闭进程“快照”句柄
  
  End Sub

本文转自
http://hi.baidu.com/freewolf/blog/item/c73a7d31466aff18eac4af5e.html
原创粉丝点击