Recent

Author Topic: Program unable to use resource from a .res file  (Read 5700 times)

440bx

  • Hero Member
  • *****
  • Posts: 3944
Program unable to use resource from a .res file
« on: April 07, 2018, 09:57:16 pm »
Hello,

I am using the latest version of Lazarus on Windows 7 SP1 64bit.  I should probably mention that I have one day of experience with FPC and Lazarus - IOW, I'm fairly clueless but trying to correct that problem.

I am compiling a program for 32bit execution (for now, after everything works right in 32bit, I'll tackle the 64bit version.)

The example consists of porting a very simple Windows API program which compiles and runs with Delphi 2 (yes, version 2) and performs as expected.

The program's resource file is produced by BRCC32.exe with the -w32 option to create a 32bit Windows resource file.

When Delphi 2 is handed the resulting .res file, it includes it in the executable and the resources, which include a menu and a dialog box are present and work as expected.

When using the same resource file with Lazarus or the command line FreePascal compiler, the menu and the dialog box are missing indicating that something went wrong with the resource file.

The file are as follows

1. resource.pas

Code: Pascal  [Select][+][-]
  1.   { resource IDs for the TextOut example                                      }
  2.  
  3. {-----------------------------------------------------------------------------}
  4. INTERFACE
  5. {-----------------------------------------------------------------------------}
  6.  
  7. const
  8.  IDM_EXIT        = 900;
  9.  
  10.  IDM_ABOUT       = 990;
  11.  
  12. {-----------------------------------------------------------------------------}
  13. IMPLEMENTATION
  14. {-----------------------------------------------------------------------------}
  15.  
  16. end.
  17.  

the resource file .rc which "includes" the above resource.pas is as follows:

Code: Pascal  [Select][+][-]
  1. #include "resource.pas"
  2.  
  3. APPICON                 ICON                         "APPICON.ICO"
  4.  
  5. APPMENU                 MENU
  6.   BEGIN
  7.     POPUP "&File"
  8.       BEGIN
  9.         MENUITEM "E&xit",                            IDM_EXIT
  10.       END
  11.  
  12.     POPUP "&Help    "
  13.       BEGIN
  14.         MENUITEM "&About  TextOut ...",              IDM_ABOUT
  15.       END
  16.   END
  17.  
  18. ABOUTBOX DIALOG 42, 20, 170, 90
  19. STYLE DS_MODALFRAME | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU
  20. CAPTION "About TextOut"
  21. FONT 8, "MS Sans Serif"
  22. BEGIN
  23.   ICON              "APPICON", -1, 10, 10, 32, 32
  24.   LTEXT             "Win32 API Example - Functions", -1, 35, 10, 170, 10
  25.   LTEXT             "TextOut", -1, 35, 20, 170, 10
  26.   CTEXT             "Copyright \251 1997  You can copy it",-1, 1, 40, 170, 10
  27.   CTEXT             "All rights reserved.", -1, 1, 48, 170, 10
  28.   DEFPUSHBUTTON     "OK", IDOK, 60, 67, 50, 14
  29. END
  30.  

and the relevant bit of the program that uses the resource is as follows:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE        GUI}
  2.  
  3. {$IFDEF FPC}
  4.   {$MODE       Delphi}
  5.   {$MODESWITCH ANSISTRINGS-}
  6. {$ENDIF}
  7.  
  8. {$LONGSTRINGS    OFF}
  9. {$WRITEABLECONST ON}
  10.  
  11.  
  12. {$R textout.RES}
  13.  
  14. program _TextOut;
  15.  
  16. uses Windows, Messages, Resource;
  17.  

Delphi 2 is happy with all of the above and produces what is show in the attachement that shows the program with its menu and its dialog box.

FPC and Lazarus, produce the program that has no menu and no dialog.


(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Program unable to use resource from a .res file
« Reply #1 on: April 07, 2018, 11:57:25 pm »
Where is rest of the main program? Can you attach (sources only) your project files in this thread?

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Program unable to use resource from a .res file
« Reply #2 on: April 08, 2018, 12:19:04 am »
Where is rest of the main program? Can you attach (sources only) your project files in this thread?

Most definitely can.  In the attached 7z archive are the source files, the .res file obtained with BRCC32.EXE and the executable obtained using Delphi 2.   I did this less than 5 minutes before typing this post and tested it to ensure it works as it should (using Delphi 2.0)

Thank you for your help :-)

John.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Program unable to use resource from a .res file
« Reply #3 on: July 12, 2018, 03:25:21 am »
Just in case someone else gets caught in this trap. 

The resource file was being overwritten with a new resource file of the same name by Lazarus.     

The solution is simple: do not use the same name for your resource file and the program because that's the name Lazarus uses, causing your resource file to be unceremoniously overwritten.   Call it something like "MyFunkyName.res" and it will be linked into the exe as expected and everything will work (provided that your resource IDs don't collide with those of Lazarus.)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018