mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-06-02 23:23:55 +00:00
165 lines
2.9 KiB
Go
165 lines
2.9 KiB
Go
// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
|
|
// file at the top-level directory of this distribution and at
|
|
// https://github.com/go-vgo/robotgo/blob/master/LICENSE
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
// option. This file may not be copied, modified, or distributed
|
|
// except according to those terms.
|
|
//
|
|
|
|
package ps
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/shirou/gopsutil/process"
|
|
)
|
|
|
|
// Nps process struct
|
|
type Nps struct {
|
|
Pid int32
|
|
Name string
|
|
}
|
|
|
|
// GetPid get the process id
|
|
func GetPid() int32 {
|
|
return int32(os.Getpid())
|
|
}
|
|
|
|
// Pids get the all process id
|
|
func Pids() ([]int32, error) {
|
|
var ret []int32
|
|
pid, err := process.Pids()
|
|
if err != nil {
|
|
return ret, err
|
|
}
|
|
|
|
return pid, err
|
|
}
|
|
|
|
// PidExists determine whether the process exists
|
|
func PidExists(pid int32) (bool, error) {
|
|
abool, err := process.PidExists(pid)
|
|
|
|
return abool, err
|
|
}
|
|
|
|
// Process get the all process struct
|
|
func Process() ([]Nps, error) {
|
|
var npsArr []Nps
|
|
|
|
pid, err := process.Pids()
|
|
if err != nil {
|
|
return npsArr, err
|
|
}
|
|
|
|
for i := 0; i < len(pid); i++ {
|
|
nps, err := process.NewProcess(pid[i])
|
|
if err != nil {
|
|
return npsArr, err
|
|
}
|
|
|
|
names, err := nps.Name()
|
|
if err != nil {
|
|
return npsArr, err
|
|
}
|
|
|
|
np := Nps{
|
|
pid[i],
|
|
names,
|
|
}
|
|
|
|
npsArr = append(npsArr, np)
|
|
}
|
|
|
|
return npsArr, err
|
|
}
|
|
|
|
// FindName find the process name by the process id
|
|
func FindName(pid int32) (string, error) {
|
|
nps, err := process.NewProcess(pid)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
names, err := nps.Name()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return names, err
|
|
}
|
|
|
|
// FindNames find the all process name
|
|
func FindNames() ([]string, error) {
|
|
var strArr []string
|
|
pid, err := process.Pids()
|
|
|
|
if err != nil {
|
|
return strArr, err
|
|
}
|
|
|
|
for i := 0; i < len(pid); i++ {
|
|
nps, err := process.NewProcess(pid[i])
|
|
if err != nil {
|
|
return strArr, err
|
|
}
|
|
|
|
names, err := nps.Name()
|
|
if err != nil {
|
|
return strArr, err
|
|
}
|
|
|
|
strArr = append(strArr, names)
|
|
return strArr, err
|
|
}
|
|
|
|
return strArr, err
|
|
}
|
|
|
|
// FindIds finds the all processes named with a subset
|
|
// of "name" (case insensitive),
|
|
// return matched IDs.
|
|
func FindIds(name string) ([]int32, error) {
|
|
var pids []int32
|
|
nps, err := Process()
|
|
if err != nil {
|
|
return pids, err
|
|
}
|
|
|
|
name = strings.ToLower(name)
|
|
for i := 0; i < len(nps); i++ {
|
|
psname := strings.ToLower(nps[i].Name)
|
|
abool := strings.Contains(psname, name)
|
|
if abool {
|
|
pids = append(pids, nps[i].Pid)
|
|
}
|
|
}
|
|
|
|
return pids, err
|
|
}
|
|
|
|
// FindPath find the process path by the process pid
|
|
func FindPath(pid int32) (string, error) {
|
|
nps, err := process.NewProcess(pid)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
f, err := nps.Exe()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return f, err
|
|
}
|
|
|
|
// Kill kill the process by PID
|
|
func Kill(pid int32) error {
|
|
p := os.Process{Pid: int(pid)}
|
|
return p.Kill()
|
|
}
|