/// <summary> /// Connect to E3.series process /// </summary> /// <param name="connectorSelectionDialog">Implementation of Selection Dialog /// (used in case of multiple running E3.series instances)</param> /// <returns>Wrapped e3Application COM object</returns> protected IApplication Connect(IConnectorSelectionDialog connectorSelectionDialog) { var comObject = GetProcess(connectorSelectionDialog); return(comObject != null ? new E3Application(comObject) : null); }
/// <summary> /// Get E3.series COM object /// </summary> /// <param name="connectorSelectionDialog">Selection dialog for multiple running instances of E3.series</param> /// <returns></returns> private static object GetProcess(IConnectorSelectionDialog connectorSelectionDialog = null) { var processes = Dispatcher.GetRunningE3SeriesApplications(); if (processes.Count == 0) { return(null); } if (processes.Count == 1) { return(processes.First().Value); } // If multiple instances of E3.series, try to connect to foreground process IntPtr hWnd = GetForegroundWindow(); int curPid; GetWindowThreadProcessId(hWnd, out curPid); if (Process.GetProcessById(curPid).ProcessName == Assembly.GetEntryAssembly().GetName().Name) { IntPtr targetHwnd = GetWindow(Process.GetCurrentProcess().MainWindowHandle, (uint)GetWindowCmd.GW_HWNDNEXT); while (true) { IntPtr temp = GetParent(targetHwnd); if (temp.Equals(IntPtr.Zero)) { break; } targetHwnd = temp; } GetWindowThreadProcessId(targetHwnd, out curPid); } if (Process.GetProcessById(curPid).ProcessName == "E3.series") { return(GetProcess(Process.GetProcessById(curPid).Id)); } // If connection to foreground window fails, // then show selection dialog (if exists) or throw exception if (connectorSelectionDialog == null) { throw new NotImplementedException( "Multiple running E3.series processes. Bring needed E3.series instance on top and restart application or implement connectorSelectionDialog in your application"); } object selectedProcess; var result = connectorSelectionDialog.ShowDialog(processes, out selectedProcess); return(result ? selectedProcess : null); }
public override IApplication Connect(IConnectorSelectionDialog connectorSelectionDialog) { return(base.Connect(connectorSelectionDialog)); }