ReadProcessMemory的问题 超级绿豆 其他人都近来看看啊

HtSoft 2004-06-24 12:21:35
看一下以下代码错在哪里 谢谢
代码目的是获取另一进程状态栏某1面板的文本
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 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 Declare Function VirtualAllocEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function VirtualFreeEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal dwFreeType As Long) As Long
Const PROCESS_ALL_ACCESS = &H1F0FFF
Const cBuf = 1024
Const MEM_COMMIT = &H1000
Const MEM_DECOMMIT = &H4000
Const PAGE_READWRITE = &H4&

Const WM_USER = &H400
Const SB_GETPARTS = (WM_USER + 6)
Const SB_GETTEXT = (WM_USER + 2)

Dim hMain As Long
Dim idProcess As Long
Dim hProcess As Long
Dim pBuf As Long

Dim hStatus As Long
Dim s As String * cBuf

Private Sub Command1_Click()
hMain = FindWindowEx(0, 0, "TMainForm", "窗口标题")
If hMain = 0 Then End
hStatus = FindWindowEx(hMain, 0, "TStatusBar", "")
If hStatus = 0 Then End

GetWindowThreadProcessId hMain, idProcess

hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, idProcess)
If hProcess = 0 Then End

pBuf = VirtualAllocEx(ByVal hProcess, ByVal 0&, Len(s), MEM_COMMIT, PAGE_READWRITE)
If pBuf = 0 Then End


n = SendMessage(hStatus, SB_GETTEXT, 2, ByVal pBuf)
Dim nn As Long


nn = ReadProcessMemory(ByVal hProcess, pBuf, s, Len(s), ByVal 0)

VirtualFreeEx hProcess, ByVal pBuf, 0, MEM_DECOMMIT
If nn <> 0 Then Me.Print s
End Sub
...全文
169 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
HtSoft 2004-06-24
  • 打赏
  • 举报
回复
to pigpag(噼里啪啦 - 跟我念:矤鼡秊彑夨冭)

直接发送SB_GETTEXT消息给statusbar获取消息 只能获取程序本身进程的

获取其他进程的时候 会出现内存不能为写的错误 所以只能用这种方法了

:)
pigpag 2004-06-24
  • 打赏
  • 举报
回复
另外说一下,你这样的目的大可不必用ReadProcessMemory吧,应该有直接的消息用来获取状态栏文本的。这样更安全,要求的权限也比较低。你这是大材小用阿
HtSoft 2004-06-24
  • 打赏
  • 举报
回复
哎 自己也意识到了 可能是参数传递时候的问题
呵呵

结帖了 呵呵
谢谢 supergreenbean(超级绿豆.监考ing=False)谢谢大家
pigpag 2004-06-24
  • 打赏
  • 举报
回复
Sorry Sorry白痴了一下,进程线程搞错了
刷牙ing

这句后面的看也没看,pardon
supergreenbean 2004-06-24
  • 打赏
  • 举报
回复
呵呵,第1个参数因为声明里已经加了byval,所以在后面的使用中不管加不加byval都是一样的
boywang 2004-06-24
  • 打赏
  • 举报
回复
楼上的不对吧。
ReadProcessMemory(ByVal hProcess, ByVal pBuf, ByVal s, Len(s), ByVal 0)

第一个参数不用byval的。
supergreenbean 2004-06-24
  • 打赏
  • 举报
回复
哈,pig说错了,可以出去买根胡萝卜刷牙了

nn = ReadProcessMemory(ByVal hProcess, pBuf, s, Len(s), ByVal 0)错了
应该是
nn = ReadProcessMemory(ByVal hProcess, ByVal pBuf, ByVal s, Len(s), ByVal 0)
HtSoft 2004-06-24
  • 打赏
  • 举报
回复
好象不是GetWindowThreadProcessId那句的问题

我感觉是
SendMessage
ReadProcessMemory
这2句的问题 但是找不到问题到底是什么
rainstormmaster 2004-06-24
  • 打赏
  • 举报
回复
GetWindowThreadProcessId:
VB声明
Private Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Long, lpdwProcessId As Long) As Long

【说明】
获取与指定窗口关联在一起的一个进程和线程标识符

【返回值】
Long,拥有窗口的线程的标识符

【参数表】
hwnd ----------- Long,指定窗口句柄

lpdwProcessId -- Long,指定一个变量,用于装载拥有那个窗口的一个进程的标识符

pigpag 2004-06-24
  • 打赏
  • 举报
回复
//idProcess

这个不一定正确

GetWindowThreadProcessId hMain, idProcess

改成

idProcess = GetWindowThreadProcessId(hMain, 0&)

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