public static IEnumerable <object> GetRunningObjects(string namePattern = ".*") { IEnumMoniker enumMoniker = null; IRunningObjectTable rot = null; IBindCtx bindCtx = null; try { Marshal.ThrowExceptionForHR(Ole32.CreateBindCtx(0, out bindCtx)); bindCtx.GetRunningObjectTable(out rot); rot.EnumRunning(out enumMoniker); var moniker = new IMoniker[1]; var fetched = IntPtr.Zero; while (enumMoniker.Next(1, moniker, fetched) == S_OK) { object runningObject = null; try { var roMoniker = moniker.First(); if (roMoniker == null) { continue; } roMoniker.GetDisplayName(bindCtx, null, out var name); if (!Regex.IsMatch(name, namePattern)) { continue; } Marshal.ThrowExceptionForHR(rot.GetObject(roMoniker, out runningObject)); } catch (UnauthorizedAccessException) { continue; } if (runningObject != null) { yield return(runningObject); } } } finally { if (enumMoniker != null) { Marshal.ReleaseComObject(enumMoniker); } if (rot != null) { Marshal.ReleaseComObject(rot); } if (bindCtx != null) { Marshal.ReleaseComObject(bindCtx); } } }
private static ISldWorks GetSwAppFromProcess(int processId) { var monikerName = "SolidWorks_PID_" + processId.ToString(); IBindCtx context = null; IRunningObjectTable rot = null; IEnumMoniker monikers = null; try { CreateBindCtx(0, out context); context.GetRunningObjectTable(out rot); rot.EnumRunning(out monikers); var moniker = new IMoniker[1]; while (monikers.Next(1, moniker, IntPtr.Zero) == 0) { var curMoniker = moniker.First(); string name = null; if (curMoniker != null) { try { curMoniker.GetDisplayName(context, null, out name); } catch (UnauthorizedAccessException) { } } if (string.Equals(monikerName, name, StringComparison.CurrentCultureIgnoreCase)) { object app; rot.GetObject(curMoniker, out app); return(app as ISldWorks); } } } finally { if (monikers != null) { Marshal.ReleaseComObject(monikers); } if (rot != null) { Marshal.ReleaseComObject(rot); } if (context != null) { Marshal.ReleaseComObject(context); } } return(null); }
private static void InitializeSolidWorks() { string monikerName = "SolidWorks_PID_"; object app; IBindCtx context = null; IRunningObjectTable rot = null; IEnumMoniker monikers = null; try { CreateBindCtx(0, out context); context.GetRunningObjectTable(out rot); rot.EnumRunning(out monikers); IMoniker[] moniker = new IMoniker[1]; while (monikers.Next(1, moniker, IntPtr.Zero) == 0) { var curMoniker = moniker.First(); string name = null; if (curMoniker != null) { try { curMoniker.GetDisplayName(context, null, out name); } catch (UnauthorizedAccessException ex) { MessageObserver.Instance.SetMessage("Failed to get SolidWorks_PID." + "\t" + ex, MessageType.Error); System.Windows.Forms.MessageBox.Show(ex.Message); } } if (name.Contains(monikerName)) { rot.GetObject(curMoniker, out app); swApp = (SldWorks)app; swApp.Visible = true; swApp.DocumentVisible(false, 2); swApp.DocumentVisible(false, 1); return; } } string progId = "SldWorks.Application"; Type progType = Type.GetTypeFromProgID(progId); app = Activator.CreateInstance(progType) as SldWorks; swApp = (SldWorks)app; swApp.Visible = true; //swApp.DocumentVisible(false, 2); //swApp.DocumentVisible(false, 1); return; } finally { if (monikers != null) { Marshal.ReleaseComObject(monikers); } if (rot != null) { Marshal.ReleaseComObject(rot); } if (context != null) { Marshal.ReleaseComObject(context); } } }
public static TComObj TryGetComObjectByMonikerName <TComObj>(string monikerName, IXLogger logger = null) { IBindCtx context = null; IRunningObjectTable rot = null; IEnumMoniker monikers = null; try { CreateBindCtx(0, out context); context.GetRunningObjectTable(out rot); rot.EnumRunning(out monikers); var moniker = new IMoniker[1]; while (monikers.Next(1, moniker, IntPtr.Zero) == 0) { var curMoniker = moniker.First(); string name = null; if (curMoniker != null) { try { curMoniker.GetDisplayName(context, null, out name); } catch (UnauthorizedAccessException ex) { logger?.Log(ex); } } if (string.Equals(monikerName, name, StringComparison.CurrentCultureIgnoreCase)) { object app; rot.GetObject(curMoniker, out app); return((TComObj)app); } } } catch (Exception ex) { logger?.Log(ex); throw; } finally { if (monikers != null) { while (Marshal.ReleaseComObject(monikers) > 0) { ; } } if (rot != null) { while (Marshal.ReleaseComObject(rot) > 0) { ; } } if (context != null) { while (Marshal.ReleaseComObject(context) > 0) { ; } } } return(default);
private T FindObjectByMonikerName <T>(string monikerName) where T : class { IBindCtx context = null; IRunningObjectTable rot = null; IEnumMoniker monikers = null; try { CreateBindCtx(0, out context); context.GetRunningObjectTable(out rot); rot.EnumRunning(out monikers); var moniker = new IMoniker[1]; while (monikers.Next(1, moniker, IntPtr.Zero) == 0) { var curMoniker = moniker.First(); string name = null; if (curMoniker != null) { try { curMoniker.GetDisplayName(context, null, out name); } catch (UnauthorizedAccessException) { } } if (string.Equals(monikerName, name, StringComparison.CurrentCultureIgnoreCase)) { object app; rot.GetObject(curMoniker, out app); return((T)app); } } } finally { if (monikers != null) { Marshal.ReleaseComObject(monikers); } if (rot != null) { Marshal.ReleaseComObject(rot); } if (context != null) { Marshal.ReleaseComObject(context); } } return(null); }
/// <summary> /// 从 ROT 中获取 SolidWorks 程序 /// </summary> /// <param name="processId">进程 id</param> /// <returns>SldWorks</returns> private SldWorks GetSolidWorksFromProcess(int processId) { // 进程名字 string monikerName = "SolidWorks_PID_" + processId.ToString(); // 绑定上下文 IBindCtx context = null; // 运行时对象表 Running Object Table IRunningObjectTable rot = null; // 运行时对象名称集合 IEnumMoniker monikers = null; try { // 创建绑定上下文 CreateBindCtx(0, out context); // 获取运行时对象表 Running Object Table context.GetRunningObjectTable(out rot); // 列出运行时对象名称 rot.EnumRunning(out monikers); // IMoniker[] moniker = new IMoniker[1]; while (monikers.Next(1, moniker, IntPtr.Zero) == 0) { // 当前名字 IMoniker curMoniker = moniker.First(); // 显示名称 string name = null; if (curMoniker != null) { try { // 获取显示名称 curMoniker.GetDisplayName(context, null, out name); } catch (UnauthorizedAccessException) { } } if (string.Equals(monikerName, name, StringComparison.CurrentCultureIgnoreCase)) { // 从ROT获取对象并返回 rot.GetObject(curMoniker, out object app); SldWorks sldWorks = app as SldWorks; // 这一句不起作用 sldWorks.Visible = false; return(sldWorks); } } } finally { if (monikers != null) { Marshal.ReleaseComObject(monikers); } if (rot != null) { Marshal.ReleaseComObject(rot); } if (context != null) { Marshal.ReleaseComObject(context); } } return(null); }