dispinterface 屬性

dispinterface語句會定義一組屬性和方法,您可以呼叫IDispatch::Invoke。 散發介面可以藉由明確列出語法 1) (支援的方法和屬性集,或列出單一介面 (語法 2) 來定義。

[
    [attributes]
]
dispinterface dispinterface-name
{
    properties:
        property-list
    methods:
        method-list
};

[
  [attributes]
]
dispinterface dispinterface-name
{
    interface interface-name
};

參數

attributes

指定套用至整個 dispinterface的屬性。 接受下列屬性:[helpstring][helpcoNtext][helpfile][hidden][nonextensible][oleautomation][restricted][uuid][version]。

dispinterface-name

在類型程式庫中已知 dispinterface 的名稱。 此名稱在類型程式庫中必須是唯一的。

property-list

(語法 1) 物件的選擇性屬性清單,以變數形式宣告。 這是在方法清單中宣告屬性函式的簡短形式。 如需詳細資訊,請參閱批註一節。

method-list

(語法 1) 包含 dispinterface中每個方法和屬性之函式原型的清單。 任何數目的函式定義都可以出現在 methlist中。 methlist中的函式具有下列形式:

[attributes]returntype methname type paramname (params) ;

dispinterface中的方法接受下列屬性:[helpstring]、[helpcoNtext][propget][propput]、[propput]、[propputref]、[string][vararg]。 如果指定 [vararg] ,則最後一個參數必須是 VARIANT 類型的安全陣列。

參數清單是以逗號分隔的清單,每個元素都有下列形式:

[attributes]

此類型可以是任何宣告或內建類型,或是任何類型的指標。 參數的屬性如下:

[in][out][optional][string]

interface-name

(語法 2) 宣告為 IDispatch 介面的介面名稱。

備註

MIDL 編譯器接受下列參數排序 (從左至右) :

  1. 必要參數 (沒有 [defaultvalue] 或 [選擇性] 屬性) 的參數,
  2. 具有或不含 [defaultvalue] 屬性的選擇性參數,
  3. 具有 [選擇性] 屬性且不含 [defaultvalue] 屬性的參數,
  4. [ lcid] 參數,如果有的話,
  5. [ retval] 參數

方法函式的指定方式與 模組 的參考頁面中所述完全相同,但不允許 [ entry] 屬性。 請注意,STDOLE32。TLB (STDOLE。必須匯入 16 位系統上的 TLB) ,因為 dispinterface 繼承自 IDispatch。

您可以在屬性或方法清單中宣告屬性。 宣告屬性清單中的屬性並不表示屬性支援 (的存取類型,也就是 get、put 或 putref) 。 針對不支援 put 或 putref 的屬性指定 [ readonly] 屬性。 如果您在方法清單中宣告屬性函式,則一個屬性的函式全都有相同的識別碼。

使用第一個語法時,需要屬性: 和 方法:標籤。 每個成員也需要 [ id] 屬性。 例如:

properties: 
    [id(0)] int Value;    // Default property. 
methods: 
    [id(1)] HRESULT Show();

不同于介面成員,dispinterface 成員不能使用 retval 屬性傳回除了 HRESULT 錯誤碼以外的值。 [ lcid] 屬性對 dispinterfaces 同樣無效,因為 IDispatch::Invoke 會傳遞 LCID。 不過,您可以重新宣告使用這些屬性的介面。

使用第二個語法,支援 IDispatch 且稍早在 ODL 腳本中宣告的介面可以重新宣告為 IDispatch 介面,如下所示:

dispinterface helloPro 
{ 
    interface hello; 
};

上述範例會將 hello 的所有成員和 hello 繼承的所有成員宣告為支援 IDispatch。 在此情況下,如果 hello 先前已宣告為 [lcid] 和傳回 HRESULT 的 [retval] 成員,MkTypLib 會移除每個 [lcid] 參數和 HRESULT 傳回型別,並改為將傳回型別標示為 [retval] 參數的傳回型別。

注意

Mktyplib.exe工具已過時。 請改用 MIDL 編譯器。

 

dispinterface 的屬性和方法不屬於 dispinterface 的 VTBL。 因此, CreateStdDispatchDispInvoke 無法用來實作 IDispatch::Invoke。 當應用程式需要透過自動化公開現有的非 VTBL 函式時,就會使用 dispinterface。 這些應用程式可以藉由檢查 dispidMember 參數並直接呼叫對應的函式,來實作 IDispatch::Invoke。

範例

[ 
    uuid(1e196b20-1f3c-1069-996b-00dd010fe676), 
    version(1.0), 
    helpstring("Useful help string."), 
    helpcontext(2480)
] 
dispinterface MyDispatchObject 
{ 
    properties: 
        [id(1)] int x;    //An integer property named x 
        [id(2)] BSTR y;   //A string property named y 
    methods: 
        [id(3)] HRESULT show();    //No arguments, no result 
        [id(11)] int computeit(int inarg, double *outarg); 
}; 
 
[
    uuid(1e123456-1f3c-1069-996b-00dd010fe676)
] 
dispinterface MyObject 
{ 
    properties: 
    methods: 
        [id(1), propget, bindable, defaultbind, displaybind] long x(); 
 
        [id(1), propput, bindable, defaultbind, 
         displaybind] HRESULT x(long rhs); 
}

另請參閱

bindable

defaultbind

displaybind

helpcontext

helpfile

helpstring

隱藏

in

介面

TYPEFLAGS

ODL 檔案語法

ODL 檔案範例

使用 MIDL 產生類型程式庫

out

nonextensible

propget

propput

propputref

oleautomation

限制

string

uuid

vararg

版本