Board index » delphi » Problems using Delphi to call C-functions.

Problems using Delphi to call C-functions.

Hi,

I've got a problem when trying to call a function implemented in C

Example

C-function compiled with VisualC++ 4.0 like cl /Gd -c add.c

File: add.c

long add( long x, long y)
{
  return x+y;

Quote
}

resulting in a object file add.obj

Then i create a Import unit for this function looking something like this.

unit Unit1;

interface
function add(x,y:integer):integer; far; cdecl; external; {$L add.obj}

implementation

end.

Now if i compile this file i'll get the following error message:

Bad file format: 'add.obj'

What am i doing wrong ? isn't MS VisualC++ object files compatible with Delphi
or what ?

Please reply by email.

Thanks in advance.

-------------------------------------------------------------------------------
H?kan Hellberg  

WM-data F?rsvarsdata AB

Stationsgatan 3, Box 9112, 96119 Boden, Sweden
New phonenumber from 1/1-96
Direct: +46 921-(0)56912, Fax: +46 (0)921-56939
email : h...@bod.fdata.se

 

Re:Problems using Delphi to call C-functions.


On Wed, 3 Jan 1996 14:24:58 GMT, Hakan Hellberg with WM-data commented on
Problems using Delphi to call C-functions.:

Quote
>C-function compiled with VisualC++ 4.0 like cl /Gd -c add.c

>File: add.c

>long add( long x, long y)
>{
>  return x+y;
>}

>resulting in a object file add.obj

>Then i create a Import unit for this function looking something like this.

>unit Unit1;

>interface
>function add(x,y:integer):integer; far; cdecl; external; {$L add.obj}

>implementation

>end.

>Now if i compile this file i'll get the following error message:

>Bad file format: 'add.obj'

>What am i doing wrong ? isn't MS VisualC++ object files compatible with
>Delphi
>or what ?

MSVC C++ 4.0 is a 32 bit compiler, and produces 32 bit code.  The current
Delphi is 16bit, so you would have to do some thunking to call a 32 bit
DLL.  I don't think it's even possible to call a 32 bit .obj like your
trying to do it.

BTW, I call C functions in .DLL's created with MSVC++ 1.52 (16bit version)
just fine with Delphi, the only incompatibility I am aware of is that
Borland and Microsoft return floating point types differently from
functions.

Quote
>Please reply by email.

Nope, since you posted the question here in the newsgroup, you will only
this answer if you follow the thread you started.

James V. Bacus

===============================================================================
 JVB Imaging                           jvba...@bix.com

 Medical Imaging Software & Systems - Custom Software Development - Consulting
===============================================================================

Re:Problems using Delphi to call C-functions.


Hakan,

I don't think you can link 'foreign' OBJ code into Delphi. Although I read
somewhere on the net that, with Delphi 2.0, one *will* be able to link Borland
C++ object modules into Delphi projects. I don't believe this information came
directly from Borland, however,

I'm not a C/C++ maven but I think the approach you would take would be to
generate a 16-bit DLL from your VC environment rather than an OBJ module. Among
other things, this would involve defining the function using the PASCAL
calling conventions. And then doing whatever VC needs by way of exporting the
function, etc., etc. I've heard creating DLLs is a lot less labour intensive in
Delphi.

I think there is how-to information on this around the net in the various
Delphi web pages.

If you don't want a DLL approach, I suspect you'll have to go Delphi 100%.

Regards and Happy New Year.                       ...edn

In article <30EA91BA....@bod.fdata.se>, h...@bod.fdata.se says...

Quote

>Hi,

>I've got a problem when trying to call a function implemented in C

>Example

>C-function compiled with VisualC++ 4.0 like cl /Gd -c add.c

>File: add.c

>long add( long x, long y)
>{
>  return x+y;
>}

>resulting in a object file add.obj

>Then i create a Import unit for this function looking something like this.

>unit Unit1;

>interface
>function add(x,y:integer):integer; far; cdecl; external; {$L add.obj}

>implementation

>end.

>Now if i compile this file i'll get the following error message:

>Bad file format: 'add.obj'

>What am i doing wrong ? isn't MS VisualC++ object files compatible with Delphi
>or what ?

>Please reply by email.

>Thanks in advance.

>------------------------------------------------------------------------------
-
>H?kan Hellberg  

>WM-data F?rsvarsdata AB

>Stationsgatan 3, Box 9112, 96119 Boden, Sweden
>New phonenumber from 1/1-96
>Direct: +46 921-(0)56912, Fax: +46 (0)921-56939
>email : h...@bod.fdata.se

Other Threads