Link to home
Start Free TrialLog in
Avatar of slavikn
slavikn

asked on

Delphi DLL is used in VB

Hello, I have a DLL file written in Delphi. Here is one of its functions (the function is included in the 'export' clause):

function EncryptStr(TheStr: String): String; stdcall; export;
begin
   Result := 'asd';
end;

Now I want to use this funcion in VB.

Public Declare Function EncryptStr Lib "InstStore" (ByRef TheStr As String) As String

And I want to use it.

MsgBox EncryptStr("Test")

But an error message is shown "Bad DLL calling convention". Why?
Avatar of slavikn
slavikn

ASKER

P.S.  This DLL also includes other functions which work fine. This is the only function which doesn't work (and it is the only one which returns a String value, but not the only one who has a String value as a parameter).
Avatar of slavikn

ASKER

Please don't answer!
Avatar of slavikn

ASKER

Sorry, I had to check something.
Please DO answer.
you should use PChar instead of string to make it compatible

an example how to make functions that return strings:

function GetSubject(Subject: PChar; Size: Integer): Integer; stdcall;
var tmp: string;
begin
     Result:=-1;
     if not Loaded then exit; // if you have to check something
     tmp:=yourstring;
     try
        Result:=Length(tmp);
        StrPLCopy(Subject, tmp, Size);
     except
        Result:=-2;
     end;
end;

and the vb part:

Public Declare Function GetSubject Lib "lnEMLpars.dll" (ByVal Body As String, ByVal Size as Long) As Long


Public Function EML_GetSubject() As String
Dim s As String
Dim l As Long
   
    s = Space(255) ' allocate a buffer
    l = GetSubject(s, 255)
    If l < 1 Then
        Exit Function
    End If
    EML_GetSubject = Left(s, l)

End Function


this is the correct way to use strings with dlls in delphi and VB (just like other MS API functions)
oh .. you should also check if your buffer was large enough:

Public Function EML_GetSubject() As String
Dim s As String
Dim l As Long
   
   s = Space(255) ' allocate a buffer
   l = GetSubject(s, 255)
   If l < 1 Then
       Exit Function
   End If
   If l > 255 then s = Space(l)
   l = GetSubject(s, l)
   EML_GetSubject = Left(s, l)

End Function
Avatar of slavikn

ASKER

Sorry. Your suggestions didn't help. I changed String to PChar, but still it didn't work.

Any clues?
Avatar of slavikn

ASKER

Your function works (because it doesn't return a String value. Try to write a function which will return a String value ...and please post it here.
ASKER CERTIFIED SOLUTION
Avatar of leonidn
leonidn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of slavikn

ASKER

Ok, I understand. First I have to save the result as a String and then to convert it to PChar.

Thanks!
what do you mean it didn't work ?
did you use the code I wrote ?
what didn't work ? it didn't compile or it didn't work in runtime
bah ...
Avatar of slavikn

ASKER

Your code worked. BUT, the result was not a String value. I already had functions one of whose parameters was a String value.

The problem I had was to return a String (or a PChar) value.

Am I clear?
aha then say so in the first place
anyway I showed the right way of how functions dealing with strings should be made and used
when you'll learn more about memory you'll see
Avatar of slavikn

ASKER

Please read my comment right after the question. Here it is: "This DLL also includes other functions which work fine. This is the only function which doesn't work (and it is the only one which returns a String value, but not the only one who has a String value as a parameter)."

leonidn understood that the problem was in returning a String value.
I also understood that, but you never should return a string from a dll funtion
when you run into trouble you will see why