简体   繁体   中英

convert C API call to delphi

I am struggling with the following C code to convert it to Dephi. Let's say the DLL is myDLL.DLL:

struct ConfEx_Participant {
   DWORD  dwID;
   DWORD  dwPath;
};

LONG WINAPI AddtoConfEx(
    IN OUT LPVOID lpParams,    // parameter block
    IN OUT DWORD* lpSize,    // pointer to var holding size of lpParams
    IN DWORD dwPath,
    IN const ConfEx_Participant* pParticipant,  //array of participants
    IN DWORD cParticipant   // number of participants
);

I am trying something like:

PConfEx_Participant := ^TConfEx_Participant
TConfEx_Participant = record
    dwCallID: DWORD;
    dwPath: DWORD;
end;

type TAddtoConfEx = function {
    lpParams: DWORD_PTR;
    lpsize:   DWORD_PTR;
    dwPath:   DWORD;
    ConfEx_Participant: PConfEx_participant;
    cParticipant: Integer;
 end;

then in the implentation section:

Procedure Connect();
  lResult: Integer;
  Func := TAddToConfEx;
  begin
    Handle := SafeLoadLibrary('myDLL.DLL');
    @Func := GetProcAddress(Handle, 'AddToConfEx');
    lResult := Func(lpParams, lpSize, dwPath, @PConfEx_Participant, 2);
    ...

I am a little lost in setting up the structures, populating them and then connecting it all together.

You function is declared all wrong, and you are calling it wrong.

Try something more like this instead:

type
  PConfEx_Participant = ^ConfEx_Participant;
  ConfEx_Participant = record
    dwID: DWORD;
    dwPath: DWORD;
  end;

function AddtoConfEx(
  lpParams: Pointer;    // parameter block
  var lpSize: DWORD;    // pointer to var holding size of lpParams
  dwPath: DWORD;
  const pParticipant: PConfEx_Participant;  //array of participants
  cParticipant: DWORD   // number of participants
): LONG; stdcall; external 'myDLL.DLL';

procedure Connect;
var
  Params: Pointer;
  Size: DWORD;
  Path: DWORD;
  Participants: array of ConfEx_Participant;
  lResult: LONG;
begin
  Params := ...; // whatever it needs to point at...
  Size := ...; // however many bytes Params is pointing at...
  Path := ...; // whatever value you need...
  SetLength(Participants, ...); // whatever length you need...
  // populate Participants as needed...
  lResult := AddtoConfEx(Params, Size, Path, PConfEx_Participant(Participants), Length(Participants));
  ...
end;

Or this:

type
  PConfEx_Participant = ^ConfEx_Participant;
  ConfEx_Participant = record
    dwID: DWORD;
    dwPath: DWORD;
  end;

TAddtoConfEx = function(
  lpParams: Pointer;    // parameter block
  var lpSize: DWORD;    // pointer to var holding size of lpParams
  dwPath: DWORD;
  const pParticipant: PConfEx_Participant;  //array of participants
  cParticipant: DWORD   // number of participants
): LONG; stdcall;

procedure Connect;
var
  Func: TAddToConfEx;
  Params: Pointer;
  Size: DWORD;
  Path: DWORD;
  Participants: array of ConfEx_Participant;
  lResult: LONG;
begin
  Handle := SafeLoadLibrary('myDLL.DLL');
  if Handle = 0 then RaiseLastOSError;
  @Func := GetProcAddress(Handle, 'AddToConfEx');
  if not Assigned(Func) then RaiseLastOSError;
  ...
  Params := ...; // whatever it needs to point at...
  Size := ...; // however many bytes Params is pointing at...
  Path := ...; // whatever value you need...
  SetLength(Participants, ...); // whatever length you need...
  // populate Participants as needed...
  lResult := Func(Params, Size, Path, PConfEx_Participant(Participants), Length(Participants));
  ...
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM