urbansound
Programmer
Hi from the new guy and thanks in advance to all those who help solve problems in community efforts.
I've been working with FreePascalCompiler in Win 9x/XP since the 1.x days, presently at FPC 2.0.2. I have a rather unique interface situation and due mostly to the manner of parameter passing of strings in Pascal, (which I've not had the prior pleasure of), I need some pointers. (ooohhh, bad pun, sorry).
It's a Dll <> DLL <> Dll configuration which while I've had various parts of it working, I'm looking for the most reliable way, for obvious reasons, not to mention FPC in Delphi mode has some unique string typcasting automation available.
PowerBasic DLL FreePascal DLL Delphi 6 DLL stub
AsciiZ strings <> My project <> via script engine
by Ref long/short string variant / shortstring
I'm hoping to focus more on the PB <> FPC part of things, which is where I've had the most trouble.
PB relies on the win32 API handling of AsciiZ which apparently works fairly transparently with most Win32 systems. e.g. in VB I could apparently just toss strings at the parameters mostly and get back strings. In Pascal, it appears I need to either setup the typcasts very carefully, or possibly set up buffer and pointer management, or there's a two-step process casting strings first as constants, perhaps and faking the compiler by tucking the strings in parenthesis to appear as an expression.
The FPC app looks like this...
Essentially, I pass a string in from Delphi,(sPass) that calls a particular procedure in FPC. That procedure will then pass parameter structures to the PB dll, which being a byRef association will return modified data in the param list. The results coming back from PBdll are concatenated and tucked into the ShortString return value to the Delphi App where I parse the results out for use.
One issue is that the Variant data type from Delphi is quite persistent, but there are ways to solve that type conversion.
The issue has been either pre-sizing the strings in FPC before sending their pointer reference off as Pchar and/or dereferencing the data back out into strings reliably, rather than pchar pointer references (which cannot concatenate in FPC), in order to return it in the retval of the Delphi return parameter string.
In other instances, if I get the PB data to return in tact, the pchar's need to be reset for the next pass and doing so tends to make them become read-only according to the FPC docs as FPC auto dereferences them to zero if they are equated to an empty string, else I don't know what size the next call might require, or I could just load them with a dummy string on size.
So, I'm trying to do this by typecasting only and likely wasting a lot of time, shifting data between types and pointers.
Anyone care to brave a clean pass at "How would you do it?" philosopy?
Otherwise, I'll start adding more code structure if others would prefer.
Thank you in advance,
Mike
I've been working with FreePascalCompiler in Win 9x/XP since the 1.x days, presently at FPC 2.0.2. I have a rather unique interface situation and due mostly to the manner of parameter passing of strings in Pascal, (which I've not had the prior pleasure of), I need some pointers. (ooohhh, bad pun, sorry).
It's a Dll <> DLL <> Dll configuration which while I've had various parts of it working, I'm looking for the most reliable way, for obvious reasons, not to mention FPC in Delphi mode has some unique string typcasting automation available.
PowerBasic DLL FreePascal DLL Delphi 6 DLL stub
AsciiZ strings <> My project <> via script engine
by Ref long/short string variant / shortstring
I'm hoping to focus more on the PB <> FPC part of things, which is where I've had the most trouble.
PB relies on the win32 API handling of AsciiZ which apparently works fairly transparently with most Win32 systems. e.g. in VB I could apparently just toss strings at the parameters mostly and get back strings. In Pascal, it appears I need to either setup the typcasts very carefully, or possibly set up buffer and pointer management, or there's a two-step process casting strings first as constants, perhaps and faking the compiler by tucking the strings in parenthesis to appear as an expression.
The FPC app looks like this...
Code:
{LongStrings ON} //Or not, if we prefer
Library MyDLL;
//declare PB shared lib calls
procedure PBstringSub(s1,s2,s3,s4:pchar);external 'PBdll32.dll' //All are byRef
//declare the Delphi interface (stuck with it)
s := DelphDLL(var Variant1,Variant2,Variant3,Variant4, var sPass:ShortString):ShortString; //all are byRef
var s1,s2,s3,s4 :string type ?? {global variables in FPC}
//use a local proc to setup strings.
procedure DoStuff;
var sLocal1, sLocal2 :somekindofstring
begin
//setup strings with pchars or AsciiZ data?
PBstringSub(s1,s2,s3,s4);
//reset variables for next use, different call.
end;
Exports: s1+s2+s3+s4 //Answer back to Delphi, (but not as pointers).
begin
End.
Essentially, I pass a string in from Delphi,(sPass) that calls a particular procedure in FPC. That procedure will then pass parameter structures to the PB dll, which being a byRef association will return modified data in the param list. The results coming back from PBdll are concatenated and tucked into the ShortString return value to the Delphi App where I parse the results out for use.
One issue is that the Variant data type from Delphi is quite persistent, but there are ways to solve that type conversion.
The issue has been either pre-sizing the strings in FPC before sending their pointer reference off as Pchar and/or dereferencing the data back out into strings reliably, rather than pchar pointer references (which cannot concatenate in FPC), in order to return it in the retval of the Delphi return parameter string.
In other instances, if I get the PB data to return in tact, the pchar's need to be reset for the next pass and doing so tends to make them become read-only according to the FPC docs as FPC auto dereferences them to zero if they are equated to an empty string, else I don't know what size the next call might require, or I could just load them with a dummy string on size.
So, I'm trying to do this by typecasting only and likely wasting a lot of time, shifting data between types and pointers.
Anyone care to brave a clean pass at "How would you do it?" philosopy?
Otherwise, I'll start adding more code structure if others would prefer.
Thank you in advance,
Mike