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

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

本文简介:选择自 shidongdong 的 blog

using platform invoke with delphi 2005 this topic describes the basic techniques of using unmanaged apis from delphi 2005. some of the common mistakes and pitfalls are pointed out, and a quick reference for translating delphi data types is provided. this topic does not attempt to explain the basics of platform invoke or marshaling data. please refer to the links at the end of this topic for more information on platform invoke and marshaling. understanding attributes and how they are used is also highly recommended before reading this document. the win32 api is used for several examples. for further details on the api functions mentioned, please see the windows platform sdk documentation. the following topics are discussed in this section:

  • calling unmanaged functions
  • structures
  • callback functions
  • passing object references
  • using com interfaces
calling unmanaged functions when calling unmanaged functions, a managed declaration of the function must be created that represents the unmanaged types. in many cases functions take pointers to data that can be of variable types. one example of such a function is the win32 api function systemparametersinfo that is declared as follows:
bool systemparametersinfo(
  uint uiaction,  // system parameter to retrieve or set
  uint uiparam,   // depends on action to be taken
  pvoid pvparam,  // depends on action to be taken
  uint fwinini    // user profile update option
);
depending on the value of uiaction, pvparam can be one of dozens of different structures or simple data types. since there is no way to represent this with one single managed declaration, multiple overloaded versions of the function must be declared (see borland.vcl.windows.pas), where each overload covers one specific case. the parameter pvparam can also be given the generic declaration intptr . this places the burden of marshaling on the caller, rather than the built in marshaler. note that the data types used in a managed declaration of an unmanaged function must be types that the default marshaler supports. otherwise, the caller must declare the parameter as intptr and be responsible for marshaling the data. data types most data types do not need to be changed, except for pointer and string types. the following table shows commonly used data types, and how to translate them for managed code:
unmanaged data typemanaged data type
input parameteroutput parameter
pointer to string (pchar) string stringbuilder
untyped parameter/buffer tbytes tbytes
pointer to structure (prect) const trect var trect
pointer to simple type (pbyte) const byte var byte
pointer to array (pinteger) array of integer array of integer
pointer to pointer type (^pinteger) intptr intptr

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

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

go top