Solved

Calling D3 DLL from VB & passing strings

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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