【发布时间】:2017-06-01 09:48:22
【问题描述】:
美好的一天!我一直在尝试列出所有当前正在运行的应用程序并使用 masm 将其写入文本文件。我是组装新手,但使用 MSDN 作为我的参考。到目前为止,我知道如何使用 CreateFile、WriteFile、ReadFile 等,但我不明白 Process32First 的工作原理。
我正在尝试将此链接中的代码转换为 MASM,(https://msdn.microsoft.com/en-us/library/windows/desktop/ms686701(v=vs.85).aspx) 但运气不好,我无法获得任何输出。
我将非常感谢任何帮助!谢谢!祝你有美好的一天。
include \masm32\include\masm32rt.inc
.data
pe32 PROCESSENTRY32 <>
errorCreateTool db "ERROR: CreateToolhelp32Snapshot", 0
errorPF db "ERROR: Process32First", 0
errorOP db "ERROR: OpenProcess", 0
yesMsg db "proceed", 0
.data?
dwPriorityClass dd ?
hProcessSnap HANDLE ?
hProcess HANDLE ?
.code
_start:
push 0
push TH32CS_SNAPPROCESS
call CreateToolhelp32Snapshot
mov hProcessSnap, eax
cmp hProcessSnap, INVALID_HANDLE_VALUE
je _errorCT
mov pe32.dwSize, sizeof PROCESSENTRY32
push offset pe32
push hProcessSnap
call Process32FirstW
cmp eax, ERROR_NO_MORE_FILES
je _errorPF
push offset pe32.szExeFile
call StdOut
mov dwPriorityClass, 0
push offset pe32.th32ProcessID
push FALSE
push PROCESS_ALL_ACCESS
call OpenProcess
cmp eax, 00H ;if I comment this out, the code will proceed
je _errorOpen
push offset pe32.th32ProcessID ;but this doesn't have any value and doesn't print out
call StdOut
push offset yesMsg ;while this prints out on the console
call StdOut
jmp _done
_errorOpen:
push offset errorOP
call StdOut
jmp _done
_errorPF:
push offset errorPF
call StdOut
jmp _done
_errorCT:
push offset errorCreateTool
call StdOut
_done:
push 0
call ExitProcess
end _start
【问题讨论】:
-
StdOut打印以零结尾的字符串。pe32.th32ProcessID不是字符串。 (顺便说一句,你确定你想要Process32First的unicode 版本吗?) -
嗨,迈克尔!谢谢你的意见。我刚刚意识到我不需要获取 th32ProcessID。我需要的只是 exe 文件名,它现在可以工作了。我所做的只是将 kernel32.inc 和 kernel32p.inc 中的 Process32FirstW 更改为 Process32First,将 Process32NextW 更改为 Process32Next。谢谢你的线索! :)
标签: c++ assembly masm msdn masm32