Search code examples
uwpwin-universal-appmidlc++-winrt

How to declare an enum in an .IDL file?


I have a runtimeclass that I would like to add an enum to. I have tried the following syntax as suggested by the MSDN documentation here: https://learn.microsoft.com/en-ca/uwp/midl-3/intro

namespace my_project
{
    runtimeclass my_rt_class
    {        
        enum my_enum
        {
            first = 0,
            second = 1
        };
    }
}

However I get the following error from MIDL:

error MIDL2025: [msg]syntax error [context]: expecting an identifier near ";"

Whats the correct syntax for this? I am using version 10.0.17763.0 of the windows SDK.


Solution

  • You cannot nest enumerations in types. From the documentation you linked to:

    The key organizational concepts in a MIDL 3.0 declaration are namespaces, types, and members. A MIDL 3.0 source file (an .idl file) contains at least one namespace, inside which are types and/or subordinate namespaces. Each type contains zero or more members.

    • Classes, interfaces, structures, and enumerations are types.
    • Fields, methods, properties, and events are examples of members.

    Since enumerations are types, they must appear in a namespace. You will need to change your IDL to this:

    namespace my_project
    {
        enum my_enum
        {
            first = 0,
            second = 1
        };
    
        runtimeclass my_rt_class
        {        
        }
    }