If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > help! calling c dll from delphi

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-27-04, 15:47
byte2002 byte2002 is offline
Registered User
 
Join Date: Oct 2002
Posts: 19
Question help! calling c dll from delphi

I have some problems with calls to dlls from delphi.

How I should call a dll written in c from a program in delphi?

I, from a visual basic module, call to the procedure in the following way:

Declare Sub Proc2 Lib "p32.dll" (ByRef mat_a As Double, ByVal p As Integer, ByVal nmax As Integer, ByRef vector_d As Double, ByRef vector_e As Double)

and I pass the values this way:

Call Proc2(mat_a(1, 1), p, nmax, vector_d(1), vector_e(1))

from dolphin it should be proceeded in same way?
Reply With Quote
  #2 (permalink)  
Old 11-06-04, 10:50
sundialsvcs sundialsvcs is offline
Registered User
 
Join Date: Oct 2003
Posts: 706
Quote:
Originally Posted by byte2002
I, from a visual basic module, call to the procedure in the following way:

Declare Sub Proc2 Lib "p32.dll" (ByRef mat_a As Double, ByVal p As Integer, ByVal nmax As Integer, ByRef vector_d As Double, ByRef vector_e As Double)

and I pass the values this way:

Call Proc2(mat_a(1, 1), p, nmax, vector_d(1), vector_e(1))
Look at the external directive. Open up the VCL source-code (say Windows.pas) and look at how the declarations to Windows-API routines are declared.

Also take note of stdcall. There are several "calling conventions," which determine exactly how parameters are passed to-and-from the called routine.

Finally, in the declaration of a routine, the Visual Basic byRef is accomplished by using the keywords var or const in the procedure-declaration. The opposite, byVal, is the default in Delphi.

I politely point out that this particular subject is very well-covered in the Delphi documentation. There's much more to say about the subject, and these docs are pretty complete.
__________________
ChimneySweep(R): fast, automatic
table repair at a click of the
mouse! http://www.sundialservices.com
Reply With Quote
  #3 (permalink)  
Old 11-12-04, 06:02
Filip Poverud Filip Poverud is offline
Registered User
 
Join Date: Oct 2004
Location: Norway
Posts: 53
The easiest way to call a DLL from Delphi is first of all to look at the C declaration and then convert it to Delphi Pascal.

---

As mentioned earlier in this thread you are able to load a DLL file initially and then use it's exported functions, however another way, which I like better for many reasons, is to load the DLL dynamically.

I will give you an example.

type
TFoo = function (Arg1: PChar; Arg2: Integer): Boolean;
var
Foo: TFoo;
hLibrary: THandle;
begin
try
hLibrary:= LoadLibrary('NameOfDll.DLL');
@Foo:= GetProcAddress(hLibrary, 'CaseSensitiveNameOfExportedFunction');

Foo('Will This Work ?', 0);
finally
FreeLibrary(hLibrary);
end;
end;

In this context you are loading the DLL at the time you need it and then freeing it when you are done.

As mentioned earlier about calling conventions, both stdcall and cdecl parse the arguments the same direction. The difference is how they clean up the stack after usage. In general, cdecl is used for C/C++ compiled DLLs and stdcall / safecall are recommended in general. This applies to Windows.

I hope this gave you some more clues and less confusion about calling external functions from Delphi.

---

/pF
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On