tres.pp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. { Test for resources support. }
  2. {%TARGET=win32,win64,wince,linux,freebsd,darwin,netbsd,openbsd,solaris}
  3. {$mode objfpc}
  4. {$R tres1.res}
  5. procedure Fail(const Msg: string);
  6. begin
  7. writeln(Msg);
  8. Halt(1);
  9. end;
  10. function GetResource(ResourceName, ResourceType: PChar; PResSize: PLongInt = nil): pointer;
  11. var
  12. hRes: TFPResourceHandle;
  13. gRes: TFPResourceHGLOBAL;
  14. begin
  15. hRes:=FindResource(HINSTANCE, ResourceName, ResourceType);
  16. if hRes = 0 then
  17. Fail('FindResource failed.');
  18. gRes:=LoadResource(HINSTANCE, hRes);
  19. if gRes = 0 then
  20. Fail('LoadResource failed.');
  21. if PResSize <> nil then begin
  22. PResSize^:=SizeofResource(HINSTANCE, hRes);
  23. if PResSize^ = 0 then
  24. Fail('SizeofResource failed.');
  25. end;
  26. Result:=LockResource(gRes);
  27. if Result = nil then
  28. Fail('LockResource failed.');
  29. end;
  30. procedure DoTest;
  31. var
  32. s: string;
  33. p: PChar;
  34. sz: longint;
  35. begin
  36. p:=GetResource('TestFile', 'FILE', @sz);
  37. SetString(s, p, sz);
  38. if s <> 'test file.' then
  39. Fail('Invalid resource loaded.');
  40. writeln(s);
  41. p:=GetResource('Test', 'TEXT', @sz);
  42. SetString(s, p, sz);
  43. if s <> 'Another test file.' then
  44. Fail('Invalid resource loaded.');
  45. writeln(s);
  46. end;
  47. begin
  48. writeln('Resources test.');
  49. DoTest;
  50. writeln('Done.');
  51. end.