go-vgo / robotgo

RobotGo, Go Native cross-platform RPA and GUI automation @vcaesar
Apache License 2.0
9.6k stars 879 forks source link

robotgo.ActivePid panic #619

Open xingcxb opened 11 months ago

xingcxb commented 11 months ago
func main() {
    app := application.New(application.Options{
              ...
        }
       MainWindow(app *application.App)
}

func MainWindow(app *application.App) {
    actionPid := robotgo.GetPid()
        mainWindow := app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
            ....
        }
        // 自定义事件,双击卡片事件
    customEvents.HandleCardDoubleClick(app, mainWindow, actionPid)
}

func HandleCardDoubleClick(app *application.App, window *application.WebviewWindow, actionPid int) {
    app.Events.On(common.EventsHandleCardDoubleClickToCore, func(e *application.WailsEvent) {
        pasteData := db.FindPasteById(func() int {
            return int(e.Data.(float64))
        }())
        if pasteData.Id == 0 {
            return
        }
        window.Close()
        pasteContent := formatContent(pasteData.Content, pasteData.Type)
        fmt.Println("要释放的数据:", pasteContent)
        fmt.Println("要接受数据的pid:", actionPid)
        if actionPid != 0 {
            pidExists, err := robotgo.PidExists(actionPid)
            if err != nil || !pidExists {
                fmt.Println("查询程序是否存在失败", err.Error())
                return
            }
            fmt.Println(robotgo.FindName(actionPid))
            err = robotgo.ActivePid(actionPid) // 
            if err != nil {
                fmt.Println("切换到下一个程序聚焦失败", err.Error())
                return
            }
            if pasteData.Type == "text" {
                robotgo.TypeStr(pasteContent)
            }
        }

    })
}
SIGSEGV: segmentation violation
PC=0x1a7291820 m=12 sigcode=2
signal arrived during cgo execution

goroutine 48 [syscall]:
runtime.cgocall(0x1049582c4, 0x14000341c98)
        /usr/local/go/src/runtime/cgocall.go:157 +0x44 fp=0x14000341c50 sp=0x14000341c10 pc=0x10402a604
github.com/go-vgo/robotgo._Cfunc_active_PID(0x1c1, 0x0)
        _cgo_gotypes.go:333 +0x30 fp=0x14000341c90 sp=0x14000341c50 pc=0x1047fc960
...
PastePlus/core/api/customEvents.HandleCardDoubleClick.func1(0x140001ed600)
        /Users/symbol/Go/src/PastePlus/core/api/customEvents/customEvents.go:121 +0x480 fp=0x14000341fa0 sp=0x14000341d00 pc=0x1047ff530

Description

Before activating a new window, I first obtained the pid of the previous application. If pid is consistent with the pid of the current application, I will return pid=0. When I operate the current application, panic appears when activating the previous application, and I have judged Pid! =0 and also checked with pidexists, but in calling robotgo. ActivePid appeared panic ...