[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Delphi DLL is used in VB

Posted on 2002-07-14
14
Medium Priority
?
277 Views
Last Modified: 2010-04-04
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?
0
Comment
Question by:slavikn
  • 8
  • 5
14 Comments
 
LVL 1

Author Comment

by:slavikn
ID: 7152161
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).
0
 
LVL 1

Author Comment

by:slavikn
ID: 7152168
Please don't answer!
0
 
LVL 1

Author Comment

by:slavikn
ID: 7152172
Sorry, I had to check something.
Please DO answer.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7153303
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)
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7153307
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
0
 
LVL 1

Author Comment

by:slavikn
ID: 7153620
Sorry. Your suggestions didn't help. I changed String to PChar, but still it didn't work.

Any clues?
0
 
LVL 1

Author Comment

by:slavikn
ID: 7153634
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.
0
 

Accepted Solution

by:
leonidn earned 80 total points
ID: 7153637
function TestFunc: PChar; stdcall; export;
var
   Test: String;
begin
   Test := 'qwe';
   Result := PChar(Test);
end;
0
 
LVL 1

Author Comment

by:slavikn
ID: 7153651
Ok, I understand. First I have to save the result as a String and then to convert it to PChar.

Thanks!
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7154057
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 ...
0
 
LVL 1

Author Comment

by:slavikn
ID: 7154117
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?
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7154405
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
0
 
LVL 1

Author Comment

by:slavikn
ID: 7154680
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.
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7155955
I also understood that, but you never should return a string from a dll funtion
when you run into trouble you will see why
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month20 days, 2 hours left to enroll

873 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