tres2ext.pp 2.3 KB

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