• golang--获取进程ID(windows)


    package main
    
    import (
    	"fmt"
    	"strconv"
    	"syscall"
    	"unsafe"
    )
    
    type ulong int32
    type ulong_ptr uintptr
    
    type PROCESSENTRY32 struct {
    	dwSize              ulong
    	cntUsage            ulong
    	th32ProcessID       ulong
    	th32DefaultHeapID   ulong_ptr
    	th32ModuleID        ulong
    	cntThreads          ulong
    	th32ParentProcessID ulong
    	pcPriClassBase      ulong
    	dwFlags             ulong
    	szExeFile           [260]byte
    }
    
    func main() {
    	kernel32 := syscall.NewLazyDLL("kernel32.dll")
    	CreateToolhelp32Snapshot := kernel32.NewProc("CreateToolhelp32Snapshot")
    	pHandle, _, _ := CreateToolhelp32Snapshot.Call(uintptr(0x2), uintptr(0x0))
    	if int(pHandle) == -1 {
    		return
    	}
    	Process32Next := kernel32.NewProc("Process32Next")
    	for {
    		var proc PROCESSENTRY32
    		proc.dwSize = ulong(unsafe.Sizeof(proc))
    		if rt, _, _ := Process32Next.Call(uintptr(pHandle), uintptr(unsafe.Pointer(&proc))); int(rt) == 1 {
    			fmt.Println("ProcessName : " + string(proc.szExeFile[0:]))
    			fmt.Println("th32ModuleID : " + strconv.Itoa(int(proc.th32ModuleID)))
    			fmt.Println("ProcessID : " + strconv.Itoa(int(proc.th32ProcessID)))
    		} else {
    			break
    		}
    	}
    	CloseHandle := kernel32.NewProc("CloseHandle")
    	_, _, _ = CloseHandle.Call(pHandle)
    }
    

      

  • 相关阅读:
    使用 Python 编码和解码 JSON 对象
    搞定github下载加速
    git错误:fatal: Could not read from remote repository.解决
    webstorm安装配置
    node.js下载安装
    IDEA安装小配置
    JAVA软件安装
    关于升级一般软件的一些想法
    linux 的 逻辑卷管理
    记一次内核升级。
  • 原文地址:https://www.cnblogs.com/saryli/p/11119327.html
Copyright © 2020-2023  润新知