Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calling D3 DLL from VB & passing strings

Posted on 1998-06-25
4
Medium Priority
?
160 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 400 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

722 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