Calling D3 DLL from VB & passing strings
Posted on 1998-06-25
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;
i : Integer;
x : PChar;
workString : PChar;
// figure out how much memory we need...
amtToAllocate := StrLen(str1) + StrLen(str2) + StrLen(str3) + StrLen(str4) + 6;
// allocate some storage
workString := AllocMem(amtToAllocate);
// encrypt it
for i := 0 to StrLen(workString) - 1 do begin
workString[i] := chr(ord(workString[i]) xor 7);
// free up the workstring
authenticate := i;
---[ end of Delphi code ]---
Public Declare Function authenticate lib "mydll" (str1 as String, str2 as String, str3 as String, str4 as String) as Integer
---[end of VB declaration]---
private sub calldll()
dim retVal as Integer
retVal = authenticate('XXXXXXX','XXXXXXX','XXX-XXX','X')
' display results here...