13

NOTE: I am answering my own question in the event it helps others in the future.

I'm getting the error:

The assembly "C:\XYZ.dll" could not be converted to a type library. Type library exporter encountered an error while processing 'XYZ'. Error: Element not found.

Here is the code that causes the problem:

[Guid("7a4e9867-96a7-43f0-9492-0327b9053853"),
ClassInterface(ClassInterfaceType.None)]
public class TimeSeriesPoint
{
    public string Date { get; set; }
    public float Value { get; set; }
}

[Guid("7a4e9867-96a7-43f0-9492-0327b9053853"),
InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IDataHelper
{
    //RCOMServerLib.IStatConnector Connector { set; }
    string Text { set; }
    void DoCallback();
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321

2 Answers2

23

I was using the same GUID from the AssemblyInfo file:

[assembly: Guid("7a4e9867-96a7-43f0-9492-0327b9053853")]

You need to use unique GUIDs to resolve the error:

[Guid("C25D485B-F7DE-4F1C-99FE-FFAF5A219B77"),
ClassInterface(ClassInterfaceType.None)]
public class TimeSeriesPoint
{
    public string Date { get; set; }
    public float Value { get; set; }
}

[Guid("FA6F70DD-CDD0-4FF3-94BA-E2B94E68321D"),
InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IDataHelper
{
    //RCOMServerLib.IStatConnector Connector { set; }
    string Text { set; }
    void DoCallback();

To get unique GUIDs in Visual Studio click Tools Menu > Create GUID > select the 4th option Registry Format > Copy:

enter image description here

Ref: http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/a440f695-652c-46d2-bb52-650c6227d3e9

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
2

Similar problem, but with the final error statement:

Error: Error loading type library/DLL.

In my case, there was a referenced project/assembly that didn't have its tlb generated.

However, running regasm manually worked. It generated the tlb for both the referenced project/assembly and the target, Acme.Widgets.dll. And there were no explicit references to the related project specified on the regasm command line:

@ECHO OFF

pushd "%~dp0"

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\regasm Acme.Widgets.dll /tlb Acme.Widgets.tlb /codebase 

popd

pause

I eventually realized the referenced project/assembly didn't have the Register for COM interop setting enabled within Visual Studio. Figured it was enough to have it enabled for the target only, but that wasn't the case.

The referenced project/assembly began its life serving an ordinary .Net app where COM wasn't a factor.

bvj
  • 3,294
  • 31
  • 30
  • 1
    Nice one, see comments in this answer, I think **Reg for Com Interop** should be more obvious when it's explicitly needed: https://stackoverflow.com/a/52050257/495455 – Jeremy Thompson Oct 13 '19 at 01:17