private bool IsOrphaned(Process process) { try { var mainHandle = process.MainWindowHandle; if (mainHandle.ToInt32() > 0) { var IID_IDispatch = RunningObjectTableService.IID_IDispatch; int res = OleAccWrapper.AccessibleObjectFromWindow(mainHandle, OBJID_NATIVEOM, ref IID_IDispatch, out Application app); return(res < 0); } } catch { } return(true); }
private RotApplicationModel GetApplicationFromProcess(Process process) { var mainHandle = process.MainWindowHandle; if (mainHandle.ToInt32() > 0) { var IID_IDispatch = RunningObjectTableService.IID_IDispatch; int res = OleAccWrapper.AccessibleObjectFromWindow(mainHandle, OBJID_NATIVEOM, ref IID_IDispatch, out Application app); if (res >= 0) { return(new RotApplicationModel() { Name = app.Name, Application = app, ApplicationMainHwnd = app.hWndAccessApp(), ProcessId = process.Id }); } throw new Exception($"AccessibleObjectFromWindow returned false for hwnd {mainHandle}"); } throw new Exception($"Process {process.ProcessName} id {process.Id} does not have main window"); }