Solved

Calling D3 DLL from VB & passing strings

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 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