process.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package goautoit
  2. import "log"
  3. // IsAdmin -- Checks if the current user has full administrator privileges.
  4. func IsAdmin() int {
  5. ret, _, lastErr := isAdmin.Call()
  6. if ret == 0 {
  7. log.Println(lastErr)
  8. }
  9. return int(ret)
  10. }
  11. // ProcessClose -- Terminates a named process.
  12. func ProcessClose(process string) int {
  13. ret, _, lastErr := processClose.Call(strPtr(process))
  14. if ret == 0 {
  15. log.Println(lastErr)
  16. }
  17. return int(ret)
  18. }
  19. // ProcessExists -- Checks to see if a specified process exists.
  20. func ProcessExists(process string) int {
  21. ret, _, lastErr := processExists.Call(strPtr(process))
  22. if ret == 0 {
  23. log.Println(lastErr)
  24. }
  25. return int(ret)
  26. }
  27. // ProcessSetPriority -- Changes the priority of a process.
  28. func ProcessSetPriority(process string, priority int) int {
  29. ret, _, lastErr := processSetPriority.Call(strPtr(process), intPtr(priority))
  30. if ret == 0 {
  31. log.Println(lastErr)
  32. }
  33. return int(ret)
  34. }
  35. // ProcessWait -- Pauses script execution until a given process exists.
  36. func ProcessWait(process string, args ...interface{}) int {
  37. var timeout int
  38. var ok bool
  39. if len(args) == 0 {
  40. timeout = 0
  41. } else if len(args) == 1 {
  42. if timeout, ok = args[0].(int); !ok {
  43. panic("timeout must be a int")
  44. }
  45. } else {
  46. panic("Error parameters")
  47. }
  48. ret, _, lastErr := processWait.Call(strPtr(process), intPtr(timeout))
  49. if ret == 0 {
  50. log.Println(lastErr)
  51. }
  52. return int(ret)
  53. }
  54. // ProcessWaitClose -- Pauses script execution until a given process does not exist.
  55. func ProcessWaitClose(process string, args ...interface{}) int {
  56. var timeout int
  57. var ok bool
  58. if len(args) == 0 {
  59. timeout = 0
  60. } else if len(args) == 1 {
  61. if timeout, ok = args[0].(int); !ok {
  62. panic("timeout must be a int")
  63. }
  64. } else {
  65. panic("Error parameters")
  66. }
  67. ret, _, lastErr := processWaitClose.Call(strPtr(process), intPtr(timeout))
  68. if ret == 0 {
  69. log.Println(lastErr)
  70. }
  71. return int(ret)
  72. }
  73. // RunWait -- Runs an external program and pauses script execution until the program finishes.
  74. // flag 3(max) 6(min) 9(normal) 0(hide)
  75. func RunWait(szProgram string, args ...interface{}) int {
  76. var szDir string
  77. var flag int
  78. var ok bool
  79. if len(args) == 0 {
  80. szDir = ""
  81. flag = SWShowNormal
  82. } else if len(args) == 1 {
  83. if szDir, ok = args[0].(string); !ok {
  84. panic("szDir must be a string")
  85. }
  86. flag = SWShowNormal
  87. } else if len(args) == 2 {
  88. if szDir, ok = args[0].(string); !ok {
  89. panic("szDir must be a string")
  90. }
  91. if flag, ok = args[1].(int); !ok {
  92. panic("flag must be a int")
  93. }
  94. } else {
  95. panic("Too more parameter")
  96. }
  97. pid, _, lastErr := runWait.Call(strPtr(szProgram), strPtr(szDir), intPtr(flag))
  98. // log.Println(pid)
  99. if int(pid) == 0 {
  100. log.Println(lastErr)
  101. }
  102. return int(pid)
  103. }
  104. // RunAs -- Runs an external program under the context of a different user.
  105. func RunAs(user, domain, password string, loginFlag int, szProgram string, args ...interface{}) int {
  106. var szDir string
  107. var flag int
  108. var ok bool
  109. if len(args) == 0 {
  110. szDir = ""
  111. flag = SWShowNormal
  112. } else if len(args) == 1 {
  113. if szDir, ok = args[0].(string); !ok {
  114. panic("szDir must be a string")
  115. }
  116. flag = SWShowNormal
  117. } else if len(args) == 2 {
  118. if szDir, ok = args[0].(string); !ok {
  119. panic("szDir must be a string")
  120. }
  121. if flag, ok = args[1].(int); !ok {
  122. panic("flag must be a int")
  123. }
  124. } else {
  125. panic("Too more parameter")
  126. }
  127. pid, _, lastErr := runAs.Call(strPtr(user), strPtr(domain), strPtr(password), intPtr(loginFlag), strPtr(szProgram), strPtr(szDir), intPtr(flag))
  128. // log.Println(pid)
  129. if int(pid) == 0 {
  130. log.Println(lastErr)
  131. }
  132. return int(pid)
  133. }
  134. // RunAsWait -- Runs an external program under the context of a different user and pauses script execution until the program finishes.
  135. func RunAsWait(user, domain, password string, loginFlag int, szProgram string, args ...interface{}) int {
  136. var szDir string
  137. var flag int
  138. var ok bool
  139. if len(args) == 0 {
  140. szDir = ""
  141. flag = SWShowNormal
  142. } else if len(args) == 1 {
  143. if szDir, ok = args[0].(string); !ok {
  144. panic("szDir must be a string")
  145. }
  146. flag = SWShowNormal
  147. } else if len(args) == 2 {
  148. if szDir, ok = args[0].(string); !ok {
  149. panic("szDir must be a string")
  150. }
  151. if flag, ok = args[1].(int); !ok {
  152. panic("flag must be a int")
  153. }
  154. } else {
  155. panic("Too more parameter")
  156. }
  157. pid, _, lastErr := runAsWait.Call(strPtr(user), strPtr(domain), strPtr(password), intPtr(loginFlag), strPtr(szProgram), strPtr(szDir), intPtr(flag))
  158. // log.Println(pid)
  159. if int(pid) == 0 {
  160. log.Println(lastErr)
  161. }
  162. return int(pid)
  163. }