如何在 .IDL 文件中声明枚举?

How to declare an enum in an .IDL file?

我有一个要添加枚举的运行时类。我已经按照这里的 MSDN 文档的建议尝试了以下语法:https://docs.microsoft.com/en-ca/uwp/midl-3/intro

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

但是我从 MIDL 中收到以下错误:

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

正确的语法是什么?我正在使用 windows SDK 的 10.0.17763.0 版本。

您不能在类型中嵌套枚举。从 documentation 您链接到:

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.

由于枚举是类型,因此它们必须出现在命名空间中。您需要将您的 IDL 更改为:

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

    runtimeclass my_rt_class
    {        
    }
}