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

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 shidongdong 的 blog

" namespace="ms-help://borland.bds3" keywords="system.runtime.interopservices.gchandle">gchandle , although relatively easy, is fairly expensive in terms of performance. it also has the possibility of resource leaks if handles aren’t freed correctly. if object references are maintained in the managed code, it is possible to pass a unique index, for example the hash code returned by the 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 typemanaged data type
supply datareceive 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

本文关键:Delphi2005学习笔记2——Using Platform Invoke with Delphi 2005
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top