Search code examples
cdelphilabview

Problems calling C++ DLLs in Delphi


I'm having some problems calling C++ DLLs in delphi, the DLLs are written in Labview but the DLL syntax is all C++, I think the main issue is to do with trying to pass data to the function as a dynamic array of double (which is inherently a pointer right?)

the function definition in the.h header file is:

int16_t __cdecl NRELIVparExtract(
  double voltageV[], 
  double currentA[], 
  int32_t nDataPoints, 
  uint16_t fitAlgorithim, 
  int16_t *twoOrLessPoints, 
  double *Voc, 
  double *Isc, 
  double *Vmp, 
  double *Imp
);

My delphi code i'm trying to use to call it is:

public   { Public declarations }
end;

 var
    Function NRELIVparExtract (voltageV, currentA: Array of Double; nDataPoints :Integer;
            fitAlgorithim :Word; Var twoOrLessPoints : SmallInt;
       Voc, Isc, Vmp, Imp : Double): smallint ; CDecl;External IVparExtract_NREL.dll'

blah

procedure TFormMainIVanalysis.ExtractNREL(InputFileName : ShortString);
var
  VoltArray, CurrArray : Array Of double;
  blah
Begin

  NRELresult := NRELIVparExtract(VoltArray,CurrArray,NpointsForFitting, fitAlgorithm,     twoOrLessPoints, LVoc, LISc, LVmpp, LImpp);

I variously get either an access violation error when the compiler gets to the begin line in my delphi .dpr OR I get IVparExtract_NREL.dll not found

any suggestions very welcome, cheers, Brian


Solution

  • The C type double[] does not translate to the Delphi type array of Double. The Delphi type is an open array, which actually translates to two parameters internally, a pointer to the first element, and an integer holding one less than the number of elements in the array. Instead, you should do as C does, and "decay" the array to a simple pointer.

    The C type double* does not translate to the Delphi type Double. It's a pointer, so declare your parameter types as pointers.

    That should give you the following declaration:

    function NRELIVparExtract(voltageV, currentA: PDouble; nDataPoints: Integer;
        fitAlgorithim: Word; var twoOrLessPoints: SmallInt;
        Voc, Isc, Vmp, Imp: PDouble): SmallInt; cdecl;
        external 'IVparExtract_NREL.dll';
    

    If your program cannot find the DLL, then you should make sure that your DLL lives in a place where the OS will look for it, such as your program's directory, or somewhere on the system path. MSDN has the details on the library search order.