[fpc-pascal] array of const with cdecl procedure
Sven Barth
pascaldragon at googlemail.com
Sun Nov 11 15:16:01 CET 2012
On 11.11.2012 13:58, ik wrote:
> On Sun, Nov 11, 2012 at 2:54 PM, Sven Barth <pascaldragon at googlemail.com> wrote:
>> On 11.11.2012 13:40, ik wrote:
>>>
>>> Hello,
>>>
>>> I'm trying to write a shard library of my own that you can use with C
>>> as well. I'm using mode fpc, and not objfpc.
>>>
>>> I created something like this:
>>>
>>> ...
>>> procedure varargs_example(params : array of consts); cdecl;
>>> ...
>>>
>>> The compiler report an error:
>>> Error:Type identifier expected
>>> Fatal: Syntax error, ")" expected but "CONST" found
>>>
>>>
>>> Please note that I need to access the "params" variable inside my code.
>>> How can I implement it correctly with FPC 2.6.0 ?
>>
>>
>> You can not write cdecl functions with variable argument lists in FPC. You
>> can only access such functions exported from C code. See also here:
>> http://wiki.freepascal.org/User_Changes_2.6.0#Array_of_const_parameters_and_cdecl_routines
>
> I know this one, but here I'm the one who write the code that is
> exported to C and not vice versa.
> How can I write it in a way that C can also use it ?
Yes and this is exactly what the change states. You can not write
"varargs" functions in Pascal that can be accessed from C.
The only solution would be to basically do something like the TVarRec
record does: have a record of which one field is a "type specifier" and
the remaining is a union out of the different supported types. You then
pass a Pointer to the first element of an array of that type and the
length of the array to the Pascal function. The negative point is of
course that you'll need to instantiate, populate and afterwards free the
array you pass to the pascal function.
Regards,
Sven
More information about the fpc-pascal
mailing list