Interfacing Delphi to a C Library

Is it possible to directly invoke the functions of a C library from a Delphi program? Assuming I have the .o and .h file from the C library, how do I invoke the functions of that library from the Delphi application?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

No, you can't directly (please correct me if I'm wrong... ...but I think this is the answer...).

Two possibility's:
- Rewrite the C library's to delphi units you can include.
- Restructure the code from the C library in a COM object, using COM you can make code from any language re-usable in other languages.



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Yes you can, though there are some caveats: You need to compile the .o file with a Borland C compiler for a start...

Here's an example:

unit externalLib;

  Function Fred(I : Integer):Integer;


{$L ExtLib.o}

Function Fred(i : Integer) : Integer; external;


You can the call the function Fred.


mtrudgiaAuthor Commented:

I thought of perhaps encapsulating the C library in a DLL wrapper, and using the DLL from Delphi... would this work?
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Yes, that would also work (and is the best idea if you only, say, the MS C compiler...)

You might like to note the JPEG support in delphi is jsut the C JPEG library compiled to .o files and linked in as I showed above.


mtrudgiaAuthor Commented:

The only thing is I've never written a DLL before.

I have an old C++ compiler (Optima++ from PowerSoft) which says it can be used to produce Windows 32-bit DLLs.

Do you know of a good web resource that might instruct me on how to write a DLL in C or C++.

Thanks again
Why not use Delphi to make the dll´s  i made my first one last saturday, total mess but i got there...
you may want to study some text on calling conventions.

i recently build a delphi wrapper component around a C++ builder (tcp/ip) .dll, wich after a few design modifications (the calling conventions and event handlers) works quite well.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.