Board index » delphi » Linking Borlan C function to a Borland Pascal Program

Linking Borlan C function to a Borland Pascal Program

We need to link a C-written function to an existing pascal program.

The pascal program is in protected mode. We are using Borland Pascal
7.0.

Is this possible?

If yes, what is the way to do it. Which C-Compiler should be used?

Thanks
  Detlef Wassmann

 

Re:Linking Borlan C function to a Borland Pascal Program


Quote
Detlef Wa?mann wrote:

> We need to link a C-written function to an existing pascal program.

> The pascal program is in protected mode. We are using Borland Pascal
> 7.0.

> Is this possible?

Yes. You have to write a Pascal-C interface in assembly.
The Pascal program calls the interface, the interface creates a C-style
stack frame and calls the external C proc, then restores the parameters
passed to the proc and removes the stack frame. Not too easy, but it's
not impossible.

Andras

Re:Linking Borlan C function to a Borland Pascal Program


Detlef Wa?mann schrieb in Nachricht <371211F2.3...@vt.siemens.de>...

Quote
>We need to link a C-written function to an existing pascal program.

>The pascal program is in protected mode. We are using Borland Pascal
>7.0.

>Is this possible?

>If yes, what is the way to do it. Which C-Compiler should be used?

>Thanks
>  Detlef Wassmann

-----------------------------------------------------------------

I think you are trying to do someting like: write a lowlevel fast c function
and link the object together with pascal code. You want to call C function
by pascal?

If it is what you want - Yes that is possible. But!!

C function is not allowed to use any library function. You only can do some
pointer arithmetik ...

All of the C function have to be declared as pascal like:

  int pascal anyfunc(int a, int b)

There should'nt be any restrictions to memory model or so. As I remember
borland has in it's manual some infos about mixed language programing

Regards
  Georg

Re:Linking Borlan C function to a Borland Pascal Program


Quote
Detlef Wa?mann wrote:
> We need to link a C-written function to an existing pascal program.
> The pascal program is in protected mode. We are using Borland Pascal
> 7.0.
> Is this possible?

What about using DLLs?

Quote
> If yes, what is the way to do it. Which C-Compiler should be used?

If the C-compiler is able to create 16-Bit DLLs, it should be possible
to call them directly from BP (without an ASM-interface)!
But since I've never really worked with C, I don't know about compilers
supporting DLLs! Sorry!

Hope to have helped anyway!
--
Arno Fehm (af...@bigfoot.de)

------------------------------------------------------------------------
Member of Grey Dreams Design: visit http://GreyDreams.home.pages.de !!!!
He who can destroy a thing has the real control over it. (Frank Herbert)
------------------------------------------------------------------------

Re:Linking Borlan C function to a Borland Pascal Program


On Mon, 12 Apr 1999 17:32:02 +0100, Detlef Wa?mann

Quote
<Detlef.Wassm...@vt.siemens.de> wrote:
> We need to link a C-written function to an existing pascal program.
> The pascal program is in protected mode. We are using Borland Pascal
> 7.0.
> Is this possible?
> If yes, what is the way to do it. Which C-Compiler should be used?

Preferrably any 16 Bit Borland compiler from Turbo C 2.0 upwards or a
MS 16 bit compiler or WATCOM. TP 6.0 and 7.0 support the standard C
models created by

The external C functions to be called from Pascal have to be declared
as

           void pascal p(<params>);  /* procedure */
           anytype pascal f(<params>);  /* function */

A function may return any scalar type.

The memory  model to be chosen is preferrably LARGE (far functions and
32-bit data pointers). Data pointers have to be large because TP
pointers are 32 bit. If the function is to be used in the interface in
a unit, it has to be far. If the functions are declared in the
interface part of a unit or in the main program, you may use the
memory model COMPACT (near calls, 32 bit pointers) for C compilation.

There are some restrictions, though. A C module to be linked to a TP
program may not declare externals (via header files) which are defined
in libraries or other C modules. Every variable at file level or
function which is not explicitly declared as 'static' has to be
declared in the TP program. Thus every global variable used in Pascal
_and_ in C has to be declared in Pascal and imported in the C program
via 'extern'.

A simple example:

Quote
>---p.c-------------------<

void pascal p(int *i) { *i = 42; }
int pascal f(int i, int j) { return i-j; }

Quote
>-------------------------<

Borland: TCC (or BCC) -c -ml p.c
WATCOM : WCL -c -ml -s -zl p.c

-ml       MODEL LARGE  ( -mc : MODEL COMPACT)

Quote
>--test.pas----------------<

procedure p(var i:integer);far;external;
function f(i,j:integer):integer;far;external;
{$L p.obj}
var
  i:integer;
begin
  p(i);
  writeln(i);
  writeln(f(3*i,i));
end.

Quote
>--------------------------<

BPC test

If you encounter problems, please contact me by email.

Regards
Horst

Other Threads