Get windows process start time

Viewed 347

I know there is package like go-ps to give us active running process in Windows, but this package doesn't have process start time and other information of process.

And I know in Windows power shell with this command I can get process start time.

Get-Process | select name, starttime

I don't want to execute command and get result the parse it, actually the main Idea is how to get process time with main Go packages like os.

1 Answers

You can use the w32 package for this. I maintain this repository and you can ask to include new WinAPI functions by creating an issue or do it yourself and open a pull request.

Here is a sample of how you can get the current process' run time. We wait a second, ask for the time and the output is slightly higher than a second.

package main

import (
    "fmt"
    "time"

    "github.com/gonutz/w32/v2"
)

func main() {
    time.Sleep(time.Second)
    creation, _, _, _, ok := w32.GetProcessTimes(w32.GetCurrentProcess())
    if !ok {
        panic("GetProcessTimes failed")
    }
    fmt.Println("creation", creation.Time())
    fmt.Println("run time", time.Now().Sub(creation.Time()))
}
Related