Board index » delphi » Delphi DLL call in VB : probleme

Delphi DLL call in VB : probleme

This is what VB return when i try to use a DLL (make in Delphi 4) :

    Bad DLL Calling Convention

This is the VB function that call the dll function :

    Private Sub Command1_Click()
    Dim M2 As Integer
    Dim Rep As Integer

        M2 = 23
        Rep = Calculator (M2)
        ' same result with : Rep = Calculator(23) or Rep = Calculator(ByVal
23)
    End Sub

This is the declaration in VB module :

    Declare Function Calculator Lib
"C:\WINDOWS\Bureau\Pas\Srv_DLL\Srv_Mess.dll" (VVV As Integer) As Integer

This is the DLL in Delphi 4 :

    library Srv_Mess;

    uses
        SysUtils,
        Classes;

    function Calculator(VVV : smallint) : smallint;
    var rep : smallint;
    begin
        rep := 2345 + VVV;
        result := rep;
    end;

    Exports
        Calculator;

    begin
    end.

At compilation and build there are no error and no warning...

I can do function without argument and that run ok, but when I add Argument
I have the error : Bad DLL Calling Argument, Why ???

Really Thanks...

Robin Leblond
rob...@hebdomag.com

 

Re:Delphi DLL call in VB : probleme


The problem is exactly what is stated. For a function to be called Delphi by
default uses another method of passing parameters to the dll. VB doesn't
know this method. For this reason the delphi function should be declared
using stdcall. I think the helpfile will have info on this.

Quote
Robin Leblond <rob...@hebdomag.com> wrote:
> This is what VB return when i try to use a DLL (make in Delphi 4) :
>     Bad DLL Calling Convention
>     function Calculator(VVV : smallint) : smallint;

try this
     function Calculator(VVV:smallint) : smallint; stdcall;

Quote
>     Exports
>         Calculator;
>     begin
>     end.
> At compilation and build there are no error and no warning...

Makes sense, delphi can perfectly call this dll.

Quote
> Really Thanks...
> Robin Leblond
> rob...@hebdomag.com

Roel

Re:Delphi DLL call in VB : probleme


Hi Robin,

I don't know VB at all, but I think I've heard you have to use the
stdcall calling convention to interface with VB:

Quote
>     function Calculator(VVV : smallint) : smallint; stdcall;

                                                         ^^^

Greetings, Robert
--
Robert Ro?mair
http://home.t-online.de/home/Robert.Rossmair/
Programming environment: Delphi 3.0, WinNT 4.0 SP5

Other Threads