beckhoff TE1000 TwinCAT 3 — 6.1.5 Reading the VS output and the error list

Samples | Visual Studio $asmInterop = [System.Reflection.Assembly]::LoadFrom($interopDll.FullName) # Compile a C# helper class at runtime using the loaded assemblies as references. # This is necessary because PowerShell uses COM late binding, which loses type information # and prevents a direct cast from DTE to DTE2. C# uses early binding and resolves # the cast correctly via the 'as' operator and QueryInterface under the hood.

Add-Type -ReferencedAssemblies @( $asmEnvDTE.Location, $asmEnvDTE80.Location, $asmInterop.Location ) -TypeDefinition @" using System; using System.Collections.Generic; using EnvDTE; using EnvDTE80; public class ErrorListReader { public static List GetErrors(object dteObject) { var results = new List (); // Cast the generic COM object to DTE2 using early binding. // This is the key step that fails in PowerShell but works correctly in C#. DTE2 dte2 = dteObject as DTE2; if (dte2 == null) return results; // Access the ErrorList via ToolWindows, which is only available on DTE2 ErrorList errorList = dte2.ToolWindows.ErrorList; ErrorItems items = errorList.ErrorItems; // Note:

COM collections are 1-indexed, not 0-indexed for (int i = 1; i { ErrorItem item = items.Item(i); results.Add(new string[] { item.ErrorLevel.ToString(), // 1 = Error, 2 = Warning, 3 = Message item.Description, item.FileName, item.Line.ToString(), item.Project }); } return results; } } "@ # Get a reference to the running TcXaeShell instance via the Running Object Table (ROT) $dte = [System.Runtime.InteropServices.Marshal]::GetActiveObject("TcXaeShell.DTE.17.0") # Call the C# method, passing the raw COM object $errors = [ErrorListReader]::GetErrors($dte) # Format and display the results as a PowerShell table if ($errors.Count -eq 0) { Write-Host "No errors found." } else { $errors | ForEach-Object { [PSCustomObject]@{ Level = $_[0] Description = $_[1] File = $_[2] Line = $_[3] Project = $_[4] } } | Format-Table -AutoSize } The late-binding issue only affects direct access to the ErrorItems collection via PowerShell.

In C#, dte2.ToolWindows.ErrorList.ErrorItems works without any problems. TE1000 191 Version: 1.7.0