在golang中列出当前正在运行的进程列表,Windows版本

huangapple go评论90阅读模式
英文:

List of currently running process in golang, Windows version

问题

我如何在Windows下使用golang获取当前正在运行的进程列表?

我需要类似于以下链接的东西:

https://stackoverflow.com/questions/9030680/list-of-currently-running-process-in-golang

但也可在Windows下使用。

英文:

How can I get the list of currently running processes in golang under Windows?

I need something like:

https://stackoverflow.com/questions/9030680/list-of-currently-running-process-in-golang

but usable under Windows too.

答案1

得分: 8

我刚刚实现了你需要的函数(就像axw上面说的那样,EnumProcess)。
请查看https://github.com/AllenDang/w32。你可能想等到我的拉取请求完成之后再看:)
一个使用示例:https://gist.github.com/3083408

英文:

I just implemented the function you need (EnumProcess as axw stated above).
Check out https://github.com/AllenDang/w32. You might want to wait until my pull request is through 在golang中列出当前正在运行的进程列表,Windows版本
An example on how to use: https://gist.github.com/3083408

答案2

得分: 5

你需要使用Windows API函数EnumProcesses。在Windows上,syscall包可以加载任意的DLL和它们的函数(例如通过LoadLibrary/GetProcAddress)。所以你可以在psapi.dll中找到EnumProcesses。这会给你一个PID列表;然后你可以使用OpenProcess和EnumProcessModules来获取进程名。

有可能已经有人已经完成了实现这个功能的工作,但我不知道有什么。如果你找不到任何东西,可以查看syscall包的源代码(例如src/pkg/syscall/zsyscall_windows_386.go),并类似于其他Windows API函数的实现方式进行操作。

英文:

You need to use the Windows API function EnumProcesses. The syscall package on Windows enables you load arbitrary DLLs and their functions (i.e. via LoadLibrary/GetProcAddress). So you can get at EnumProcesses in psapi.dll. This gives you a list of PIDs; you can then use OpenProcess and EnumProcessModules to get the process name.

It's possible that someone has already done the work to implement this, but I don't know of anything. If you can't find anything, take a look at the syscall package's source (say, src/pkg/syscall/zsyscall_windows_386.go) and do something similar to what's done for the other Windows API functions.

答案3

得分: 3

根据syscall包的文档:该包已被锁定。在标准Go存储库之外的代码应迁移到golang.org/x/sys存储库中的相应包中。

您可以使用golang.org/x/sys/windows,它具有Process32First和Process32Next函数,用于枚举系统进程。

英文:

according to the syscall package docs: This package is locked down. Code outside the standard Go repository should be migrated to use the corresponding package in the golang.org/x/sys repository.

You can use golang.org/x/sys/windows, it has Process32First and Process32Next to let enumerate system processes.

答案4

得分: 0

package main
import "golang.org/x/sys/windows"

// unsafe.Sizeof(windows.ProcessEntry32{})
const processEntrySize = 568

func main() {
h, e := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
if e != nil {
panic(e)
}
p := windows.ProcessEntry32{Size: processEntrySize}
for {
e := windows.Process32Next(h, &p)
if e != nil { break }
s := windows.UTF16ToString(p.ExeFile[:])
println(s)
}
}

英文:

This seems to do it:

package main
import "golang.org/x/sys/windows"

// unsafe.Sizeof(windows.ProcessEntry32{})
const processEntrySize = 568

func main() {
   h, e := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
   if e != nil {
      panic(e)
   }
   p := windows.ProcessEntry32{Size: processEntrySize}
   for {
      e := windows.Process32Next(h, &p)
      if e != nil { break }
      s := windows.UTF16ToString(p.ExeFile[:])
      println(s)
   }
}

https://pkg.go.dev/golang.org/x/sys/windows#CreateToolhelp32Snapshot

答案5

得分: 0

代码更清晰,如果你使用Windigo(为了简洁起见,省略了错误检查):

package main

import (
    "fmt"

    "github.com/rodrigocfd/windigo/win"
    "github.com/rodrigocfd/windigo/win/co"
)

func main() {
    pids, _ := win.EnumProcesses()

    for _, pid := range pids {
        hSnap, _ := win.CreateToolhelp32Snapshot(co.TH32CS_SNAPMODULE, pid)
        defer hSnap.CloseHandle()

        hSnap.EnumModules(func(me32 *win.MODULEENTRY32) {
            fmt.Printf("PID: %d, %s @ %s\n",
                me32.Th32ProcessID, me32.SzModule(), me32.SzExePath())
        })
    }
}

或者,如果你只想获取进程而不包括模块:

package main

import (
    "fmt"

    "github.com/rodrigocfd/windigo/win"
    "github.com/rodrigocfd/windigo/win/co"
)

func main() {
    pids, _ := win.EnumProcesses()

    for _, pid := range pids {
        hSnap, _ := win.CreateToolhelp32Snapshot(co.TH32CS_SNAPPROCESS, pid)
        defer hSnap.CloseHandle()

        hSnap.EnumProcesses(func(pe32 *win.PROCESSENTRY32) {
            fmt.Printf("PID: %d @ %s\n",
                pe32.Th32ProcessID, pe32.SzExeFile())
        })
    }
}
英文:

The code is cleaner if you use Windigo (error checking omitted for brevity):

package main

import (
    "fmt"

    "github.com/rodrigocfd/windigo/win"
    "github.com/rodrigocfd/windigo/win/co"
)

func main() {
    pids, _ := win.EnumProcesses()

    for _, pid := range pids {
        hSnap, _ := win.CreateToolhelp32Snapshot(co.TH32CS_SNAPMODULE, pid)
        defer hSnap.CloseHandle()

        hSnap.EnumModules(func(me32 *win.MODULEENTRY32) {
            fmt.Printf("PID: %d, %s @ %s\n",
                me32.Th32ProcessID, me32.SzModule(), me32.SzExePath())
        })
    }
}

Or if you just want the processes, without the modules:

package main

import (
    "fmt"

    "github.com/rodrigocfd/windigo/win"
    "github.com/rodrigocfd/windigo/win/co"
)

func main() {
    pids, _ := win.EnumProcesses()

    for _, pid := range pids {
        hSnap, _ := win.CreateToolhelp32Snapshot(co.TH32CS_SNAPPROCESS, pid)
        defer hSnap.CloseHandle()

        hSnap.EnumProcesses(func(pe32 *win.PROCESSENTRY32) {
            fmt.Printf("PID: %d @ %s\n",
                pe32.Th32ProcessID, pe32.SzExeFile())
        })
    }
}

huangapple
  • 本文由 发表于 2012年7月6日 13:23:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/11356264.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定