c++ - 将 C API 调用转换为 delphi

标签 c++ delphi winapi

我正在努力处理以下 C 代码以将其转换为 Dephi。假设 DLL 是 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
);

我正在尝试类似的东西:

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;

然后在实现部分:

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

我在设置结构、填充它们然后将它们连接在一起时有点迷茫。

最佳答案

你的函数声明全错了,你调用它也是错误的。

尝试更像这样的东西:

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;

或者这个:

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;

关于c++ - 将 C API 调用转换为 delphi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31255664/

相关文章:

c++ - WinApi 和 C++ - 状态栏不更新

c++ - 在 C++ 的 Eclipse 中识别@param?

c++ - 我可以在 while 循环中定义一个迭代器吗?

c++ - 在 C++ 中复制整数数组

delphi - 为什么delphi ide会增加windows平台定时器分辨率?

multithreading - TThread.CreateAnonymousthread/FreeOnTerminate 但随后使用 TTask/ITask

c++ - 相同大小的小对象的非常快速的对象分配器

delphi - 如何使用 Delphi 从命令行编译 Win32 程序

c++ - 进行 SendInput 时调用全局低级键盘 Hook 。如何预防?

c++ - 如何获取物理和逻辑磁盘列表?