手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>delphi>列表

Delphi2005学习笔记2——Using Platform Invoke with Delphi 2005

来源:互联网 作者:西部数码 时间:2008-04-09
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
GetHashCode method, to the unmanaged API instead of an object reference. A hash table can be maintained on the managed side to facilitate retrieving an object instance from a hash value if needed. An example of using this technique can be found in the TTreeNodes class (in Borland.Vcl.ComCtrls). 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
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!