9 Replies Latest reply on Sep 20, 2018 12:40 PM by Jim Sculley

    Retrieving COM class factory failed error: 80040154

    Michael Kudla

      Hi there, thank you for reading.

       

      I am working on developing a piece of code which executes within and accesses data from a commercial radiation planning software (Eclipse). I'm attempting to have this script also update some model parameters within a SolidWorks model. When attempting to open the SolidWorks application (SldWorks swApp = new SldWorks(); ) i get an error: "Retrieving the COM class factory for component with CSLID {xxx-xxx-xxx} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))."

       

      I understand that this means that the application is attempting something in the registry that isn't there, but it isn't clear what I should do to remedy this.

       

      I am running SW 2018 in Windows 7, using Visual Studio Community 2017 as my editor.

      The piece of code works on its own, and I believe I have all of the relevant Solidworks .dll's references. I have no build errors in my solution.

      I apologize if there is anything confusing in this, as I am new to C# and the Solidworks API. I will do my best to clarify any questions.

       

      Any help is greatly appreciated!

       

      Thank you!

        • Re: Retrieving COM class factory failed error: 80040154
          Artem Taturevych
          • Make sure that your project is not built for x86. It should be either AnyCPU or x64.
          • Uncheck 'Prefer 32 bit' in project options if you are building .exe application
          • Set Embed Interop Types option to false for all SOLIDWORKS type libraries

           

          Check this article for more details: Connect to SOLIDWORKS from Stand-Alone Application

           

          If none of the above helps you might have your type libraries not registered correctly (this might be due to some components installed-uninstalled or some windows updates). The best option would be to repair SOLIDWORKS.

          • Re: Retrieving COM class factory failed error: 80040154
            Jim Sculley

            Try something simpler.  Create a new Visual Studio Console Application project.  Add the 'SldWorks 2018 Type Library' and the 'SolidWorks 2018 Constant type library' as project References.  Create the following code:

             

            using System;
            using SldWorks;
            using SwConst;
            namespace SWTest
            {
                class SWTest
                {
                    static void Main(string[] args)
                    {
                        SldWorks.SldWorks swApp;
                        swApp = new SldWorks.SldWorks();
                        swApp.SendMsgToUser2("It worked", (int)swMessageBoxIcon_e.swMbInformation, (int)swMessageBoxBtn_e.swMbOk);
                        swApp.ExitApp();
                        swApp = null;   
                    }
                }
            }
            

             

            Build and Run.  SOLIDWORKS should start up in the background (i.e. invisible) and eventually a message box will be shown.  If this works, your SW API installation is not broken and you should look elsewhere for the cause.  My next step would probably be to make sure I can run a simple non-SW application using simple Windows Form stuff such as MessageBox::Show().

             

            You may have a conflict between your Eclipse software and SW where Eclipse is loading a DLL and SW later tries to load a different version of the same DLL.

             

            Finally, a word of caution.  Visual Studio Community has a pretty restrictive license.  Make sure you are legally allowed to use it for developing your code.

              • Re: Retrieving COM class factory failed error: 80040154
                Michael Kudla

                Thanks for the response Jim! For me, attempting the method you described actually does work. I can build a Console application and open solidworks, create a model, etc.

                 

                The way this script runs is:

                A "Launcher.cs" script is run from within Eclipse, which runs the main file:

                 

                //Launcher.cs

                using System;

                using System.Linq;

                using System.Text;

                using System.Windows;

                using System.Collections.Generic;

                using VMS.TPS.Common.Model.API;

                using VMS.TPS.Common.Model.Types;

                using System.Reflection;

                namespace VMS.TPS

                {

                    public class Script

                    {

                        public Script()

                        {

                        }

                        ScriptContext originalcontext;

                        public void Execute(ScriptContext context)

                        {

                            this.originalcontext = context;

                            var assemblypath = @" the location of the .exe here, same folder as Launcher.cs";

                            var assem = Assembly.UnsafeLoadFrom(assemblypath);

                            var script = Activator.CreateInstanceFrom(assemblypath, "WPFTemplate.XScript").Unwrap();

                            var type = script.GetType();

                            type.InvokeMember("Execute",

                                BindingFlags.Default | BindingFlags.InvokeMethod,

                                null,

                                script,

                                new object[] { this.originalcontext });

                        }

                    }

                }

                //End of Launcher.cs

                 

                The calls to open solidworks are within this XScript method, and i've tried all the methods suggested in these two places:

                Create C# Stand-Alone Application for SOLIDWORKS

                C#: DLL is registered but COM error 80040154 still appear - Stack Overflow

                And still no solution.

                 

                To your second suggestion:

                "You may have a conflict between your Eclipse software and SW where Eclipse is loading a DLL and SW later tries to load a different version of the same DLL."

                You may be right about this. How could I check?

                 

                To your last point about licenses: This is purely an academic research project, well within the guidelines, as far as my understanding goes. I appreciate pointing that out though.

                 

                Any further ideas?

                Thanks

                  • Re: Retrieving COM class factory failed error: 80040154
                    Jim Sculley

                    Michael Kudla wrote:

                     

                    Thanks for the response Jim! For me, attempting the method you described actually does work. I can build a Console application and open solidworks, create a model, etc.

                     

                    The way this script runs is:

                    A "Launcher.cs" script is run from within Eclipse, which runs the main file:

                     

                    //Launcher.cs

                    using System;

                    using System.Linq;

                    using System.Text;

                    using System.Windows;

                    using System.Collections.Generic;

                    using VMS.TPS.Common.Model.API;

                    using VMS.TPS.Common.Model.Types;

                    using System.Reflection;

                    namespace VMS.TPS

                    {

                    public class Script

                    {

                    public Script()

                    {

                    }

                    ScriptContext originalcontext;

                    public void Execute(ScriptContext context)

                    {

                    this.originalcontext = context;

                    var assemblypath = @" the location of the .exe here, same folder as Launcher.cs";

                    var assem = Assembly.UnsafeLoadFrom(assemblypath);

                    var script = Activator.CreateInstanceFrom(assemblypath, "WPFTemplate.XScript").Unwrap();

                    var type = script.GetType();

                    type.InvokeMember("Execute",

                    BindingFlags.Default | BindingFlags.InvokeMethod,

                    null,

                    script,

                    new object[] { this.originalcontext });

                    }

                    }

                    }

                    //End of Launcher.cs

                     

                    The calls to open solidworks are within this XScript method, and i've tried all the methods suggested in these two places:

                    Create C# Stand-Alone Application for SOLIDWORKS

                    C#: DLL is registered but COM error 80040154 still appear - Stack Overflow

                    And still no solution.

                     

                    To your second suggestion:

                    "You may have a conflict between your Eclipse software and SW where Eclipse is loading a DLL and SW later tries to load a different version of the same DLL."

                    You may be right about this. How could I check?

                     

                     

                    What is the actual CSLID from the error message?  If you search the Windows Registry for that CLSID under HKEY_CLASSES_ROOT you may find multiple entries under the InprocSvr32 subkey pointing to different versions of a DLL. 

                      • Re: Retrieving COM class factory failed error: 80040154
                        Michael Kudla

                        Thanks again, Jim!

                        Per your suggestion I searched the registry for the error CSLID: "13551c8b-f74d-493e-b550-489532b7a905". I searched the entire Registry Editor. Within HKEY_CLASSES_ROOT\CSLID directory I find the following folder:

                         

                        The InprocHandler32 Data is ole32.dll, the LocalServer32 Data is the C: directory location for SLDWORKS.exe, the ProgID Data is SldWorks.Application.26, TypeLib looks like another GUID, and the VersionIndependentProgID Data is SldWorks.Application. For each of these folders, the Type is REG_SZ.

                         

                        Further, the only other results are the within HKEY_LOCAL_MACHINE and are HKEY_LOCAL_MACHINE\SldWorks.Application and HKEY_LOCAL_MACHINE\SldWorks.Application.26:

                         

                        From the above image, the Data value of shell is "(value not set)". For both of these, the CSLID is the same as the one I searched, and for all of these folders, the Type is REG_SZ.

                         

                        Does this give you a better idea?

                        Thanks again!

                          • Re: Retrieving COM class factory failed error: 80040154
                            Jim Sculley

                            Michael Kudla wrote:

                             

                            Thanks again, Jim!

                            Per your suggestion I searched the registry for the error CSLID: "13551c8b-f74d-493e-b550-489532b7a905". I searched the entire Registry Editor. Within HKEY_CLASSES_ROOT\CSLID directory I find the following folder:

                             

                            The InprocHandler32 Data is ole32.dll, the LocalServer32 Data is the C: directory location for SLDWORKS.exe, the ProgID Data is SldWorks.Application.26, TypeLib looks like another GUID, and the VersionIndependentProgID Data is SldWorks.Application. For each of these folders, the Type is REG_SZ.

                             

                            Further, the only other results are the within HKEY_LOCAL_MACHINE and are HKEY_LOCAL_MACHINE\SldWorks.Application and HKEY_LOCAL_MACHINE\SldWorks.Application.26:

                             

                            From the above image, the Data value of shell is "(value not set)". For both of these, the CSLID is the same as the one I searched, and for all of these folders, the Type is REG_SZ.

                             

                            Does this give you a better idea?

                            Thanks again!

                            Unfortunately, this doesn't indicate a problem.

                             

                            Are you creating a single file plugin (that Eclipse compiles on the fly), a binary plugin or a standalone application?

                              • Re: Retrieving COM class factory failed error: 80040154
                                Michael Kudla

                                Basically, there is a "Launcher.cs" that is run from within Eclipse. Eclipse has a method for launching .cs files from within the program, provided that the eclipse scripting namespace is used (VMS.TPS). It runs whatever is within the Execute method, in the Script Class.

                                 

                                As you may have seen above, in this case, the program uses UnsafeLoadFrom and CreateInstanceFrom and InvokeMember to load and run the XScript method in my WPFTemplate.exe file which does all the extracting and writing to various places, including Excel. It gets hung up at creating, or even editing an instance of Solidworks.

                                 

                                I guess this is technically a single file plugin which then executes a binary.

                                 

                                I had a thought last night that instead of attempting to execute SW from within this file, that maybe I should be attempting to simply open another Console Application for example. I could pass the relevant variables to the console application via read/writing a .txt file.

                                 

                                Any other ideas before I throw my computer off the roof?

                                  • Re: Retrieving COM class factory failed error: 80040154
                                    Jim Sculley

                                    Michael Kudla wrote:

                                     

                                    Basically, there is a "Launcher.cs" that is run from within Eclipse. Eclipse has a method for launching .cs files from within the program, provided that the eclipse scripting namespace is used (VMS.TPS). It runs whatever is within the Execute method, in the Script Class.

                                     

                                    As you may have seen above, in this case, the program uses UnsafeLoadFrom and CreateInstanceFrom and InvokeMember to load and run the XScript method in my WPFTemplate.exe file which does all the extracting and writing to various places, including Excel. It gets hung up at creating, or even editing an instance of Solidworks.

                                     

                                    I guess this is technically a single file plugin which then executes a binary.

                                     

                                    I had a thought last night that instead of attempting to execute SW from within this file, that maybe I should be attempting to simply open another Console Application for example. I could pass the relevant variables to the console application via read/writing a .txt file.

                                     

                                    Any other ideas before I throw my computer off the roof?

                                    You have a lot of moving parts here.  Does everything else work correctly?  Does you WPFTemplate.exe executable successfully communicate with Eclipse when started via Activator.CreateInstanceFrom?  Have you tried starting or connecting to SOLIDWORKS from inside the Launcher.cs program instead, skipping the WPFTemplate portion altogether?  You need to isolate which moving part is failing.