简体   繁体   中英

C dll header on Delphi

I want to loadd a function from "x.dll",and I have this definition of the function(.h file):

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;

But, I only to pass the last parameter and the others I want to pass 0 (null)... How can I implement? There is any way to do automatic with de .lib?

I tried, but I implemented in wrong way.. :\\

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';

Thanks, for attention.

I think this could work:

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';

Calling it like this should work, if all the parameters can be zero or 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);

Of course this assumes a lot, since you did not provide any information about many of the pointer parameters. I have declared them as PByte, but that's probably wrong.

Looking at the sources, I would convert that like this:

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;  

Note that in Win32, BioAPI is #defined as __stdcall , so cdecl is definitely wrong. There is nothing that indicates that records must be packed. I would leave them at their natural alignment.

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;

In case cdecl is not the correct convention replace with stdcall

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