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

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

来源:互联网 作者:西部数码 时间:2008-04-09
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
Marshal class contains a method to retrieve the offset of a given field, it is extremely slow and should be avoided in most situations. Informal performance tests show that for a structure with eight or nine numeric fields, it is much faster to allocate a block of unmanaged memory, copy the byte array to the unmanaged memory and call PtrToStructure than finding the position of just one field using Marshal. OffsetOf and converting the data using the BitConverter class. Borland.Vcl.WinUtils contains helper functions to perform conversions between byte arrays and structures (see StructureToBytes and BytesToStructure ). Special cases There are cases where custom processing is required, such as sending a message with a pointer to an array of integers. For situations like this, the Marshal class provides methods to copy data directly to the unmanaged buffer, at specified offsets (so you can construct an array of a custom data type after allocating a buffer). The following example shows how to send a message where the LParam is a pointer to an array of Integer:
function SendArrayMessage(Handle: HWND; Msg: UINT; WParam: WPARAM;
  LParam: TIntegerDynArray): LRESULT; 
var
  Buffer: IntPtr;
begin
  Buffer := Marshal.AllocHGlobal(Length(LParam) * SizeOf(Integer));
  try
    Marshal.Copy(LParam, 0, Buffer, Length(LParam));
    Result := SendMessage(Handle, Msg, WParam, Buffer);
  finally
    Marshal.FreeHGlobal(Buffer);
  end;
end;
Callback Functions When passing a function pointer for a managed function to an unmanaged API, a reference must be maintained to the delegate or it will be garbage collected. If you pass a pointer to your managed function directly, a temporary delegate will be created, and as soon as it goes out of scope (at the end of MyFunction in the example below), it is subject to garbage collection. Consider the following Delphi 7 code:
function MyFunction: Integer;
begin
  ...
  RegisterCallback(@MyCallback);
  ...
end;
In order for this to work in a managed environment, the code needs to be changed to the following:
const
  MyCallbackDelegate: TFNMyCallback = @MyCallback;

function MyFunction: Integer;
begin
  ...
  RegisterCallback(MyCallbackDelegate);
  ...
end;
This will ensure that the callback can be called as long as MyCallbackDelegate is in scope. Data types The same rules apply for callbacks as any other unmanaged API function. Special cases Any parameters used in an asynchronous process must be declared as IntPtr . The marshaler will free any memory it has allocated for unmanaged types when it returns from the function call. When using an IntPtr , it is your responsibility to free any memory that has been allocated.
Passing Object References

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!