tres2.pp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. { Test for FindResourceEx function. }
  2. {%TARGET=win32,win64,linux,freebsd,darwin,netbsd,openbsd,solaris}
  3. {$mode objfpc}
  4. uses
  5. sysutils;
  6. {$R tresb.res}
  7. procedure Fail(const Msg: string);
  8. begin
  9. writeln(Msg);
  10. Halt(1);
  11. end;
  12. function GetResource(ResourceType, ResourceName: PChar; ResLang : word; PResSize: PLongInt = nil): pointer;
  13. var
  14. hRes: TFPResourceHandle;
  15. gRes: TFPResourceHGLOBAL;
  16. begin
  17. writeln('trying ',ResourceType,':',ResourceName,':',IntToHex(ResLang,4));
  18. hRes:=FindResourceEx(HINSTANCE, ResourceType,ResourceName,ResLang);
  19. if hRes = 0 then
  20. Fail('FindResourceEx failed.');
  21. gRes:=LoadResource(HINSTANCE, hRes);
  22. if gRes = 0 then
  23. Fail('LoadResource failed.');
  24. if PResSize <> nil then begin
  25. PResSize^:=SizeofResource(HINSTANCE, hRes);
  26. if PResSize^ = 0 then
  27. Fail('SizeofResource failed.');
  28. end;
  29. Result:=LockResource(gRes);
  30. if Result = nil then
  31. Fail('LockResource failed.');
  32. end;
  33. procedure DoTest;
  34. const
  35. LANG_ENGLISH = $09;
  36. SUBLANG_ENGLISH_US = $01;
  37. LANG_ITALIAN = $10;
  38. SUBLANG_ITALIAN = $01;
  39. SUBLANG_ITALIAN_SWISS = $02;
  40. LANG_GERMAN = $07;
  41. SUBLANG_GERMAN = $01;
  42. var
  43. s: string;
  44. p: PChar;
  45. sz: longint;
  46. begin
  47. //us english, exact match
  48. p:=GetResource('FILE','TestFile', MakeLangID(LANG_ENGLISH,SUBLANG_ENGLISH_US), @sz);
  49. SetString(s, p, sz);
  50. if s <> 'test file.' then
  51. Fail('Invalid resource loaded.');
  52. writeln(s);
  53. //italian, exact match
  54. p:=GetResource('FILE','TestFile', MakeLangID(LANG_ITALIAN,SUBLANG_ITALIAN), @sz);
  55. SetString(s, p, sz);
  56. if s <> 'test file (italian).' then
  57. Fail('Invalid resource loaded.');
  58. writeln(s);
  59. { On Windows, FindResourceEx behaviour varies between versions, so we
  60. can't rely on the following tests }
  61. {$IFNDEF WINDOWS}
  62. //swiss italian , should fallback to italian
  63. p:=GetResource('FILE','TestFile', MakeLangID(LANG_ITALIAN,SUBLANG_ITALIAN_SWISS), @sz);
  64. SetString(s, p, sz);
  65. if s <> 'test file (italian).' then
  66. Fail('Invalid resource loaded.');
  67. writeln(s);
  68. //german, should fallback on the first resource found (english)
  69. p:=GetResource('FILE','TestFile', MakeLangID(LANG_GERMAN,SUBLANG_GERMAN), @sz);
  70. SetString(s, p, sz);
  71. if s <> 'test file.' then
  72. Fail('Invalid resource loaded.');
  73. writeln(s);
  74. {$ENDIF}
  75. end;
  76. begin
  77. writeln('Resources test.');
  78. DoTest;
  79. writeln('Done.');
  80. end.