Reputation: 1
I'm trying to use Delphi's DLL from Visual FoxPro 9 by passing string from VFP to Delphi's DLL. The Foxpro Crashes once I run the VFP code. My string values are under 254 characters.
Including ShareMem in delphi's code does not make any difference. It seems like the wrong string type is being used and I really don't know other types of string to code.
Please help me with an example on how to pass string.
The dll code works fine within Delphi.
in delphi's DLL...
library dll_examp_With_PARA;
uses
ShareMem,
SysUtils,
Classes,
Dialogs;
{$R *.res}
function showValues(var a:shortstring):shortstring; stdcall; export;
begin
Result:=('you passed ' + a);
end;
exports showValues;
end.
in VFP.....
CLEAR ALL
LOCAL vfpString as String
DECLARE STRING showValues IN dll_examp_With_PARA.dll STRING
vfpString = 'Hello World!'
? showValues(vfpString)
CLEAR ALL
Upvotes: 0
Views: 810
Reputation: 612954
That DLL cannot be called from Foxpro. You will have to modify the DLL or wrap it with an adapter. The problem is that you are using a private Delphi string type that is not suitable for interop. You have to understand that different languages have different ways to represent character data. For binary interop both sides must use the same representation.
Strings are passed from Foxpro as pointers to null-terminated arrays of 8 bit ANSI characters. In Delphi that is PAnsiChar
. That will allow you to pass a string from Foxpro to Delphi. In the other direction you need the Foxpro code to allocate a sufficiently large enough string. And then the Delphi code can copy the text into the memory provided by Foxpro. You will therefore want to also pass the length of the out string buffer so that the Delphi code can avoid writing beyond the end of the buffer.
Adding Sharemem
cannot help. That allows two Delphi modules to share the same native Delphi heap. Interop is difficult. You won't get anywhere with trial and error.
Upvotes: 1