diff --git a/robotgo.go b/robotgo.go index 9367c89..96cdf7b 100644 --- a/robotgo.go +++ b/robotgo.go @@ -777,3 +777,9 @@ func GetTitle() string { // Println("title...", gtittle) return gtittle } + +//GetPID Get the process id +func GetPID() int { + pid := C.aGetPID() + return int(pid) +} diff --git a/window/goWindow.h b/window/goWindow.h index fe0617b..4a7d3ca 100644 --- a/window/goWindow.h +++ b/window/goWindow.h @@ -57,3 +57,8 @@ char* aGetTitle(){ // printf("title::::%s\n",title ); return title; } + +int32 aGetPID(void){ + int pid=WGetPID(); + return pid; +} diff --git a/window/window.h b/window/window.h index 3e9da69..0c082b2 100644 --- a/window/window.h +++ b/window/window.h @@ -867,3 +867,39 @@ char *GetTitle(){ #endif } +int32 WGetPID(void){ + // Check window validity + if (!IsValid()) return 0; + +#if defined(IS_MACOSX) + + pid_t pid = 0; + // Attempt to retrieve the window pid + if (AXUIElementGetPid(mData.AxID, &pid) + == kAXErrorSuccess) return pid; + + return 0; + +#elif defined(USE_X11) + + // Ignore X errors + XDismissErrors(); + + // Get the window PID + long* result = (long*) + GetWindowProperty(mData, WM_PID,NULL); + + // Check result and convert it + if (result == NULL) return 0; + int32 pid = (int32) *result; + XFree (result); return pid; + +#elif defined(IS_WINDOWS) + + DWORD id = 0; + GetWindowThreadProcessId (mData.HWnd, &id); + return id; + +#endif +} +