github.com/iDigitalFlame/xmt@v0.5.4/cmd/list_windows.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  // Copyright (C) 2020 - 2023 iDigitalFlame
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    18  //
    19  
    20  package cmd
    21  
    22  import (
    23  	"sort"
    24  
    25  	"github.com/iDigitalFlame/xmt/device/winapi"
    26  	"github.com/iDigitalFlame/xmt/util/bugtrack"
    27  )
    28  
    29  // Processes attempts to gather the current running Processes and returns them
    30  // as a slice of ProcessInfo structs, otherwise any errors are returned.
    31  func Processes() ([]ProcessInfo, error) {
    32  	if err := winapi.GetDebugPrivilege(); err != nil {
    33  		if bugtrack.Enabled {
    34  			bugtrack.Track("cmd.Processes(): GetDebugPrivilege failed with err=%s", err.Error())
    35  		}
    36  	}
    37  	var (
    38  		r   = make(processList, 0, 64)
    39  		err = winapi.EnumProcesses(func(e winapi.ProcessEntry) error {
    40  			u, _ := e.User()
    41  			r = append(r, ProcessInfo{Name: e.Name, User: u, PID: e.PID, PPID: e.PPID})
    42  			return nil
    43  		})
    44  	)
    45  	sort.Sort(r)
    46  	return r, err
    47  }