beckhoff TE1000 TwinCAT 3 — 6.3.5 Connecting to the Automation Interface via DTE

Samples | System through all entries in the ROT and checks each DTE entry for its solution path. This is necessary when multiple identical hosts – such as several TcXaeShell.DTE.17.0 – are registered at the same time and you need to ensure that the correct instance is accessed. The three methods are interdependent:

using EnvDTE; using System.Collections; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; internal static class DteHelper { [DllImport("ole32.dll")] private static extern int GetRunningObjectTable(uint reserved, out IRunningObjectTable pprot); [DllImport("ole32.dll")] private static extern int CreateBindCtx(uint reserved, out IBindCtx ppbc); public static Hashtable GetRunningObjectTable() { Hashtable result = new Hashtable(); GetRunningObjectTable(0, out IRunningObjectTable rot); rot.EnumRunning(out IEnumMoniker enumMoniker); IMoniker[] monikers = new IMoniker[1]; while (enumMoniker.Next(1, monikers, IntPtr.Zero) == 0) { CreateBindCtx(0, out IBindCtx ctx); monikers[0].GetDisplayName(ctx, null, out string name); rot.GetObject(monikers[0], out object obj); result[name] = obj; } return result; } public static Hashtable GetIDEInstances(bool openSolutionsOnly, string progId) { Hashtable result = new Hashtable(); Hashtable runningObjects = GetRunningObjectTable(); IDictionaryEnumerator enumerator = runningObjects.GetEnumerator(); while (enumerator.MoveNext()) { string candidateName = (string)enumerator.Key; if (!candidateName.StartsWith("!" + progId)) continue; DTE ide = enumerator.Value as DTE; if (ide == null) continue; if (openSolutionsOnly) { try { string solutionFile = ide.Solution.FullName; if (solutionFile != string.Empty) result[candidateName] = ide; } catch { } TE1000 205 Version:

1.7.0 Method Description GetRunningObjectTable() Query the Running Object Table (ROT) for a snapshot of all running processes. Returns all found processes in a Hashtable. GetIDEInstances() Search for DTE instances in the ROT snapshot. The query for candidateName can be customized to a more specific ProgId, such as "VisualStudio.DTE.17.0" for Visual Studio 2022 instances. attachToExistingDte() Uses GetIDEInstances() to select a DTE instance based on its solution path and attaches a new DTE object to that instance.