tw7838b.pp 937 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. { %target=win32,win64,wince,linux}
  2. program prog;
  3. {$mode objfpc}
  4. uses
  5. dynlibs;
  6. // this function is exported from the EXE
  7. function exetest: longint; {public name 'exetest';}
  8. begin
  9. writeln('exe test');
  10. result:=5;
  11. end;
  12. exports
  13. exetest name 'exetest';
  14. const
  15. {$ifdef unix}
  16. {$ifdef darwin}
  17. libname = './libtw7838a.dylib';
  18. {$else}
  19. libname = './libtw7838a.so';
  20. {$endif}
  21. {$endif}
  22. {$ifdef mswindows}
  23. libname = '.\tw7838a.dll';
  24. {$endif}
  25. var
  26. dllf: function: longint;
  27. lh: tlibhandle;
  28. begin
  29. lh:= loadlibrary(libname); // load dyn.so (unix) or dyn.dll (ms windows)
  30. if lh = nilhandle then begin writeln('dyn library returned nil handle'); halt; end;
  31. pointer(dllf):= getprocaddress(lh, 'dllf'); // get function from dll
  32. // call function in dll, which calls function in exe, and then prints
  33. // a result number 5
  34. if (dllf()<>5) then
  35. halt(1);
  36. writeln(dllf());
  37. writeln('end of program');
  38. freelibrary(lh);
  39. end.