Documentation ¶
Index ¶
- func Cmd(name string, args []string, sysProcAttr *syscall.SysProcAttr) *exec.Cmd
- func CmdWithoutWindow(name string, args ...string) *exec.Cmddeprecated
- func GetLocalLangLoc(defaultLang, defaultLoc string) (string, string)
- func IsSingleInstance(curExeName string) booldeprecated
- func IsTaskRunning(exeName string) bool
- func ListenToDelete(ch chan string, cbFunc func(string, error), sysProcAttr *syscall.SysProcAttr)
- func TaskKill(exeName string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cmd ¶ added in v2.4.1
Example ¶
cmd := Cmd("powershell", []string{fmt.Sprintf("Get-FileHash %s -Algorithm md5 | select Hash,Path", os.Args[0])}, &syscall.SysProcAttr{ HideWindow: false, CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP, }) rtnBytes, err := cmd.Output() if err != nil { return // <-- github action 會無法執行成功,為了避免影響測試,故不進行錯誤處理 } log.Println(string(rtnBytes))
Output:
func CmdWithoutWindow
deprecated
Deprecated: Use the Cmd to instead of this.
Example ¶
cmd := CmdWithoutWindow("powershell", fmt.Sprintf("Get-FileHash %s -Algorithm md5 | select Hash,Path", os.Args[0])) rtnBytes, err := cmd.Output() if err != nil { return // <-- github action 會無法執行成功,為了避免影響測試,故不進行錯誤處理 } log.Println(string(rtnBytes))
Output:
func GetLocalLangLoc ¶
GetLocalLangLoc Get-Culture List: http://www.codedigest.com/CodeDigest/207-Get-All-Language-Country-Code-List-for-all-Culture-in-C---ASP-Net.aspx Chinese (Singapore) zh-SG Chinese (People's Republic of China) zh-CN Chinese (Hong Kong S.A.R.) zh-HK Chinese (Macao S.A.R.) zh-MO English (United States) en-US Japanese (Japan) ja-JP
Example ¶
lang, loc := GetLocalLangLoc("en", "US") log.Println(lang, loc)
Output:
func IsSingleInstance
deprecated
Deprecated: I do not recommend that you use this method. Use "CreateMutex" instead, see below, https://github.com/CarsonSlovoka/go-pkg/blob/4b6ee040d9e5d9831740d20918e992a260594e80/v2/w32/kernel32_func_test.go#L9-L35
Example ¶
testApp := "notepad.exe" if err := exec.Command(testApp).Start(); err != nil { panic(err) } if !IsSingleInstance(testApp) { panic("假設notepad的應用程式對象只有一個,那麼不該有錯誤") } // 建立第二個notepad if err := exec.Command(testApp).Start(); err != nil { // run again panic(err) } if IsSingleInstance(testApp) { panic("此時有兩個對象,所以InSingleInstance應該為false") }
Output:
func IsTaskRunning ¶
Example ¶
testApp := "taskmgr.exe" // Task manager if err := exec.Command(testApp).Start(); err != nil { // please run with admin panic(err) // The requested operation requires elevation. } time.Sleep(250 * time.Millisecond) if err := TaskKill(testApp); err != nil { panic(err) } if IsTaskRunning(testApp) { panic("The program was still alive.") }
Output:
func ListenToDelete ¶
func ListenToDelete(ch chan string, cbFunc func(string, error), sysProcAttr *syscall.SysProcAttr)
ListenToDelete Send the file path which you want to delete to the channel, and then it will start another process by Powershell to delete the file. If you want to delete the executable (self), you can send os.Args[0] "ch" 正常而言您不需要透過此函數來刪除檔案,僅需要`os.Remove()`即可。 此函數的特色:能刪除掉自身執行檔 如果您選擇的是刪除自身執行檔,那麼callbackFunc,不應該寫得太複雜,因為結束動作是直接打開powershell運行, 因此當您的callback要花很多時間,有可能還沒跑完就刪除了
Example ¶
Delete Self Exe
chanRemoveFile := make(chan string) chanQuit := make(chan bool) go ListenToDelete(chanRemoveFile, func(curFile string, err error) { defer close(chanQuit) if err != nil { fmt.Println(err) return } // fmt.Printf("remove the file successful: %s\n", curFile) // 由於我們用start去運行,因此沒有錯誤只是代表呼叫成功,但不意味已經刪除該檔案了 fmt.Printf("call the del command successful: %s\n", curFile) }, nil) chanRemoveFile <- os.Args[0] select { case <-chanQuit: return }
Output:
Example (Multiple) ¶
chanRemoveFile := make(chan string) chanQuit := make(chan bool) deleteFiles := []string{ "temp.txt", "temp.exe", } for _, testFilepath := range deleteFiles { f, _ := os.Create(testFilepath) if err := f.Close(); err != nil { panic(err) } } time.Sleep(time.Second) // just let you see the file created. wg := new(sync.WaitGroup) wg.Add(len(deleteFiles)) go ListenToDelete(chanRemoveFile, func(curFile string, err error) { defer wg.Done() if err != nil { fmt.Println(err) return } fmt.Printf("call the del command successful: %s\n", curFile) }, nil) go func() { var curFile string for { if len(deleteFiles) == 0 { close(chanRemoveFile) close(chanQuit) break } curFile, deleteFiles = deleteFiles[0], deleteFiles[1:] // pop chanRemoveFile <- curFile } }() select { case <-chanQuit: wg.Wait() time.Sleep(time.Second) // goland的debug貌似主程序結束,powershell的呼叫也會被中斷,所以要等待,如果是打包出去的執行檔,不需要特別加上sleep for _, filePath := range []string{ "temp.txt", "temp.exe", } { if _, err := os.Stat(filePath); !os.IsNotExist(err) { panic("the file still exists, not deleted. " + err.Error()) } } return }
Output:
func TaskKill ¶
TaskKill 強制關閉程式, 可透過tasklist查看task清單
Example ¶
testApp := "powershell.exe" // 如果用taskmgr.exe(Task manager)會需要提升管理權限才有辦法運行,否則會有錯誤: The requested operation requires elevation. if err := exec.Command(testApp).Start(); err != nil { // please run with admin panic(err) // The requested operation requires elevation. } time.Sleep(250 * time.Millisecond) if err := TaskKill(testApp); err != nil { panic(err) } if IsTaskRunning(testApp) { panic("The program was still alive.") }
Output:
Types ¶
This section is empty.