Question

I have an old .TLB file which is called 'GrpSvr.tlb', it contains a class called GrpCall. I have registered the .tlb on my Win7 x64 machine using regtlibv12.exe which worked correctly. I want to invoke the methods withing this library from C#, so first I tried:

Type objectType = System.Type.GetTypeFromProgID("GrpSvr.GrpCall"); 
dynamic thirdPartyDLLObject = System.Activator.CreateInstance(objectType);

but this returns null for objectType.

Question 1. can I invoke my .TLB file this way and what I am I doing wrong in this case?

Moving away from this method I then decided to follow

Import TLB into C#

which describes how to create a C# DLL using MSs Type Library Importer. I followed this and created GrouperServer.dll from GrpSrv.tlb using the command:

C:\Program Files (x86)\Microsoft Visual Studio 12.0>tlbimp F:\Groupers\DRGROUP\GrpSvr.tlb /out:C:\GrouperServer.dll /nam espace:GrouperServer Microsoft (R) .NET Framework Type Library to Assembly Converter 4.0.30319.33440 Copyright (C) Microsoft Corporation. All rights reserved.

TlbImp : Type library imported to C:\GrouperServer.dll

I then imported this DLL into my C# project (and set Embed interop Types = false following this answer) and attempted to invoke the class via

GrouperServer.GrpCallClass grouperServer = new GrouperServer.GrpCallClass();

but this does not work and at run-time I get the following error:

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in DrGroupIN.exe

Additional information: Retrieving the COM class factory for component with CLSID {FFB54BC4-B15E-11D1-99BC-0000E803C444} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

Following the advice given in

Retrieving the COM class factory for component with CLSID {XXXX} failed due to the following error: 80040154

I then set the project to target x86, but this does not help in this case.

Question 2: Why is this error being thrown and how can I resolve it?

Was it helpful?

Solution

Yeah, you are doing this wrong. A type library is not a COM server, using regtlibv12.exe is never necessary. You'll need to read the exception for what it is trying to tell you, as tens of thousands of Google hits do, the COM server is not registered.

A COM server is executable code, usually stored in a .dll. Sometimes an .ocx or an .exe. An installation step is required to register that file so that COM can find it back later when a program tries to create a COM object that the server implements. The Regsvr32.exe utility is a very common way to do that, but you should always use the installer provided by the vendor or author. Which ensures that the executable file(s) are copied onto your machine and writes the registry entries. Including the ones for the type library. The exception tells you that this wasn't done, COM cannot find the registry entry that tells it where the file is stored.

A .tlb file only describes the COM component, it tells a compiler what the declarations looks like. Helping the compiler to generate correct code to use the component and telling you when your code is wrong. It is metadata, very similar to the metadata that's present in a .NET assembly that describes the .NET types that are implemented by the assembly. It is usually embedded in the DLL or EXE as a resource, much like metadata is embedded in a .NET assembly. You can look at it with OleView.exe, File + View Typelib. Also used by Tlbimp.exe, a .NET utility that converts a type library to a .NET interop assembly, converting the declarations into a format that the CLR can easily understand.

Can't help you find the correct installer, it isn't anything standard.

OTHER TIPS

Put GrpSvr.Dll and its dependencies under the exe folder, and try again. The error is because it can not the find the GrpSvr.Dll based on the GUID in the registry.

Or you can search the registry with that GUID, you will find an entry which specify where the DLL is.

One tool to debug this kind error is the process monitor in the system internal kit, the log will give you a great detail on how the exe is searching registry and files, from the log, you should find the missing parts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top