thoga31

Test DLL Object Pascal

Jan 25th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 0.87 KB | None | 0 0
  1. program test_call_lib;
  2. type
  3.    TLibReport = (LibOK, LibUnkown, LibNoMethod);
  4.  
  5. function CallProc(const idLib, idProc : string) : TLibReport;
  6. const
  7.    LIBEXT = {$ifdef windows} '.dll' {$else} '.so' {$endif}
  8. type
  9.    TProc = procedure; stdcall;
  10. var
  11.    dll  : Cardinal;
  12.    proc : TProc;
  13.  
  14. begin
  15.    CallProc := LibOK;
  16.    dll := LoadLibrary(idLib + LIBEXT);
  17.    if dll <> 0 then begin
  18.       @proc := GetProcAddress(dll, idProc);
  19.       if Assigned(proc) then
  20.          proc
  21.       else
  22.          CallProc := LibNoMethod;
  23.       FreeLibrary(dll);
  24.    end else
  25.       CallProc := LibUnknown;
  26. end;
  27.  
  28.  
  29. begin
  30.    case CallProc('libexample', 'foo') of
  31.       LibOK       : writeln('[Done]');
  32.       LibUnknown  : writeln('Library not found');
  33.       LibNoMethod : writeln('Library doesn''t have the method.');
  34.    end;
  35.    {$ifdef windows} readln; {$endif}  // pause for Windows
  36. end.
Advertisement
Add Comment
Please, Sign In to add comment