Sebastián Grignoli
Sebastián Grignoli

Reputation: 33462

List of currently running process in golang, Windows version

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

I need something like:

List of currently running process in Golang

but usable under Windows too.

Upvotes: 8

Views: 9793

Answers (5)

rodrigocfd
rodrigocfd

Reputation: 8078

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())
        })
    }
}

Upvotes: 0

Zombo
Zombo

Reputation: 1

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

Upvotes: 0

fluter
fluter

Reputation: 13846

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.

Upvotes: 3

Michael
Michael

Reputation: 96

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 :) An example on how to use: https://gist.github.com/3083408

Upvotes: 8

axw
axw

Reputation: 7083

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.

Upvotes: 5

Related Questions