simple.pp 671 B

123456789101112131415161718192021222324252627282930313233
  1. program simple;
  2. uses
  3. ffi;
  4. function WritePChar(s: PChar): LongInt; cdecl;
  5. begin
  6. Writeln(s);
  7. WritePChar := StrLen(s);
  8. end;
  9. var
  10. cif: ffi_cif;
  11. args: array[0..0] of pffi_type;
  12. values: array[0..0] of Pointer;
  13. s: PChar;
  14. rc: ffi_arg;
  15. begin
  16. args[0] := @ffi_type_pointer;
  17. values[0] := @s;
  18. if ffi_prep_cif(@cif, FFI_DEFAULT_ABI, 1, @ffi_type_sint, @args[0]) = FFI_OK then begin
  19. s := 'Hello World';
  20. ffi_call(@cif, ffi_fn(@WritePChar), @rc, @values[0]);
  21. Writeln('Length: ', rc);
  22. s := 'This is cool!';
  23. ffi_call(@cif, ffi_fn(@WritePChar), @rc, @values[0]);
  24. Writeln('Length: ', rc);
  25. end else
  26. Writeln('ffi_prep_cif failed');
  27. end.