在 Windows 命令提示字元下執行執行檔以及參數就可以了:
cgx_STATIC.exe -b C:\Users\m3\AppData\Local\Temp\shot-277325955.fbd
但是,當透過 Golang 執行相同的可執行檔時,在可執行檔建立一些輸出檔後,可執行檔將變得無回應。
<code> // Run an executable and print its log into a file. func RunWithLogFile(pthExe string, arg []string, fLog *os.File) error { cmd := exec.Command(pthExe, arg...) stdout, err := cmd.StdoutPipe() if err != nil { return err } stderr, err := cmd.StderrPipe() if err != nil { return err } err = cmd.Start() if err != nil { return err } // Stream logs: // https://stackoverflow.com/a/48849811/3405291 scannerOut := bufio.NewScanner(stdout) scannerErr := bufio.NewScanner(stderr) scannerOut.Split(bufio.ScanRunes) scannerErr.Split(bufio.ScanRunes) for scannerOut.Scan() { _, err = fLog.WriteString(scannerOut.Text()) if err != nil { return err } } for scannerErr.Scan() { _, err = fLog.WriteString(scannerErr.Text()) if err != nil { return err } } if scannerOut.Err() != nil { return err } if scannerErr.Err() != nil { return err } err = cmd.Wait() return err } </code>
我想知道上面的 Go 程式碼是否存在某種錯誤或不適合執行可執行檔?
單獨的 goroutine
按照@BurakSerdar的建議,我在單獨的goroutines中從stdout和stderr讀取內容,但問題沒有解決:
<code> // Run an executable and print its log into a file. func RunWithLogFile(pthExe string, arg []string, fLog *os.File) error { cmd := exec.Command(pthExe, arg...) stdout, err := cmd.StdoutPipe() if err != nil { return err } stderr, err := cmd.StderrPipe() if err != nil { return err } var wg sync.WaitGroup wg.Add(2) go streamToLogFile(stdout, fLog, &wg) go streamToLogFile(stderr, fLog, &wg) err = cmd.Start() if err != nil { return err } wg.Wait() err = cmd.Wait() return err } func streamToLogFile(output io.ReadCloser, fLog *os.File, wg *sync.WaitGroup) { defer wg.Done() scanner := bufio.NewScanner(output) scanner.Split(bufio.ScanRunes) for scanner.Scan() { _, err := fLog.WriteString(scanner.Text()) if err != nil { log.Printf("error: write to log file: %s", err.Error()) } } err := scanner.Err() if err != nil { log.Printf("error: write to log file: %s", err.Error()) } } </code>
正確答案
透過使用管理員權限執行可執行檔解決了無回應問題。我正在這樣做:
A C# code runs a Go code and the Go code runs a C Code, i.e. external executable.
C 可執行檔呼叫一些 OpenGL GLUT 呼叫。也許他們需要管理員權限。
透過 C# 啟動 Go 程式碼解決了這個問題,如下所示:https://www.php.cn/link/ac90e5f00f7542d99231f63fb0dfeecf
#<code> public static void RunLogic(string exePath, string args, PostProcess pp) { cmd = new Process(); try { cmd.StartInfo.FileName = exePath; cmd.StartInfo.Arguments = args; cmd.StartInfo.UseShellExecute = true; cmd.StartInfo.CreateNoWindow = false; cmd.StartInfo.RedirectStandardOutput = false; cmd.StartInfo.RedirectStandardError = false; cmd.StartInfo.RedirectStandardInput = false; // Vista or higher check. // https://www.php.cn/link/ac90e5f00f7542d99231f63fb0dfeecf if (System.Environment.OSVersion.Version.Major >= 6) { // Run with admin privileges to avoid a non-responsive executable. cmd.StartInfo.Verb = "runas"; } cmd.EnableRaisingEvents = true; cmd.Exited += new EventHandler(cmd_Exited); cmd.Exited += new EventHandler(pp); cmd.Start(); } catch (Exception ex) { RhinoApp.WriteLine("Error on process start: {0}", ex.Message); } } </code>
以上是在命令列上運行可執行檔沒問題,但透過另一個程式運行會導致無響應的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Gohandlesinterfacesandtypeassertionseffectively,enhancingcodeflexibilityandrobustness.1)Typeassertionsallowruntimetypechecking,asseenwiththeShapeinterfaceandCircletype.2)Typeswitcheshandlemultipletypesefficiently,usefulforvariousshapesimplementingthe

Go語言的錯誤處理通過errors.Is和errors.As函數變得更加靈活和可讀。 1.errors.Is用於檢查錯誤是否與指定錯誤相同,適用於錯誤鏈的處理。 2.errors.As不僅能檢查錯誤類型,還能將錯誤轉換為具體類型,方便提取錯誤信息。使用這些函數可以簡化錯誤處理邏輯,但需注意錯誤鏈的正確傳遞和避免過度依賴以防代碼複雜化。

tomakegoapplicationsRunfasterandMorefly,useProflingTools,leverageConCurrency,andManageMoryfectily.1)usepprofforcpuorforcpuandmemoryproflingtoidentifybottlenecks.2)upitizegorizegoroutizegoroutinesandchannelstoparalletaparelalyizetasksandimproverperformance.3)

go'sfutureisbrightwithtrendslikeMprikeMprikeTooling,仿製藥,雲 - 納蒂維德象,performanceEnhancements,andwebassemblyIntegration,butchallengeSinclainSinClainSinClainSiNgeNingsImpliCityInsImplicityAndimimprovingingRornhandRornrorlling。

goroutinesarefunctionsormethodsthatruncurranceingo,啟用效率和燈威量。 1)shememanagedbodo'sruntimemultimusingmultiplexing,允許千sstorunonfewerosthreads.2)goroutinessimproverentimensImproutinesImproutinesImproveranceThroutinesImproveranceThrountinesimproveranceThroundinesImproveranceThroughEasySytaskParallowalizationAndeff

purposeoftheInitfunctionoIsistoInitializeVariables,setUpConfigurations,orperformneccesSetarySetupBeforEtheMainFunctionExeCutes.useInitby.UseInitby:1)placingitinyourcodetorunautoamenationally oneraty oneraty oneraty on inity in ofideShortAndAndAndAndForemain,2)keepitiTshortAntAndFocusedonSimImimpletasks,3)

Gointerfacesaremethodsignaturesetsthattypesmustimplement,enablingpolymorphismwithoutinheritanceforcleaner,modularcode.Theyareimplicitlysatisfied,usefulforflexibleAPIsanddecoupling,butrequirecarefulusetoavoidruntimeerrorsandmaintaintypesafety.

在Go中使用recover()函數可以從panic中恢復。具體方法是:1)在defer函數中使用recover()捕獲panic,避免程序崩潰;2)記錄詳細的錯誤信息以便調試;3)根據具體情況決定是否恢復程序執行;4)謹慎使用,以免影響性能。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具