简体   繁体   中英

64 bits DLL c/c++ interface to Delphi

I have a .h file that I need to translate into Delphi, to call a DLL interface that is written in C/C++.

In 32bit, everything goes well, I can use the DLL with a Delphi app with no issue.

In 64bit, it does not work very well.

I did not write this DLL interface, it comes from a third party that does hardware.

namespace gXusb {
    
typedef int            INTEGER;
typedef short          INT16;
typedef unsigned       CARDINAL;
typedef unsigned char  CARD8;
typedef float          REAL;
typedef double         LONGREAL;
typedef char           CHAR;
typedef unsigned char  BOOLEAN;
typedef void *         ADDRESS;
    
struct CCamera;
    
extern "C" EXPORT_ void __cdecl Enumerate( void (__cdecl *CallbackProc)(CARDINAL) );
extern "C" EXPORT_ CCamera *__cdecl Initialize( CARDINAL Id );

Is Cardinal still 32bit unsigned under 64bit?

I'm not very sure about this way of declaring this function type and what it does with 64bit compilation:

void (__cdecl *CallbackProc)(CARDINAL)

It looks a bit cumbersome.

What puzzles me is this:

typedef unsigned       CARDINAL;

I have figured out this is 32bit for a 32bit DLL, but did it stay 32bit under a 64bit DLL?

Something like the following should work fine in both 32bit and 64bit:

unit gXusb;

interface

type
  // prefixing types that Delphi already declares...
  _INTEGER = Int32;
  _INT16 = Int16;
  _CARDINAL = UInt32;
  CARD8 = UInt8;
  _REAL = Single;
  LONGREAL = Double;
  _CHAR = AnsiChar;
  _BOOLEAN = ByteBool;
  ADDRESS = Pointer;

  CCamera = record end;
  PCCamera = ^CCamera;

  UsbEnumCallback = procedure(Param: _CARDINAL); cdecl;

procedure Enumerate(Cb: UsbEnumCallback); cdecl;
function Initialize(Id: _CARDINAL): PCCamera; cdecl;

implementation

const
  TheDLLName = 'the.dll';

procedure Enumerate; external TheDLLName;
function Initialize; external TheDLLName;

end.

Personally, I would just get rid of any type aliases that are not strictly necessary, use native Delphi types were appropriate, eg:

unit gXusb;

interface

type
  CARD8 = UInt8;

  CCamera = record end;
  PCCamera = ^CCamera;

  UsbEnumCallback = procedure(Param: UInt32); cdecl;

procedure Enumerate(Cb: UsbEnumCallback); cdecl;
function Initialize(Id: UInt32): PCCamera; cdecl;

implementation

const
  TheDLLName = 'the.dll';

procedure Enumerate; external TheDLLName;
function Initialize; external TheDLLName;

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