Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Enum in a C++ Library

Tags:

c++

c#

enums

com

I'm having issues using a public enum defined in C# within a C++ Interface. The .NET project is exposed to COM to be used within C++ and VB legacy software.

C# Code:

namespace ACME.XXX.XXX.XXX.Interfaces.Object
{
   [Guid(".....")]
   [InterfaceType(ComInterfaceType.InterfaceIsDual)]
   [ComVisible(true)]
   public interface TestInterface
   {
       void Stub();
   }

   [ComVisible(true)]
   public enum TestEnum
   {
        a = 1,
        b = 2
   }
}

C++ Code:

Edit: In the idl for the project I imported the tlb. (importlib("\..\ACME.XXX.XXX.XXX.Interfaces.tlb"))

interface ITestObject : IDispatch
{
  [id(1), helpstring("one")]
  HRESULT MethodOne([in] TestInterface *a);

  [id(2), helpstring("two")]     
  HRESULT MethodTwo([in] TestEnum a);
}

In MethodTwo, I keep getting errors stating

Excepting Type Specification near TestEnum

I'm assuming there's something I'm not doing correctly. MethodOne seems to be finding the reference correctly.

Is there some magic to referencing .NET enum object in C++ interface?

like image 634
Steve Borgra Avatar asked Mar 23 '16 14:03

Steve Borgra


1 Answers

Enums are a fairly quirky, the type library that you got from your C# project does not have a typedef for TestEnum. You can write it like this instead:

  [id(2), helpstring("two")]     
  HRESULT MethodTwo([in] enum TestEnum a);

Note the added enum keyword. Or you can declare your own typedef if you use the identifier in multiple places or need it in your C++ code, put it before your interface declaration:

  typedef enum TestEnum TestEnum;

You probably prefer the latter.

like image 117
Hans Passant Avatar answered Sep 24 '22 06:09

Hans Passant