Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2002
    Posts
    19

    Question Unanswered: 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?

  2. #2
    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

  3. #3
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •