Solved

Calling D3 DLL from VB & passing strings

Posted on 1998-06-25
4
156 Views
Last Modified: 2010-04-04
I'm trying to write a DLL in Delphi 3 that I know will be called from Delphi 3, VB 5, Access forms, and very likely Powerbuiler and Java (with the MicroSoft SDK for Java).

Right now, I'm concentrating on getting concepts down for writing Delphi 3 DLLs and trying to get it to work with VB5 in a test scenario.

From the VB side, I need to pass in 4 strings and get an integer back.  On the Delphi side, I need to process the strings and send and integer.  Right now I'm passing back the number of characters in the processed string.

When I make the call from a VB sample app, I get an incorrect answer (18).  The same code in a Delphi sample app produces the correct answer (25).

I'm trying to avoid the DELPHIMM DLL buy using PChars. VB supposedly passes by reference as default, but something here is going amiss.

Any ideas?  Sample code follows :

Delphi function code (resides in DLL) :
---------------------------------------
Function authenticate (str1 : PChar;
                       str2 : PChar;
                       str3 : PChar;
                       str4 : PChar) : Integer; stdcall; export;
var
        i          : Integer;
        x          : PChar;
        amtToAllocate: Integer;
        workString : PChar;
begin
        // figure out how much memory we need...
        amtToAllocate := StrLen(str1) + StrLen(str2) + StrLen(str3) + StrLen(str4) + 6;
        // allocate some storage
        workString := AllocMem(amtToAllocate);

        StrCopy(workString,str1);
        StrCat(workString,'+');
        StrCat(workString,str2);
        StrCat(workString,'+');
        StrCat(workString,str3);
        StrCat(workString,'+');
        StrCat(workstring,str4);

        // encrypt it
        for i := 0 to StrLen(workString) - 1 do begin
                workString[i] := chr(ord(workString[i]) xor 7);
        end;

        StrCat(workString,chr(13));
        StrCat(workString,chr(10));

        // free up the workstring
        FreeMem(workString);
        authenticate := i;
end;

---[ end of Delphi code ]---

VB Declaration
--------------
Public Declare Function authenticate lib "mydll" (str1 as String, str2 as String, str3 as String, str4 as String) as Integer

---[end of VB declaration]---

VB call
-------
private sub calldll()
   dim retVal as Integer

   retVal = authenticate('XXXXXXX','XXXXXXX','XXX-XXX','X')  

   ' display results here...
   msgBox(retVal)
end sub

0
Comment
Question by:FBlair
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 

Author Comment

by:FBlair
ID: 1355848
Edited text of question
0
 

Author Comment

by:FBlair
ID: 1355849
Edited text of question
0
 
LVL 3

Accepted Solution

by:
vladika earned 100 total points
ID: 1355850
It is simple
If in Delphi you pass parameters by value then in VB you must pass parameters by value
If in Delphi you pass parameters by ref then in VB you must pass parameters by ref
So
or change code in Delphi as
function authenticate(VAR str1, str2, str3, str4: PChar): Integer; stdcall; export;
or change code in VB as
Public Declare Function authenticate lib "mydll" (ByVal str1 as String, ByVal str2 as String, ByVal str3 as String, ByVal str4 as String) as Integer

0
 

Author Comment

by:FBlair
ID: 1355851
Worked like a champ.  Thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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