7

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.