From c4926c6e9f0bfbde05216c297208c5b245b2ee1a Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Wed, 11 Feb 2026 11:26:40 -0500 Subject: [PATCH] refactor: migrate to .NET 8 SDK-style project format Convert from .NET Framework 4.8 legacy csproj to .NET 8 SDK-style. Add ComHelper class for COM Running Object Table access, replacing Marshal.GetActiveObject which is not available in .NET Core. Add EF Core and System.Configuration.ConfigurationManager packages. Co-Authored-By: Claude Opus 4.6 --- ExportDXF/ExportDXF.csproj | 258 ++++-------------------- ExportDXF/Services/SolidWorksService.cs | 61 +++++- 2 files changed, 95 insertions(+), 224 deletions(-) diff --git a/ExportDXF/ExportDXF.csproj b/ExportDXF/ExportDXF.csproj index 80c955b..4a45d38 100644 --- a/ExportDXF/ExportDXF.csproj +++ b/ExportDXF/ExportDXF.csproj @@ -1,235 +1,47 @@ - - - + + - Debug - AnyCPU - {05F21D73-FD31-4E77-8D9B-41C86D4D8305} + net8.0-windows WinExe - Properties + true + disable + disable ExportDXF ExportDXF - v4.8 - 512 - false - - \\REMCOSRV0\Data\Software\ExportDXF\ - true - Unc - true - Foreground - 7 - Days - false - false - true - Rogers Engineering - true - publish.htm - 8 - 1.6.0.%2a - false - true - true - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - false - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - false - - - 34BB4CCEF0A2D6409091A3AC44083A6F09D1DF82 - - - ExportDXF_TemporaryKey.pfx - - - true - - - false - - - true + + + false + - - - - - - False - False - C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\api\redist\SolidWorks.Interop.sldworks.dll - - - False - False - C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\api\redist\SolidWorks.Interop.swconst.dll - - - - - - - - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\api\redist\SolidWorks.Interop.sldworks.dll + False + + + C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\api\redist\SolidWorks.Interop.swconst.dll + False + + + + + + + - - - - - - - - - - - - Form - - - DrawingSelectionForm.cs - - - Form - - - MainForm.cs - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - DrawingSelectionForm.cs - - - MainForm.cs - - - ResXFileCodeGenerator - Resources.Designer.cs - Designer - - - True - Resources.resx - True - - - - SettingsSingleFileGenerator - Settings.Designer.cs - - - True - Settings.settings - True - Always - - - - - - - - - - - - False - Microsoft .NET Framework 4 %28x86 and x64%29 - true - - - False - .NET Framework 3.5 SP1 - false - - - False - Windows Installer 4.5 - true - - - - - {229c2fb9-6ad6-4a5d-b83a-d1146573d6f9} - EtchBendLines - - - - - {05BFD3F1-6319-4F30-B752-C7A22889BCC4} - 1 - 0 - 0 - tlbimp - False - True - - - {05BFD3F1-6319-4F30-B752-C7A22889BCC4} - 1 - 0 - 0 - aximp - False - - - - - \ No newline at end of file + + + diff --git a/ExportDXF/Services/SolidWorksService.cs b/ExportDXF/Services/SolidWorksService.cs index 0fde4b9..6d736f4 100644 --- a/ExportDXF/Services/SolidWorksService.cs +++ b/ExportDXF/Services/SolidWorksService.cs @@ -3,10 +3,69 @@ using SolidWorks.Interop.sldworks; using SolidWorks.Interop.swconst; using System; using System.Runtime.InteropServices; +using System.Runtime.InteropServices.ComTypes; using System.Threading.Tasks; namespace ExportDXF.Services { + /// + /// Helper class to get COM objects from Running Object Table (ROT) in .NET Core/5+ + /// + internal static class ComHelper + { + [DllImport("ole32.dll")] + private static extern int GetRunningObjectTable(int reserved, out IRunningObjectTable rot); + + [DllImport("ole32.dll")] + private static extern int CreateBindCtx(int reserved, out IBindCtx bindCtx); + + public static object GetActiveObject(string progId) + { + IRunningObjectTable rot = null; + IEnumMoniker enumMoniker = null; + IBindCtx bindCtx = null; + + try + { + if (GetRunningObjectTable(0, out rot) != 0 || rot == null) + return null; + + rot.EnumRunning(out enumMoniker); + if (enumMoniker == null) + return null; + + if (CreateBindCtx(0, out bindCtx) != 0 || bindCtx == null) + return null; + + IMoniker[] monikers = new IMoniker[1]; + IntPtr fetched = IntPtr.Zero; + + while (enumMoniker.Next(1, monikers, fetched) == 0) + { + monikers[0].GetDisplayName(bindCtx, null, out string displayName); + + if (displayName != null && displayName.IndexOf(progId, StringComparison.OrdinalIgnoreCase) >= 0) + { + rot.GetObject(monikers[0], out object obj); + return obj; + } + } + + return null; + } + catch + { + return null; + } + finally + { + if (enumMoniker != null) Marshal.ReleaseComObject(enumMoniker); + if (rot != null) Marshal.ReleaseComObject(rot); + if (bindCtx != null) Marshal.ReleaseComObject(bindCtx); + } + } + } + /// /// Service for managing SolidWorks application connection and document lifecycle. /// @@ -245,7 +304,7 @@ namespace ExportDXF.Services { try { - return Marshal.GetActiveObject("SldWorks.Application") as SldWorks; + return ComHelper.GetActiveObject("SldWorks.Application") as SldWorks; } catch (COMException) {