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

女生的网名这么多〃 提交于 2019-12-24 19:23:53

问题


When I debug my Windows Form Application I get this error:

Retrieving the COM class factory for component with CLSID {27526253-6119-4B38-A1F9-2DC877E72334} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))

and because of it, my WFA can't interface with the software Solidworks installed on my computer; the only library that WFA needs to interface with Solidworks is SolidWorks.Interop.sldworks.dll (native position in the Solidworks directory C:\Program Files\SOLIDWORKS Corp 2017\SOLIDWORKS\SolidWorks.Interop.sldworks.dll).

Other informations below.

  • Compiler: Microsoft Visual C# 2010 Express
  • Operating system: Microsoft Windows 7 64 bit (updated)
  • Logged account: Administrator
  • Framework of destination of WFA: Microsoft .NET Framework 4.0 Client Profile
  • Architecture of WFA: x86 (32 bit)
  • Architecture of the library: x86 (32 bit)

When I added the library SolidWorks.Interop.sldworks.dll into my project references, through Visual Studio, there wasn't the CLSID

{27526253-6119-4B38-A1F9-2DC877E72334}

into the Windows registry, so I tried to register that library in these ways:

    • Pasted SolidWorks.Interop.sldworks.dll into directory C:\Windows\SysWOW64
    • Ran Command Prompt as Administrator, than typed C:\Windows\SysWOW64>regsvr32 C:\Windows\SysWOW64\SolidWorks.Interop.sldworks.dll
    • Pressed Enter
    • Read this message:

The module C:\Windows\SysWOW64\SolidWorks.Interop.sldworks.dll was loaded but the call to DllRegisterServer failed...

So, the library was not registered.

    • Pasted SolidWorks.Interop.sldworks.dll into the directory C:\Windows\Microsoft.NET\Framework\v4.0.30319
    • Ran Command Prompt as Administrator, than typed C:\Windows\Microsoft.NET\Framework\v4.0.30319>regasm SolidWorks.Interop.sldworks.dll
    • Pressed Enter
    • Read this message:

The types were registered.

So, I think, now the library is registered, in fact I see the CLSID

{27526253-6119-4B38-A1F9-2DC877E72334}

into the Windows registry.

The problem, however, persists.

  1. In my C# code I created a new Guid; this is the code:

    using System;
    using System.Diagnostics;
    using System.Windows.Forms;
    using SolidWorks.Interop.sldworks;
    
    namespace CreateModelSW
    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        //Create interface
        SldWorks swApp;
    
        private void buttonCreateModel_Click(object sender, EventArgs e)
        {
            //Kill Solidworks processes
            Process[] processes = Process.GetProcessesByName("SLDWORKS");
            foreach (Process process in processes)
            {
                process.CloseMainWindow();
                process.Kill();
            }
    
            //Create new GUID
            Guid myGuid1 = new Guid("27526253-6119-4B38-A1F9-2DC877E72334");
            object processSW = System.Activator.CreateInstance(System.Type.GetTypeFromCLSID(myGuid1));
    
            //Create new SOLIDWORKS Part
            swApp = (SldWorks)processSW;
            swApp.Visible = true;
            swApp.NewPart();
        }
    }
    }
    

The problem persists.

Can you please help me? Thanks.


回答1:


You should be using version agnostic progId, try this :

SldWorks swApp = (SldWorks)Activator.CreateInstance(System.Type.GetTypeFromProgID("SldWorks.Application"));


来源:https://stackoverflow.com/questions/44618287/c-dll-is-registered-but-com-error-80040154-still-appear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!