encapsulated_union

typedef [ [type-attribute-list] ]
    union
[ struct-name ] switch (switch-type switch-name) [ union-name ] {
        [ case (
limited-expression-list ) ]
            
[ [ field-attribute-list ] ] type-specifier declarator-list ;
        ...
    }

type-attribute-list
Specifies zero or more attributes that apply to the union type. Valid type attributes include handle, transmit_as; the pointer attribute ref, unique, or ptr; and the usage attributes context_handle and ignore. Separate multiple attributes with commas.
struct-name
Specifies an optional tag that names the structure generated by the MIDL compiler.
switch-type
Specifies an int, char, enum type, or an identifier that resolves to one of these types.
switch-name
Specifies the name of the variable of type switch-type that acts as the union discriminant.
union-name
Specifies an optional identifier that names the union in the structure, generated by the MIDL compiler, that contains the union and the discriminant.
limited-expression-list
Specifies one or more C-language expressions. The MIDL compiler supports conditional expressions, logical expressions, relational expressions, and arithmetic expressions. MIDL does not allow function invocations in expressions and does not allow increment and decrement operators.
field-attribute-list
Specifies zero or more field attributes that apply to the union member. Valid field attributes include first_is, last_is, length_is, max_is, size_is; the usage attributes string, ignore, and context_handle; the pointer attribute unique or ptr; and, for members that are tnonencapsulated unions, the union attribute switch_type. Separate multiple field attributes with commas.
type-specifier
Specifies a base_type, struct, union, enum type, or type identifier. An optional storage specification can precede type-specifier.
declarator-list
One or more standard C declarators, such as identifiers, pointer declarators, and array declarators. (Function declarators and bit-field declarations are not allowed in unions that are transmitted in remote procedure calls. Except when you use the MIDL compiler switch /osf, these declarators are allowed in unions that are not transmitted.) Separate multiple declarators with commas.

Examples

typedef union _S1_TYPE switch (long l1) U1_TYPE { 
    case 1024: 
        float f1; 
    case 2048: 
        double d2; 
} S1_TYPE; 
 
/* in generated header file */ 
typedef struct _S1_TYPE { 
    long l1; 
    union { 
        float f1; 
        double d2; 
    } U1_TYPE; 
} S1_TYPE; 
 

Remarks

The encapsulated union is indicated by the presence of the switch keyword. This type of union is so named because the MIDL compiler automatically encapsulates the union and its discriminant in a structure for transmission during a remote procedure call.

If the union tag is missing (U1_TYPE in the example above), the compiler will generate the structure with the union field named tagged_union.

The shape of unions must be the same across platforms to ensure interconnectivity.

See Also

IDL, ms_union, non-encapsulated_union, switch_is, switch_type, union