Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Returning parameters to a Delphi Dll from a Firefox addon sdk


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Aug 29, 2014 3:29 PM Last Post By: Alastair Breingan
Alastair Breingan

Posts: 7
Registered: 1/11/14
Returning parameters to a Delphi Dll from a Firefox addon sdk  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 14, 2014 5:07 PM
I am trying to call a delphi (xe5) dll from a Firefox extension built with the addon sdk which seems to use ctypes, and i am having trouble returning a string parameter.

the firefox code is

var lib = res.ctypes.open("C:
Documents
RAD Studio
Projects
test
test.dll");
var getVar = lib.declare("GetBrwInf", res.ctypes.winapi_abi, ctypes.jschar.ptr, ctypes.jschar.ptr, ctypes.int32_t, ctypes.jschar.ptr, ctypes.jschar.ptr, ctypes.jschar.ptr);
var ret = getVar("WINTTL", 0, "google", "", "");

and the delphi procedure started as per a working version that is called from other windows programs and use PChars, and an output variable rather than a function return.

procedure GetBrwInf(const CmdIn: PChar; const OffSet: Integer;
const SchTxtIn: PChar; const SchSubIn: PChar;
const FldSelIn: PChar; const OutVar: PChar)stdcall; export;

The input paramenters are received correctly and all seems to work until the procedure ends at which point firefox crashes, presumably due to a fault in the return parameter, which is set by the following code:-

OutTxtP := PChar(OutTxt);
i := length(OutTxtP);
StrMove(OutVar, OutTxtP, i);
OutVar[i] := #0;

I would be very grateful for any help
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: Returning parameters to a Delphi Dll from a Firefox addon sdk  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 15, 2014 1:30 AM   in response to: Alastair Breingan in response to: Alastair Breingan
Alastair Breingan wrote:

I am trying to call a delphi (xe5) dll from a Firefox extension built
with the addon sdk which seems to use ctypes, and i am having trouble
returning a string parameter.

the firefox code is

var lib = res.ctypes.open("C:
Documents
RAD
Studio
Projects
test
test.dll"); var getVar =
lib.declare("GetBrwInf", res.ctypes.winapi_abi, ctypes.jschar.ptr,
ctypes.jschar.ptr, ctypes.int32_t, ctypes.jschar.ptr,
ctypes.jschar.ptr, ctypes.jschar.ptr); var ret = getVar("WINTTL", 0,
"google", "", "");

and the delphi procedure started as per a working version that is
called from other windows programs and use PChars, and an output
variable rather than a function return.

procedure GetBrwInf(const CmdIn: PChar; const OffSet: Integer;
const SchTxtIn: PChar; const SchSubIn: PChar;
const FldSelIn: PChar; const OutVar: PChar)stdcall; export;

This setup requires the caller of the procedure to allocate a block
of memory for the characters and pass its address as OutVar. Since the
parameter list has no parameter that could take the number of
characters that could fit into the memory the procedure has to assume a
certain fixed size (which the caller is guaranteed to use by contract),
and guard against copying more characters into the passed buffer than
are supposed to fit.

The input paramenters are received correctly and all seems to work
until the procedure ends at which point firefox crashes, presumably
due to a fault in the return parameter, which is set by the following
code:-

OutTxtP := PChar(OutTxt);
i := length(OutTxtP);
StrMove(OutVar, OutTxtP, i);
OutVar[i] := #0;

When posting such code snippets always include the declaration of the
variables referred to!

The main problem of this setup is

- you do not check whether OutVar is nil.
- you do not guard against buffer overwrites (which require you to
know the size if the buffer OutVar points at).

You could change the setup to return a pointer to a Delphi-allocated
buffer (make the parameter "out OutVar: PChar") and declare it on the
firefox side as a ctypes.char.ptr.ptr (if I got this correctly, see
http://stackoverflow.com/questions/6485312/need-a-working-example-of-fir
efox-ctypes-output-string-parameter ).
The main problem here is that each call would leak that memory block,
since the Firefox side has no way to deallocate it. Your library has to
provide a separate function the Firefox code has to call with the
pointer it receives to allow the Delphi side to free the memory. You
could use StrAlloc/StrDispose for this buffer. You cannot just return
a PChar cast from a local string variable, since that variable goes out
of scope and is automatically freed when the procedure exits.

--
Peter Below (TeamB)
Alastair Breingan

Posts: 7
Registered: 1/11/14
Re: Returning parameters to a Delphi Dll from a Firefox addon sdk  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 29, 2014 3:29 PM   in response to: Peter Below in response to: Peter Below
In case this helps anyone I have (eventually) got this working. The Ctypes code must try and emulate what happens when a C program in Windows calls a DLL because the only thing that seems to work is to define the DLL just as you would for that combination. So the function definition might be:-
function GetBrwInf(const CmdIn: PChar; const OffSetIn: Integer;
const SchTxtIn: PChar; const SchSubIn: PChar;
const FldSelIn: PChar): PChar stdcall; export;
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02