默认指针类型

指针不需要具有显式属性说明。 如果未提供显式属性,则 MIDL 编译器使用默认指针属性。

未归因指针的默认情况如下:

  • 参数列表中出现的顶级指针默认为 [ref] 指针。
  • 所有其他指针默认为 [pointer_default] 属性指定的类型。 如果未提供 [pointer_default] 属性,则当 MIDL 编译器处于 Microsoft 扩展模式时,这些指针默认为 [unique] 属性;当 MIDL 编译器处于 DCE 兼容模式时,这些指针默认为 [ptr] 属性。

当远程过程返回指针时,返回值必须是 [ unique ] 或完整 ([ ptr ]) 指针。

/* IDL file compiled without /osf */
[ 
  uuid(ba209999-0c6c-11d2-97cf-00c04f8eea45),
  version(1.0),
  pointer_default(ptr)
]
interface MyInterface
{
    typedef long *PLONG;
  
    struct MyCircularList {
        struct MyCircularList *pRight;
        struct MyCircularList *pLeft;
        long Data;
    };

    void Foo1( [in] PLONG p );                   // p is ref
 
    void Foo2( [in] struct MyCircularList *p );  // p is ref, p->pRight and p->pLeft is ptr

    struct MyCircularList *Foo3( void );         // returned pointer is ptr.    
}

[ 
  uuid(ba209999-0c6c-11d2-97cf-00c04f8eea46),
  version(1.0)
]
interface MyInterface2
{
    struct MySingleList
       {
       struct MySingleList *pNext;
       long Data;
       };
    void Foo4( [in] struct MySingleList *p );  // p is ref, p->pNext is unique

    struct MySingleList *Foo5( void );         // returned pointer is unique.    
}

备注

为了确保明确的指针-属性行为,在定义指针时始终使用显式指针属性。

建议仅在需要指针别名时才使用 [ptr]。