Using COM Interfaces When using COM interfaces, a similar approach is taken as when using unmanaged API’s. The interface needs to be declared, using custom attributes to describe the type interface and the GUID. Next the methods are declared; using the same approach as for unmanaged API’s. The following example uses the IAutoComplete interface, defined as follows in Delphi 7: IAutoComplete = interface(IUnknown)
[''''{00bb2762-6a77-11d0-a535-00c04fd7d062}'''']
function Init(hwndEdit: HWND; punkACL: IUnknown;
pwszRegKeyPath: LPCWSTR; pwszQuickComplete: LPCWSTR): HRESULT; stdcall;
function Enable(fEnable: BOOL): HRESULT; stdcall;
end;
In Delphi 2005 it is declared as follows: [ComImport, GuidAttribute(''''00BB2762-6A77-11D0-A535-00C04FD7D062''''), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
IAutoComplete = interface
function Init(hwndEdit: HWND; punkACL: IEnumString;
pwszRegKeyPath: IntPtr; pwszQuickComplete: IntPtr): HRESULT;
function Enable(fEnable: BOOL): HRESULT;
end;
Note the custom attributes used to describe the GUID and type of interface. It is also essential to use the ComImportAttribute class. There are some important notes when importing COM interfaces. You do not need to implement the IUnknown/IDispatch methods, and inheritance is not supported. Data types The same rules as unmanaged functions apply for most data types, with the following additions:
Unmanaged Data Type |
Managed Data Type |
| Supply Data |
Receive Data |
GUID
System.Guid
System.Guid
IUnknown
TObject
TObject
IDispatch
TObject
TObject
Interface
TObject
TObject
Variant
TObject
TObject
SafeArray (of type)
array of <type>
array of <type>
BSTR
String
String Using the MarshalAsAttribute custom attribute is required for some of the above uses of TObject , specifying the exact unmanaged type (such as UnmanagedType.IUnknown , UnmanagedType.IDispatch or UnmanagedType.Interface
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!