|
|
|
package win32api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
func WTSGetActiveConsoleSessionId() (DWORD, error) {
|
|
|
|
kernel32, err := syscall.LoadLibrary("kernel32.dll")
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.New("Can't Load Kernel32 API")
|
|
|
|
}
|
|
|
|
defer syscall.FreeLibrary(kernel32)
|
|
|
|
WTGet, err := syscall.GetProcAddress(syscall.Handle(kernel32), "WTSGetActiveConsoleSessionId")
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.New("Can't Load WTSGetActiveConsoleSessionId API")
|
|
|
|
}
|
|
|
|
res, _, _ := syscall.Syscall(uintptr(WTGet), 0, 0, 0, 0)
|
|
|
|
return DWORD(res), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CloseHandle(hObject HANDLE) error {
|
|
|
|
kernel32, err := syscall.LoadLibrary("kernel32.dll")
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("Can't Load Kernel32 API")
|
|
|
|
}
|
|
|
|
defer syscall.FreeLibrary(kernel32)
|
|
|
|
CH, err := syscall.GetProcAddress(syscall.Handle(kernel32), "CloseHandle")
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("Can't Load CloseHandle API")
|
|
|
|
}
|
|
|
|
if r, _, err := syscall.Syscall(uintptr(CH), 1, uintptr(hObject), 0, 0); r == 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateToolhelp32Snapshot(dwFlags, th32ProcessID DWORD) (HANDLE, error) {
|
|
|
|
kernel32, err := syscall.LoadLibrary("kernel32.dll")
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.New("Can't Load Kernel32 API")
|
|
|
|
}
|
|
|
|
defer syscall.FreeLibrary(kernel32)
|
|
|
|
CTS, err := syscall.GetProcAddress(syscall.Handle(kernel32), "CreateToolhelp32Snapshot")
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.New("Can't Load CreateToolhelp32Snapshot API")
|
|
|
|
}
|
|
|
|
r, _, err := syscall.Syscall(uintptr(CTS), 2, uintptr(dwFlags), uintptr(th32ProcessID), 0)
|
|
|
|
if int(r) == -1 {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return HANDLE(r), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Process32Next(hSnapshot HANDLE, lppe *PROCESSENTRY32) error {
|
|
|
|
kernel32, err := syscall.LoadLibrary("kernel32.dll")
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("Can't Load Kernel32 API")
|
|
|
|
}
|
|
|
|
defer syscall.FreeLibrary(kernel32)
|
|
|
|
PN, err := syscall.GetProcAddress(syscall.Handle(kernel32), "Process32Next")
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("Can't Load Process32Next API")
|
|
|
|
}
|
|
|
|
r, _, err := syscall.Syscall(uintptr(PN), 2, uintptr(hSnapshot), uintptr(unsafe.Pointer(lppe)), 0)
|
|
|
|
if int(r) != 1 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetProcessId(Process HANDLE) uint32 {
|
|
|
|
kernel32, err := syscall.LoadLibrary("kernel32.dll")
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
defer syscall.FreeLibrary(kernel32)
|
|
|
|
GPI, err := syscall.GetProcAddress(syscall.Handle(kernel32), "GetProcessId")
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
r, _, _ := syscall.Syscall(uintptr(GPI), 1, uintptr(Process), 0, 0)
|
|
|
|
return uint32(r)
|
|
|
|
}
|