Newer
Older
GoModules / RunningAs-Example.go
root on 2 Feb 2022 949 bytes RunningAs added
  1. /***
  2. * Just a small demonstration script that makes an alert box containing the user the current process is running as
  3. */
  4. package main
  5.  
  6. import (
  7. "log"
  8. "os/user"
  9. "syscall"
  10. "unsafe"
  11. )
  12.  
  13. // MessageBox of Win32 API.
  14. func MessageBox(hwnd uintptr, caption, title string, flags uint) int {
  15. ret, _, _ := syscall.NewLazyDLL("user32.dll").NewProc("MessageBoxW").Call(
  16. uintptr(hwnd),
  17. uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(caption))),
  18. uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))),
  19. uintptr(flags))
  20.  
  21. return int(ret)
  22. }
  23.  
  24. // MessageBoxPlain of Win32 API.
  25. func MessageBoxPlain(title, caption string) int {
  26. const (
  27. NULL = 0
  28. MB_OK = 0
  29. )
  30. return MessageBox(NULL, caption, title, MB_OK)
  31. }
  32.  
  33. func main() {
  34. currentUser, err := user.Current()
  35. if err != nil {
  36. log.Fatalf("Unable to get current user: %s", err)
  37. }
  38. userId := currentUser.Username
  39.  
  40. MessageBoxPlain("Running as", userId)
  41. }
Buy Me A Coffee