I doubt that Delph could correctly free the C array ( they would both have to have exactly the same structure).
So you have to use WFFreeMemory call to free the memory.
This causes Delphi problems (Delphi maybe trying to change reference counts etc).
 
A point. Should this not be declared as (This may be the source of your problems).
 
type P_WF_PROCESS_INFO: ^ ARRAYOF_WF_PROCESS_INFO;
       ARRAYOF_WF_PROCESS_INFO = Array of WF_PROCESS_INFO;
var LProcessInfo: P_WF_PROCESS_INFO;
 
Then you can just assign nil to your pointer and delphi will not do anything about it. (after calling the free routine).
 
Your current declaration, declares a Delphi dynamic array (yes, which is a pointer - but unlikely to be compatible with the C array)
 
Hope this helps.
 
Myles.
-----Original Message-----
From: Stacey Verner [mailto:[EMAIL PROTECTED]]
Sent: Monday, December 18, 2000 9:33 AM
To: Multiple recipients of list delphi
Subject: [DUG]: Problems using Delphi to access C/C++ arrays from a DLL.

The following procedure access a method in WFAPI.dll which is an API to Citrix MetaFrame. I have converted the C header file to delphi, and it seems to work faily well, except I have trouble when freeing the memory that the dll has created for arrays. The DLL provides a function called WFFreeMemory which takes a pointer and frees the memory for the array stored at that address. This seems OK but then Delphi comes along and tries top free the memory itself, and we get access violations. This also happens if I explicitly go LProcessInfo := nil. If I omit the WFFreeMemory call this seems to work well, but I am not confident that the memory is being free'd.
 
How should I be freeing this memory?
 
procedure DisconnectedContacts(PUsers: TStrings);
var LProcessInfo: Array of WF_PROCESS_INFO;
  i, LCount: DWord;
begin
  // Call a method of WFAPI.DLL which creates an array, and returns a pointer
  // to the array in LProcessInfo
  if WFEnumerateProcesses(WF_CURRENT_SERVER_HANDLE,
      0,    // reserved
      1,    // version 1
      @LProcessInfo,
      LCount) then begin
    for i := 0 to LCount - 1 do begin
      // Do some stuff with the LProcessInfo array.
    end;
    // A method of WFAPI.DLL to free the memory associated with LProcessInfo.
    WFFreeMemory(LProcessInfo);
  end;
  // We then get access violations as Delphi tries to free the
  // LProcessInfo array.
end;
 
Thanks
 
Stacey.
 

Reply via email to