Kanade's Delphi Stuff
Delphi Tips, Sample code and Tools
Copyright © 1997-2004, Sanjay Kanade
 
  Tips and Tricks on Delphi
Please read the DISCLAIMER before you use any of the tips from this web site. Go to main index
  Delphi Tips: Reusing C code
Tip 36:  Calling C in OBJ files
(modified: 23 Aug 2000)


  36: Calling C in OBJ files
Date added/modified: 23 Aug 2000

Look up the keyword "Obj files, calling" in Delphi help. If you try it, it doesn't work.

I found that it only works with C modules. Here is an example:

TESTC.C file

#include 

void _stdcall _export showAMessage(char *msg) {
  MessageBox(NULL, msg, "obj test", MB_OK);
}

PAS file code, partial

{$L testc.obj}
procedure showAMessage(s: pchar); stdcall; external;

procedure TForm1.Button1Click(Sender: TObject);
begin
  showAMessage(pchar('It worked!'));
end;

I added testc.c to a C++ builder project and compiled. Then, I used it in a Delphi 5 PAS file as above. It works!

But, if I make it into a testc.cpp module then it doesn't work even if I add extern "C" to the declaration.



 

Copyright 1995-2004, Sanjay Kanade. All rights reserved.
All trademarks and copyrights belong to their respective owners.