public static ISwApplication FromProcess(Process process, IXServiceCollection services) { var app = RotHelper.TryGetComObjectByMonikerName <ISldWorks>(GetMonikerName(process)); if (app != null) { return(FromPointer(app, services)); } else { throw new Exception($"Cannot access SOLIDWORKS application at process {process.Id}"); } }
private void GetMacroParametersManager(bool createIfNotExist, out RotRegister newRegister, out IMacroParameterManager macroParamsMgr) { macroParamsMgr = RotHelper.TryGetComObjectByMonikerName <IMacroParameterManager>(MACRO_RUNNER_MONIKER_NAME); if (macroParamsMgr == null) { if (createIfNotExist) { macroParamsMgr = new MacroParameterManager(); newRegister = new RotRegister(macroParamsMgr, MACRO_RUNNER_MONIKER_NAME); } else { throw new COMException("Macro parameters manager is not registered"); } } else { newRegister = null; } }
internal ISldWorks Start(Action <Process> startHandler, CancellationToken cancellationToken) { SwVersion_e?vers = null; if (m_Version != null) { vers = m_Version.Major; } var args = new List <string>(); if (m_State.HasFlag(ApplicationState_e.Safe)) { args.Add(SwApplicationFactory.CommandLineArguments.SafeMode); } if (m_State.HasFlag(ApplicationState_e.Silent)) { args.Add(SwApplicationFactory.CommandLineArguments.SilentMode); } if (m_State.HasFlag(ApplicationState_e.Background)) { args.Add(SwApplicationFactory.CommandLineArguments.BackgroundMode); } var swPath = FindSwAppPath(vers); var prcInfo = new ProcessStartInfo(swPath, string.Join(" ", args)); if (m_State.HasFlag(ApplicationState_e.Hidden)) { prcInfo.UseShellExecute = false; prcInfo.CreateNoWindow = true; prcInfo.WindowStyle = ProcessWindowStyle.Hidden; } var prc = Process.Start(prcInfo); startHandler.Invoke(prc); try { ISldWorks app; do { if (cancellationToken.IsCancellationRequested) { throw new AppStartCancelledByUserException(); } app = RotHelper.TryGetComObjectByMonikerName <ISldWorks>(SwApplicationFactory.GetMonikerName(prc)); Thread.Sleep(100); }while (app == null); if (m_State.HasFlag(ApplicationState_e.Hidden)) { app.Visible = false; } return(app); } catch { if (prc != null) { try { prc.Kill(); } catch { } } throw; } }