Solved

Calling D3 DLL from VB & passing strings

Posted on 1998-06-25
4
151 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
  • 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now