Board index » delphi » Accessing 'C' typedefs from a .dll

Accessing 'C' typedefs from a .dll

I have a .dll, written in C, with some structs that I need to access from
Delphi.  I.e. in my C code I have a typedef like this:

typedef struct matrix {
        char    name[mxMAXNAM];
        dummy;
        int     m;
        int     n;
        double  *pr;
        double  *pi;
        int     dummy6,dummy7,dummy8;

Quote
} Matrix;

and functions like this:

_declspec(dllexport)
Matrix *average(Matrix *m1, Matrix *m2)
{
   /* .... etc */

Quote
}

How do I formulate the external cdecl in Delphi ?

-------------------==== Posted via Deja News ====-----------------------
      http://www.dejanews.com/     Search, Read, Post to Usenet

 

Re:Accessing 'C' typedefs from a .dll


dsba...@rocketmail.com schrieb in Nachricht
<885588971.924909...@dejanews.com>...

Quote
>I have a .dll, written in C, with some structs that I need to access
from
>Delphi.  I.e. in my C code I have a typedef like this:

>typedef struct matrix {
>        char    name[mxMAXNAM];
>        dummy;
>        int     m;
>        int     n;
>        double  *pr;
>        double  *pi;
>        int     dummy6,dummy7,dummy8;
>} Matrix;

type
  PMatrix = ^TMatrix;
  TMatrix = packed record
    Name: array[0..mxMAXNAM - 1] of Char;
    Dummy: Integer;
    M, N: Integer;
    PR, PI: PDouble;
    Dummy6, Dummy7, Dummy8: Integer;
  end;

Quote
>and functions like this:
>_declspec(dllexport)
>Matrix *average(Matrix *m1, Matrix *m2)
>{
>   /* .... etc */
>}

in D2/D3:

  function Average(const m1, m2: TMatrix): PMatrix; stdcall; external
'some.dll' name 'average';
                                                    =======
* In Win32, _declspec(dllexport) is the same as stdcall in Delphi
(afaik).

or in 2 steps: interface and implementation

  interface:       function Average(const m1, m2: TMatrix): PMatrix;
stdcall;
  implementation:  function Average; external 'some.dll' name
'average';

Rudy Velthuis

Quote
>How do I formulate the external cdecl in Delphi ?

>-------------------==== Posted via Deja News

====-----------------------
Quote
>      http://www.dejanews.com/     Search, Read, Post to Usenet

Re:Accessing 'C' typedefs from a .dll


dsba...@rocketmail.com schrieb in Nachricht
<885588971.924909...@dejanews.com>...

Quote
>I have a .dll, written in C, with some structs that I need to access
from
>Delphi.  I.e. in my C code I have a typedef like this:

>typedef struct matrix {
>        char    name[mxMAXNAM];
>        dummy;
>        int     m;
>        int     n;
>        double  *pr;
>        double  *pi;
>        int     dummy6,dummy7,dummy8;
>} Matrix;

type
  PMatrix = ^TMatrix;
  TMatrix = packed record
    Name: array[0..mxMAXNAM - 1] of Char;
    Dummy: Integer;
    M, N: Integer;
    PR, PI: PDouble;
    Dummy6, Dummy7, Dummy8: Integer;
  end;

Quote
>and functions like this:
>_declspec(dllexport)
>Matrix *average(Matrix *m1, Matrix *m2)
>{
>   /* .... etc */
>}

in D2/D3:

  function Average(const m1, m2: TMatrix): PMatrix; stdcall; external
'some.dll' name 'average';
                                                    =======
* In Win32, _declspec(dllexport) is the same as stdcall in Delphi
(afaik).

or in 2 steps: interface and implementation

  interface:       function Average(const m1, m2: TMatrix): PMatrix;
stdcall;
  implementation:  function Average; external 'some.dll' name
'average';

Rudy Velthuis

Quote
>How do I formulate the external cdecl in Delphi ?

>-------------------==== Posted via Deja News

====-----------------------
Quote
>      http://www.dejanews.com/     Search, Read, Post to Usenet

Re:Accessing 'C' typedefs from a .dll


dsba...@rocketmail.com wrote in article
<885588971.924909...@dejanews.com>...

Quote
> I have a .dll, written in C, with some structs that I need to access from
> Delphi.  I.e. in my C code I have a typedef like this:

> typedef struct matrix {
>         char    name[mxMAXNAM];
>         dummy;
>         int     m;
>         int     n;
>         double  *pr;
>         double  *pi;
>         int     dummy6,dummy7,dummy8;
> } Matrix;

> and functions like this:

> _declspec(dllexport)
> Matrix *average(Matrix *m1, Matrix *m2)
> {
>    /* .... etc */
> }

> How do I formulate the external cdecl in Delphi ?

type
  PMatrix = ^TMatrix;
  TMatrix = record
    name: array[0..mxMAXNAM] of Char;
    dummy: Integer;
    m: Integer;
    n: Integer;
    pr: ^double;
    pi: ^double;
    dummy6,dummy7,dummy8: Integer;
  end;

function average(m1, m2: PMatrix): PMatrix; cdecl; export;
begin
...
end;

- Show quoted text -

Quote

> -------------------==== Posted via Deja News ====-----------------------
>       http://www.dejanews.com/     Search, Read, Post to Usenet

Re:Accessing 'C' typedefs from a .dll


Dmitry schrieb in Nachricht
<01bd2a36$d6f939e0$35077...@Hobgoblin.nstu.nsk.su>...

Quote

>dsba...@rocketmail.com wrote in article
><885588971.924909...@dejanews.com>...
>> I have a .dll, written in C, with some structs that I need to
access from
>> Delphi.  I.e. in my C code I have a typedef like this:
...

>> _declspec(dllexport)
>> Matrix *average(Matrix *m1, Matrix *m2)
>> {
>>    /* .... etc */
>> }

>> How do I formulate the external cdecl in Delphi ?

>function average(m1, m2: PMatrix): PMatrix; cdecl; export;
>begin

Hello Dmitry

The Dll was written in C. The user of the dll doesn't have to export
the function. The __declspec(ddlexport) creates a stdcall routine, not
a cdecl routine (afaik).

Rudy Velthuis

Other Threads