繁体   English   中英

Delphi上的C dll标头

[英]C dll header on Delphi

我想从“ x.dll”中加载一个函数,并且我对函数(.h文件)有以下定义:

uint32 BioAPI_ModuleAttach(
    const BioAPI_UUID *ModuleGuid,
    const BioAPI_VERSION *Version,
    const BioAPI_MEMORY_FUNCS *MemoryFuncs,
    uint32 DeviceID,
    uint32 Reserved1,
    uint32 Reserved2,
    uint32 Reserved3,
    BioAPI_FUNC_NAME_ADDR *FunctionTable,
    uint32 NumFunctionTable,
    const void *Reserved4,
    BioAPI_HANDLE_PTR NewModuleHandle);

typedef uint32 BioAPI_HANDLE, *BioAPI_HANDLE_PTR;

但是,我只传递最后一个参数,而我只想传递0 (空)...如何实现? 有什么办法可以自动执行de .lib?

我尝试过,但是实现方式错误。.:\\

function BioAPI_ModuleAttach(
           ModuleGuid: array of Byte; 
           Version: HWND; 
           MemoryFuncs: HWND; 
           DeviceID: UInt32; 
           Reserved1: UInt32; 
           Reserved2: UInt32; 
           Reserved3: UInt32; 
           FunctionTable: HWND; 
           NumFunctionTable: UInt32; 
           Reserved4: Pointer; 
           NewModuleHandle: PINT) : UInt32; cdecl; external 'PvFw.dll';

谢谢您的关注。

我认为这可能有效:

function BioAPI_ModuleAttach(
  ModuleGuid: PByte; // pass a pointer to the first array element
  Version: PByte;    // PByte probably is wrong here, look up the type!
  MemoryFuncs: PByte; // Also probably wrong, what is BioAPI_MEMORY_FUNCS?
  DeviceID: UInt32; 
  Reserved1: UInt32; 
  Reserved2: UInt32; 
  Reserved3: UInt32; 
  FunctionTable: PBYTE; // pass a pointer to a BioAPI_FUNC_NAME_ADDR 
  NumFunctionTable: UInt32; // that's probably the length of the above
  Reserved4: Pointer; 
  NewModuleHandle: PUINT32) : UInt32; cdecl; external 'PvFw.dll';

如果所有参数都可以为零或NULL,则应像这样调用它:

var
  DeviceId: UInt32; // LongWord
  Handle: UInt32; // LongWord;
begin
  DeviceId := <something>
  Handle := 0;
  Res := BioAPI_ModuleAttach(nil, nil, nil, DeviceId, 0, 0, 0, nil, 0, nil, @Handle);

当然,这有很多假设,因为您没有提供有关许多指针参数的任何信息。 我已经将它们声明为PByte,但这可能是错误的。

查看源代码,我将其转换为:

const
  BioAPI_MAX_STR_LEN = 255;

type
  BioAPI_RETURN = UInt32;

  BioAPI_VERSION_PTR = ^BioAPI_VERSION;
  BioAPI_VERSION = record
    Major: UInt32;
    Minor: UInt32;
  end;

  BioAPI_MALLOC = function(
    Size: UInt32;
    AllocRef: Pointer;
    FileName: PAnsiChar;
    Line: UInt32; 
  ): Pointer stdcall;

  // etc...

  BioAPI_MEMORY_FUNCS_PTR = ^BioAPI_MEMORY_FUNCS;
  BioAPI_MEMORY_FUNCS = record
    Malloc_func: BioAPI_MALLOC;
    Free_func: BioAPI_FREE;
    Realloc_func: BioAPI_REALLOC;
    Calloc_func: BioAPI_CALLOC;
    AllocRef: Pointer;
  end;

  BioAPI_FUNC_NAME_ADDR_PTR = ^BioAPI_FUNC_NAME_ADDR;
  BioAPI_FUNC_NAME_ADDR = record
    Name: array[0..BioAPI_MAX_STR_LEN - 1] of AnsiChar;
    Address: BioAPI_PROC_ADDR;
  end;

  // etc... I'll leave the rest for you. ;-)

