本文整理汇总了Golang中syscall.OpenProcess函数的典型用法代码示例。如果您正苦于以下问题:Golang OpenProcess函数的具体用法?Golang OpenProcess怎么用?Golang OpenProcess使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了OpenProcess函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Get
func (self *ProcMem) Get(pid int) error {
handle, err := syscall.OpenProcess(PROCESS_ALL_ACCESS, false, uint32(pid))
defer syscall.CloseHandle(handle)
if err != nil {
return fmt.Errorf("OpenProcess fails with %v", err)
}
var mem PROCESS_MEMORY_COUNTERS_EX
mem.CB = uint32(unsafe.Sizeof(mem))
r1, _, e1 := procGetProcessMemoryInfo.Call(
uintptr(handle),
uintptr(unsafe.Pointer(&mem)),
uintptr(mem.CB),
)
if r1 == 0 {
if e1 != nil {
return error(e1)
} else {
return syscall.EINVAL
}
}
self.Resident = uint64(mem.WorkingSetSize)
self.Size = uint64(mem.PrivateUsage)
// Size contains only to the Private Bytes
// Virtual Bytes are the Working Set plus paged Private Bytes and standby list.
return nil
}
示例2: findExePath
// findExePath searches for process pid, and returns its executable path.
func findExePath(pid int) (string, error) {
// Original code suggested different approach (see below).
// Maybe it could be useful in the future.
//
// Find executable path from PID/handle on Windows:
// https://msdn.microsoft.com/en-us/library/aa366789(VS.85).aspx
p, err := syscall.OpenProcess(syscall.PROCESS_QUERY_INFORMATION, false, uint32(pid))
if err != nil {
return "", err
}
defer syscall.CloseHandle(p)
n := uint32(128)
for {
buf := make([]uint16, int(n))
err = _QueryFullProcessImageName(p, 0, &buf[0], &n)
switch err {
case syscall.ERROR_INSUFFICIENT_BUFFER:
// try bigger buffer
n *= 2
// but stop if it gets too big
if n > 10000 {
return "", err
}
case nil:
return syscall.UTF16ToString(buf[:n]), nil
default:
return "", err
}
}
}
示例3: OpenProcess
func OpenProcess(pid uint32) (*Process, error) {
const da = syscall.STANDARD_RIGHTS_READ | syscall.PROCESS_QUERY_INFORMATION | syscall.SYNCHRONIZE
h, err := syscall.OpenProcess(da, false, uint32(pid))
if err != nil {
return nil, os.NewSyscallError("OpenProcess", err)
}
procEntry, err := win32.FindProcessEntry(pid)
if err != nil {
return nil, err
}
modEntry, err := win32.FirstModuleEntry(pid)
if err != nil {
return nil, err
}
handleCount, err := win32.GetProcessHandleCount(h)
if err != nil {
return nil, err
}
return &Process{
Pid: pid,
handle: uintptr(h),
ParentId: procEntry.ParentProcessID,
Name: syscall.UTF16ToString(modEntry.ModuleName[:]),
Executable: syscall.UTF16ToString(procEntry.ExeFile[:]),
CmdLine: syscall.UTF16ToString(modEntry.ExePath[:]),
HandleCount: handleCount,
ThreadCount: procEntry.Threads,
}, nil
}
示例4: initJob
func initJob() {
// Create Job object and assign current process to it.
jobObject, err := createJobObject(nil, nil)
if err != nil {
log.Printf("CreateJobObject failed: %v", err)
return
}
if err = assignProcessToJobObject(jobObject, currentProcess); err != nil {
log.Printf("AssignProcessToJobObject failed: %v", err)
syscall.Close(jobObject)
return
}
iocp, err := syscall.CreateIoCompletionPort(syscall.InvalidHandle, 0, 0, 1)
if err != nil {
log.Printf("CreateIoCompletionPort failed: %v", err)
syscall.Close(jobObject)
return
}
port := JOBOBJECT_ASSOCIATE_COMPLETION_PORT{
CompletionKey: uintptr(jobObject),
CompletionPort: iocp,
}
err = setInformationJobObject(jobObject, JobObjectAssociateCompletionPortInformation, uintptr(unsafe.Pointer(&port)), uint32(unsafe.Sizeof(port)))
if err != nil {
log.Printf("SetInformationJobObject failed: %v", err)
syscall.Close(jobObject)
syscall.Close(iocp)
return
}
// Read Job notifications about new "child" processes and collect them in childProcesses.
go func() {
var code, key uint32
var o *syscall.Overlapped
for {
err := syscall.GetQueuedCompletionStatus(iocp, &code, &key, &o, syscall.INFINITE)
if err != nil {
log.Printf("GetQueuedCompletionStatus failed: %v", err)
return
}
if key != uint32(jobObject) {
panic("Invalid GetQueuedCompletionStatus key parameter")
}
if code == JOB_OBJECT_MSG_NEW_PROCESS {
pid := int(uintptr(unsafe.Pointer(o)))
if pid == syscall.Getpid() {
continue
}
c, err := syscall.OpenProcess(syscall.PROCESS_QUERY_INFORMATION, false, uint32(pid))
if err != nil {
log.Printf("OpenProcess failed: %v", err)
continue
}
childMu.Lock()
childProcesses = append(childProcesses, c)
childMu.Unlock()
}
}
}()
}
示例5: terminateProcess
func terminateProcess(pid, exitcode int) error {
h, e := syscall.OpenProcess(syscall.PROCESS_TERMINATE, false, uint32(pid))
if e != nil {
return NewSyscallError("OpenProcess", e)
}
defer syscall.CloseHandle(h)
e = syscall.TerminateProcess(h, uint32(exitcode))
return NewSyscallError("TerminateProcess", e)
}
示例6: findProcess
func findProcess(pid int) (p *Process, err error) {
const da = syscall.STANDARD_RIGHTS_READ |
syscall.PROCESS_QUERY_INFORMATION | syscall.SYNCHRONIZE
h, e := syscall.OpenProcess(da, false, uint32(pid))
if e != nil {
return nil, NewSyscallError("OpenProcess", e)
}
return newProcess(pid, uintptr(h)), nil
}
示例7: killProcess
// killProcess terminates a given pid using TerminateProcess.
func killProcess(pid uint32) error {
handle, err := syscall.OpenProcess(syscall.PROCESS_TERMINATE, false /* inheritHandle */, pid)
if err != nil {
return err
}
defer syscall.CloseHandle(handle)
log.VV("TerminateProcess(%v) with handle %v", pid, handle)
return syscall.TerminateProcess(handle, 1)
}
示例8: TestGetProcessMemoryInfo
func TestGetProcessMemoryInfo(t *testing.T) {
h, err := syscall.OpenProcess(syscall.PROCESS_QUERY_INFORMATION, false, uint32(syscall.Getpid()))
if err != nil {
t.Fatal(err)
}
counters, err := GetProcessMemoryInfo(h)
if err != nil {
t.Fatal(err)
}
t.Logf("GetProcessMemoryInfo: ProcessMemoryCountersEx=%+v", counters)
}
示例9: TestGetProcessImageFileName
func TestGetProcessImageFileName(t *testing.T) {
h, err := syscall.OpenProcess(syscall.PROCESS_QUERY_INFORMATION, false, uint32(syscall.Getpid()))
if err != nil {
t.Fatal(err)
}
filename, err := GetProcessImageFileName(h)
if err != nil {
t.Fatal(err)
}
t.Logf("GetProcessImageFileName: %v", filename)
}
示例10: _pidAlive
// _pidAlive tests whether a process is alive or not
func _pidAlive(pid int) bool {
h, err := syscall.OpenProcess(processDesiredAccess, false, uint32(pid))
if err != nil {
return false
}
var ec uint32
if e := syscall.GetExitCodeProcess(h, &ec); e != nil {
return false
}
return ec == exit_STILL_ACTIVE
}
示例11: getMemoryInfo
func getMemoryInfo(pid int32) (PROCESS_MEMORY_COUNTERS, error) {
var mem PROCESS_MEMORY_COUNTERS
c, err := syscall.OpenProcess(syscall.PROCESS_QUERY_INFORMATION, false, uint32(pid))
if err != nil {
return mem, err
}
defer syscall.CloseHandle(c)
if err := getProcessMemoryInfo(c, &mem); err != nil {
return mem, err
}
return mem, err
}
示例12: processExists
func processExists(pid int) bool {
h, err := syscall.OpenProcess(processQueryLimitedInformation, false, uint32(pid))
if err != nil {
return false
}
var c uint32
err = syscall.GetExitCodeProcess(h, &c)
syscall.Close(h)
if err != nil {
return c == stillActive
}
return true
}
示例13: getProcName
// getProcName returns the process name associated with the PID.
func getProcName(pid int) (string, error) {
handle, err := syscall.OpenProcess(processQueryLimitedInfoAccess, false, uint32(pid))
if err != nil {
return "", errors.Wrapf(err, "OpenProcess failed for pid=%v", pid)
}
defer syscall.CloseHandle(handle)
filename, err := windows.GetProcessImageFileName(handle)
if err != nil {
return "", errors.Wrapf(err, "GetProcessImageFileName failed for pid=%v", pid)
}
return filepath.Base(filename), nil
}
示例14: getParentPid
// getParentPid returns the parent process ID of a process.
func getParentPid(pid int) (int, error) {
handle, err := syscall.OpenProcess(processQueryLimitedInfoAccess, false, uint32(pid))
if err != nil {
return RunStateUnknown, errors.Wrapf(err, "OpenProcess failed for pid=%v", pid)
}
defer syscall.CloseHandle(handle)
procInfo, err := windows.NtQueryProcessBasicInformation(handle)
if err != nil {
return 0, errors.Wrapf(err, "NtQueryProcessBasicInformation failed for pid=%v", pid)
}
return int(procInfo.InheritedFromUniqueProcessID), nil
}
示例15: getRusage
func getRusage(pid int32) (*syscall.Rusage, error) {
var CPU syscall.Rusage
c, err := syscall.OpenProcess(syscall.PROCESS_QUERY_INFORMATION, false, uint32(pid))
if err != nil {
return nil, err
}
defer syscall.CloseHandle(c)
if err := syscall.GetProcessTimes(c, &CPU.CreationTime, &CPU.ExitTime, &CPU.KernelTime, &CPU.UserTime); err != nil {
return nil, err
}
return &CPU, nil
}