Solved

Delphi and static libraries

Posted on 1997-08-18
5
965 Views
Last Modified: 2010-04-06
I have a 3rd party static library (.LIB) to which I need to make function calls from within a Delphi application. I need to link this static library into the Delphi application. How do I do that?
0
Comment
Question by:trinitech
  • 2
  • 2
5 Comments
 

Author Comment

by:trinitech
ID: 1341782
Edited text of question
0
 
LVL 1

Expert Comment

by:fsanchez
ID: 1341783
What compiler and version was used to create that library?
0
 

Author Comment

by:trinitech
ID: 1341784
Not quite sure about the compiler and version which was used to create the statis library, but it was not Borland C++.
Just out of curiosity, how would that matter anyway?
0
 
LVL 1

Expert Comment

by:fsanchez
ID: 1341785
 If you want to link that library it needs to be compatible with your compiler. The lasts Borland C++ compilers are compatible with Delphi (Borland C++ 5.0 and Borland C++ Builder).
  Keep in mind that even Delphi2 and Delphi3 can't use the same DCUs.
  I think that you will not be able to link that DCU anyway. You could create a DLL with a compatible compiler exporting that function calls. Then you could use that DLL from Delphi.
0
 
LVL 1

Accepted Solution

by:
arh earned 200 total points
ID: 1341786
Most probably, what you want to do is impossible. But you can try anyhow ;-).
  First, your lib 'bitness' should match one of Delphi. 16-bit
libraries may be called from only Delphi 1, and 32-bit - only  
from Delphi 2 or 3.
  Second, you should know what 'calling convention' is used by
functions in the library. Delphi 1 supports only pascal (the default) and cdecl, Delphi 2 - pascal, cdecl, stdcall, and register.
Functions written in C are usually cdecl.
  Third, you should split your library into separate object files. This can be done by tlib utility from Borland C, or by any 'librarian' utility. To get the list of object files in library:
    tlib library.lib, library.lst
To extract object files from library:
    tlib library.lib *obj1.obj *obj2.obj
  At last, suppose you have file foo.obj, which contains function foo, written in C and looking in C as
  int foo( int a, char *b )
To make it available from Delphi, you should write:
  unit foo;
  interface
  function _foo( a: integer; b: pchar ): integer; cdecl;
  implementation
  function _foo; external;
  {$L foo.obj}
  end.
Names of the functions should be given exactly as they appear in library.lst file (here _foo instead of foo).
All functions from the library should be declared in one unit.
You don't need to know which .obj file contains wich function,
just declare needed functions and procedures from the library
and include _all_ object files by $L directives.
  But even when you succesfully compile your program, it may not work. It may not work if any function accepts or returns
floating-point value. It will not work if any function in library
makes a call to standard C library function. Or it just will crash
with no apparent reason.

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now