function BioAPI_ModuleAttach(
  ModuleGuid: PGUID;
  Version: BioAPI_VERSION_PTR;
  MemoryFuncs: BioAPI_MEMORY_FUNCS_PTR;
  DeviceID,
  Reserved1,
  Reserved2,
  Reserved3: UInt32;
  FunctionTable: BioAPI_FUNC_NAME_ADDR_PTR;
  NumFunctionTable: UInt32;
  Reserved4: Pointer;
  var NewModuleHandle: HMODULE): BioAPI_RETURN; stdcall;  

请注意,在Win32中,将BioAPI定义为__stdcall ,因此cdecl肯定是错误的。 没有任何迹象表明必须打包记录。 我会让他们保持自然状态。

const
  BioAPI_MAX_STR_LEN = 255;
  BioAPI_OK = 0;
  BioAPI_INVALID_HANDLE = 0;

type
  BioAPI_UUID = packed array[0..15] of Byte; // or TGUID
  BioAPI_UUID_PTR = ^BioAPI_UUID;

  BioAPI_DEVICE_ID = UInt32;

  BioAPI_VERSION = record
        Major: UInt32;
        Minor: UInt32;
  end;
  BioAPI_VERSION_PTR = ^BioAPI_VERSION;

    BioAPI_MALLOC = function(Size: UInt32; Allocref: Pointer): Pointer; cdecl;
  BioAPI_FREE = procedure(Memblock: Pointer; Allocref: Pointer); cdecl;
    BioAPI_REALLOC = function(Memblock: Pointer; Size: UInt32; Allocref: Pointer): Pointer; cdecl;
    BioAPI_CALLOC = function(Num: UInt32; Size: UInt32; Allocref: Pointer): Pointer; cdecl;

  BioAPI_MEMORY_FUNCS = record
        Malloc_func: BioAPI_MALLOC;
    Free_func: BioAPI_FREE;
        Realloc_func: BioAPI_REALLOC;
        Calloc_func: BioAPI_CALLOC;
        AllocRef: Pointer;
  end;
  BioAPI_MEMORY_FUNCS_PTR = ^BioAPI_MEMORY_FUNCS;

  BioAPI_PROC_ADDR = function: UInt32; stdcall;

  BioAPI_FUNC_NAME_ = record
        Name: packed array[0..BioAPI_MAX_STR_LEN-1] of AnsiChar;
        Address: BioAPI_PROC_ADDR;
  end;
  BioAPI_FUNC_NAME_ADDR = array[0..0] of BioAPI_FUNC_NAME_;
  BioAPI_FUNC_NAME_ADDR_PTR = ^BioAPI_FUNC_NAME_ADDR;

  BioAPI_HANDLE = UInt32;
  BioAPI_HANDLE_PTR = ^BioAPI_HANDLE;

function BioAPI_ModuleAttach(
    ModuleGuid: BioAPI_UUID_PTR;
    Version: BioAPI_VERSION_PTR;
    MemoryFuncs: BioAPI_MEMORY_FUNCS_PTR;
    DeviceID: BioAPI_DEVICE_ID;
    Reserved1: UInt32;
    Reserved2: UInt32;
    Reserved3: UInt32;
    FunctionTable: BioAPI_FUNC_NAME_ADDR_PTR;
    NumFunctionTable: UInt32;
    Reserved4: Pointer;
    var NewModuleHandle: BioAPI_HANDLE): UInt32; cdecl; external 'PvFw.dll';

procedure Test;
var
  NewModuleHandle: BioAPI_HANDLE;
begin
  if BioAPI_ModuleAttach(nil, nil, nil, 0, 0, 0, 0, nil, 0, nil, NewModuleHandle) = BioAPI_OK then
  begin
    if NewModuleHandle <> BioAPI_INVALID_HANDLE then
    begin
      // Success
    end;
  end;
end;

如果cdecl不是正确的约定,请用stdcall替换

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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