Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Class Not registered 80041054 on class that is registered

Tags:

c++

c#

com

I'm calling CoCreateInstance from unmanaged code onto a managed class that is registered (the HKEY_CLASSES_ROOT\CLSID{xxxxx-xxxxxx-xxxxxx-xxxxx-xxxxxx-xxxxx} registry entry exists and the file is correctly loaded from another program.

The code is below:

    HRESULT hRC;
    CoInitialize(NULL);

    char* sUTProgID = "My.Utilities";

    CLSID UTClassID;
    hRC = CLSIDFromProgID(
        _CW(sUTProgID),             // Pointer to the ProgID
        &UTClassID );               // Pointer to the CLSID
    if ( S_OK != hRC )
    {
        DOTRACE((_T("    CLSIDFromProgID error 0x%X\n", hRC)));
    }
    IUnknown* pUnknown;
    hRC = CoCreateInstance(
    UTClassID,                  // Class identifier (CLSID) of the object
        0,                          // Pointer to whether object is or isn't part of an aggregate
        CLSCTX_ALL,                 // Context for running executable code
        IID_IUnknown,               // Reference to the identifier of the interface
        (void**) &pUnknown);        // Address of output variable that receives the interface pointer requested in riid
    if ( S_OK != hRC )
    {
    //code makes it here with an 80040145 class not registered error
    }

The same code works for the other application. The code is compiled for x86 and is running on an x86 machine.

EDIT: It's a windows XP machine so I assume UAC is out. I've logged the ClassID and it is indeed the correct one. I've also checked the ProcMon logs and it shows the registry key being accessed successfully and within the registry key the following paths are accessed: InProcServer32 - success InProcServerx86 - Name not found LocalServer32 - Name not found InProcHandler32 - Name not found AppId - Name not found InProcServer32\ThreadingModel - success InProcServer32\1.0.0.0 - success InProcServer32\1.0.0.0\assembly - buffer overflow InProcServer32\1.0.0.0\assembly - success InProcServer32\1.0.0.0\class - success InProcServer32\1.0.0.0\RuntimeVersion - success InProcServer32\CodeBase - success (returns the file path)

It then checks the GAC cache, then a few directories before accessing the DLL.

CLIENT.EXE 1092 RegQueryKey HKCR\CLSID{...} SUCCESS Query: Name CLIENT.EXE 1092 RegOpenKey HKCR\CLSID{...}\InprocHandler NAME NOT FOUND Desired Access: Maximum Allowed CLIENT.EXE 1092 RegCloseKey HKCR\CLSID{9935FEE6-39FD-4EF0-87DB-8372B0992610} SUCCESS CLIENT.EXE 1092 RegOpenKey HKLM\Software\Policies\Microsoft\Windows\App Management NAME NOT FOUND Desired Access: Query Value CLIENT.EXE 1092 CreateFile LogFile.txt

I think it as the last log message that the error is logged.

EDIT2: Dll is registered using the following code:

            Assembly asm = Assembly.LoadFile(dll_name);
            RegistrationServices regAsm = new RegistrationServices();
            bool bResult = regAsm.RegisterAssembly(asm,   AssemblyRegistrationFlags.SetCodeBase);

EDIT3: Running CorFlags.exe on (my) failing DLL outputs:

 Version   : v2.0.50727
 CLR Header: 2.5
 PE        : PE32
 CorFlags  : 11
 ILONLY    : 1
 32BIT     : 1
 Signed    : 1

Running the same on the EXE calling the DLL outputs:

 Version   : v1.1.4322
 CLR Header: 2.0
 PE        : PE32
 CorFlags  : 9
 ILONLY    : 1
 32BIT     : 0
 Signed    : 1
like image 504
probably at the beach Avatar asked Feb 07 '14 06:02

probably at the beach


1 Answers

Running the same on the EXE...

You have a CLR version mismatch problem. The EXE targets .NET 1.1 and will load the v1.1 version of the CLR. That version cannot load the DLL, it targets the v2 version of the CLR, the one used in .NET 2.0 through 3.5

A workaround is to write a .config file that forces the right version of the CLR to be used. Put it in the same directory as the .exe with the name somefile.exe.config, replace "somefile" with the name of the .exe:

<configuration>
   <startup>
      <supportedRuntime version="v2.0.50757"/>
   </startup>
</configuration>
like image 75
Hans Passant Avatar answered Nov 08 '22 03:11

Hans Passant