C# class library export as COM, errors during MIDL compilation.

Robinson 156 Reputation points
2021-01-27T17:31:49.467+00:00

I'm trying to create a COM library from a C# library I have. In order to do this I've defined my C# class as follows:

[Guid("830D0BFA-8045-455B-AAB7-2A7D4A16455C")]    
[ComVisible(true)]
public interface IMyInterface
{
        uint Start();
}

[Guid("22B91F20-1CC3-4276-BB51-0AE75D7DA5BB")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class MyInterface_Impl : IMyInterface, IDisposable
{        
    public uint Start()
    {
        return 0;
    }
}

Additionally, I've enabled "Register for COM interop" in the project's build properties. The next step to allow a Win32 app (actually an MFC app) to use it is I think to generate a .tlb for the assembly. I'm doing this with the following post-build command:

"$(FrameworkSdkDir)\bin\NETFX 4.8 Tools\tlbexp.exe" "$(TargetFileName)" "$(TargetName).tlb" /win32

I see the .tlb in the build folder and then proceed to add it to the MFC project, right clicking it and setting it to Item Type: MIDL tool. When I then build this project, I get the following warnings and errors:

1>------ Build started: Project: My_Dll_Test_MFC, Configuration: Debug Win32 ------
1>Processing ..\My_Dll\bin\x86\Debug\My_Dll.tlb
1>My_Dll.tlb
1>..\My_Dll\bin\x86\Debug\My_Dll.tlb(1): warning C4335: Mac file format detected: please convert the source file to either DOS or UNIX format
1>..\My_Dll\bin\x86\Debug\My_Dll.tlb(1): error MIDL2025: syntax error : expecting an interface name or DispatchInterfaceName or CoclassName or ModuleName or LibraryName or ContractName or a type specification near "MSFT"
1>..\My_Dll\bin\x86\Debug\My_Dll.tlb(1): error MIDL2026: cannot recover from earlier syntax errors; aborting compilation
1>Done building project "My_Dll_Test_MFC.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

What mistake have I made here?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,526 questions
0 comments No comments
{count} votes

Accepted answer
  1. Robinson 156 Reputation points
    2021-01-27T18:31:41.48+00:00

    Going to answer my own question here. The mistake is you don't do that with the tlb, only the idl. The way to import it is with the #import directive, which seems to build just fine.

    0 comments No comments

0 additional answers

Sort by: Most helpful