[fpc-pascal] DLL calling Firebird: slow and crashes at the end

Reinier Olislagers reinierolislagers at gmail.com
Sat Sep 27 13:58:25 CEST 2014


On 26/09/2014 21:41, Mark Morgan Lloyd wrote:
> Reinier Olislagers wrote:
>>> If using pchars: is the program doing all storage allocation for these
>>> or is the DLL also allocating memory?
>> The DLL is generating those pchars.
> 
> What frees them?
Heh ;)
Ok, I think I've got it worked out now: calling application is
responsible for memory.

Thanks to a Rudy Velthuis article on PChars, I got this for the library
code:

// Returns bytes written to TargetPChar
function DLLPassString(SourceString: string; TargetPChar: PChar;
MaxLength: integer): integer;
begin
  if (TargetPChar<>nil) and (MaxLength>1) then
  begin
    StrLCopy(TargetPChar,PChar(SourceString),MaxLength-1);
    result:=StrLen(TargetPChar); //number of bytes in result
    {$IFDEF DPDLLDEBUG}
    writeln('DLL: DLLPassString got TargetPChar ',TargetPChar);
    writeln('DLL: DLLPassString got Result ',Result);
    {$ENDIF}
  end
  else
  begin
    {$IFDEF DPDLLDEBUG}
    writeln('DLL: DLLPassString got nil TargetPChar or maxlength=0 for
sourcestring '+SourceString);
    {$ENDIF}
    result:=0;
  end;
end;

// Returns bytes written and city in City.
// CIty must be assigned by caller;
// maxlen must contain maximum size of City
function GetCity(City:PChar; MaxLen:integer): integer; {$IFDEF
MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF};
begin
  result:=DLLPassString(Address.City,City,MaxLen);
end;
and conversely:
// Passes city ready for ValidateAddress. Pass empty value if unknown.
procedure SetCity(City: PChar); {$IFDEF
MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF};
begin
  Address.City:=City;
end;

Does that look about ok?

>>> I thought Holland /was/ flat :-)  I was again thinking of the case where
>>> both the main program and the DLL were your code. Using shortstrings
>>> removes some of the dynamic allocation, hence potentially the need for a
>>> single memory manager (cmem).
Ok, but I want it to be usable in Delphi, Excel etc...

> Always a pleasure. Spent the evening herding sheep: 80m above sea level,
> which around here are called Downs :-)
Crazy Welshmen [1]... ;) Maybe that's what you get when the altitude
starts affecting your blood oxygen levels.
At least over here down below the sea we've got plenty of oxygen - what
we do with it is another story ;)

[1] Just guessing... name+sheep+down=up?!!? :)



More information about the fpc-pascal mailing list