Page 1 of 1

resource files for beginner

Posted: Sun Mar 14, 2021 5:37 pm
by rooky06
Hello
I searched the internet and found a lot of examples of using resource files with icons and executable information but nothing that deals with a simple text file.
Indeed, I would like to put in resource a simple text file which contains a single line and to be able to extract this content and store it in a variable during the execution of my program.

Do you have a simple example ?

I also know that it is possible to modify a resource file as do the software dedicated to it without it being necessary to recompile the program ...
Is this possible in Purebasic, do you have an example please?

Thank you for the help you can give to a beginner...

Re: resource files for beginner

Posted: Sun Mar 14, 2021 6:32 pm
by mk-soft

Code: Select all

;-TOP

text = PeekS(?LabelText, -1, #PB_Ascii)
Debug text

DataSection
  LabelText:
  IncludeFile "Any Text File as ASCII.txt" ; Included text file into the executable
  Data.i 0
EndDataSection


Re: resource files for beginner

Posted: Mon Mar 15, 2021 12:12 am
by rooky06
Thank you but i can't understand one thing :
Included text file into the executable
It means that i dont need to extract the source file ?

Re: resource files for beginner

Posted: Mon Mar 15, 2021 12:24 am
by BarryG
rooky06 wrote:It means that i dont need to extract the source file ?
Correct. The PeekS() line in that example shows that the embedded text file has been read from directly in the exe (not extracted).

But mk-soft should've used a MessageRequester() to show it, because the Debug command is ignored in compiled executables (which is why nothing is shown if you compile the code as an exe to test).

Re: resource files for beginner

Posted: Mon Mar 15, 2021 6:38 am
by TI-994A
rooky06 wrote:...I would like to put in resource a simple text file which contains a single line and to be able to extract this content and store it in a variable during the execution of my program. ... I also know that it is possible to modify a resource file as do the software dedicated to it without it being necessary to recompile the program ...
Firstly, let's address the difference between resources and resource files.

Resources are elements that are embedded into the compiled binary itself, and accessed internally. Once the binary is compiled, the resources cannot be changed unless the binary is recompiled with the amended resources. In PureBasic, this is done with the IncludeFile/XIncludeFile and DataSection functions, as demonstrated in mk-soft's example.

Resource Files, on the other hand, refer to standalone files that contain resource elements, which are accessed externally. The elements within resource files can be changed anytime, independent of the compiled binaries that use them. Obviously, this only requires that the resource files are distributed and properly installed alongside the compiled binaries.

Re: resource files for beginner

Posted: Mon Mar 15, 2021 10:18 am
by Josh
@TI-994A

I wouldn't say that, although I can only speak of Windows here.

Recource files are files that come with the program or are included in the exe. In Pb this must be done in CompilerOptions > Resources. Since resources are appended at the end of the exe, they can also be replaced with a simple hack. How useful this is, everyone must decide for himself.

Whether we can call a DataSection in Pb a resource, I don't know. Includefiles are definitely no resources, maybe IncludeBinary.

Re: resource files for beginner

Posted: Mon Mar 15, 2021 11:44 am
by Keya
IncludeBinary/IncludeFile does not use resource files. In Windows for example, you cannot find IncludeFile's in compilations by enumerating the resources using either the PE format or the Microsoft API.
PB simply includes them in the ".data" section (like it does with strings for example), allowing you to reference them by their address, as mk-soft showed by using a label. They are always loaded into process memory at process startup, and readable/writeable.

Re: resource files for beginner

Posted: Mon Mar 15, 2021 12:25 pm
by BarryG
TI-994A wrote:Resources are elements that are embedded into the compiled binary itself, and accessed internally. Once the binary is compiled, the resources cannot be changed unless the binary is recompiled with the amended resources.
Not quite. The Resource Hacker app lets you remove/change some types of resources in compiled binary, without recompiling. I just tested it again by removing all embedded WAV files from my exe, and then swapping one embedded WAV file with another from my hard drive. All without recompiling my exe. The WAV files were included in PureBasic's "Compiler Options -> Resources" tab that pointed to an ".rc" file.

Re: resource files for beginner

Posted: Mon Mar 15, 2021 1:39 pm
by rooky06
thank you for your explain

Re: resource files for beginner

Posted: Mon Mar 15, 2021 2:15 pm
by blueb
rooky06 wrote:thank you for your explain
Most users would use the 'IncludeBinary' command to add icons, files, etc. to their EXE file.

It's more direct and easier to implement. I know I haven't needed to add 'Resource' files to my apps for years.

Of course, if you have a special need, you may be forced to use resources... :wink:

e.g.
;-------------
Test$ = PeekS(?Text, -1, #PB_UTF8)
MessageRequester("",Test$,0)

;-------------
DataSection
Text:
IncludeBinary "myTextInfo.txt"
Wheel:
IncludeBinary "Wheel.png"
EndDataSection

End

Re: resource files for beginner

Posted: Mon Mar 15, 2021 2:56 pm
by mk-soft
You forggot Null-Term for Text

Code: Select all

;-------------
Test$ = PeekS(?Text, -1, #PB_UTF8)
MessageRequester("",Test$,0)

;-------------
DataSection
Text:
IncludeBinary "myTextInfo.txt"
Data.i 0
BeginWheel:
IncludeBinary "Wheel.png"
EndWheel:
EndDataSection

End

Re: resource files for beginner

Posted: Mon Mar 15, 2021 4:46 pm
by TI-994A
Josh wrote:Recource files are files that come with the program or are included in the exe. In Pb this must be done in CompilerOptions > Resources. Since resources are appended at the end of the exe, they can also be replaced with a simple hack.
BarryG wrote:The Resource Hacker app lets you remove/change some types of resources in compiled binary, without recompiling. I just tested it again by removing all embedded WAV files from my exe, and then swapping one embedded WAV file with another from my hard drive. All without recompiling my exe. The WAV files were included in PureBasic's "Compiler Options -> Resources" tab that pointed to an ".rc" file.
These are referring to Microsoft's proprietary resource scripts (.rc) and files (.res).

Resources embedded with PureBasic's include functions work across all the three platforms, and are not as readily editable.

Re: resource files for beginner

Posted: Mon Mar 15, 2021 5:10 pm
by Axolotl
Hi,

resource files are not really needed with PureBasic, because there is another way of working and they are windows related. Nevertheless, you can work with resource files just like in other programming languages. Whether this is useful or not, I would leave to you.
Here is a small example how it can work.
I only use the stringtable and the language to display texts depending on the system language.

ps. i have never used this productively.

you need 3 files stringtable.h, stringtable.rc, text-program.pb

Code: Select all

// stringtable.h

#define LANG_ENGLISH      0  
#define LANG_GERMAN       1
#define SUBLANG_DEFAULT   0

#define txtHelloWorld  100 
#define btnOk          101
#define btnCancel      102 
The separation into h-file and rc-file is common, because the h-file is used in the source code. Under PB the content of the h-file can also be written directly in the rc-file (i guess, not tested).

Code: Select all

/*
* stringtable.rc
*/

#include "StringTable.h"

STRINGTABLE
LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT

BEGIN
  txtHelloWorld,  "Hello World" 
  btnOk,          "Ok"
  btnCancel,      "Cancel"
End

STRINGTABLE
LANGUAGE LANG_GERMAN, SUBLANG_DEFAULT
BEGIN
  txtHelloWorld,  "Hallo Welt" 
  btnOk,          "Ok"
  btnCancel,      "Abbrechen"
End

Code: Select all

; ***************************************************************************
; ***  File: Test_my_stringtable_resource.pb
; ***************************************************************************

Global ghInstance = GetModuleHandle_(#Null) ;' the handle to the application itself 

Procedure.s Language(uID) 
  Protected result$, buf${#MAX_PATH}  ;' #MAX_PATH == 260 -- existing constant in PB (as a lazy programmer) 

  If LoadString_(ghInstance, uID, @buf$, #MAX_PATH)  ;' returns number of characters in the string resource 
    result$ = buf$ 
  EndIf 
  ProcedureReturn result$ 
EndProcedure 


Enumeration EWindows 
  #WND_Main
EndEnumeration 

Enumeration EResourceConstants 100 
  #TXT_HelloWorld 
  #BTN_Ok  
  #BTN_Cancel
EndEnumeration

If OpenWindow(#WND_Main, #PB_Ignore, 0, 240, 200, "Test Resource.rc File", #PB_Window_SystemMenu) 
  TextGadget(#TXT_HelloWorld, 8, 8, 224, 20, Language(#TXT_HelloWorld)) 
  ButtonGadget(#BTN_Ok, 120, 40, 95, 25, Language(#BTN_Ok)) 
  ButtonGadget(#BTN_Cancel, 120, 70, 95, 25, Language(#BTN_Cancel)) 

  Repeat 
    Select WaitWindowEvent() 
      Case #PB_Event_CloseWindow 
        If MessageRequester("DEBUG", "DEBUG" + #LF$ + "Close now?", #MB_YESNO) = #IDYES 
          Break 
        EndIf 
      Case #PB_Event_Gadget
        If MessageRequester("DEBUG", "DEBUG" + #LF$ + "You pressed '" + Language(EventGadget()) + "' " + #LF$ + "Close now?", #MB_YESNO) = #IDYES  
          Break 
        EndIf 
;       Select EventGadget() 
;         Case #BTN_Cancel 
;           Break 
;         Case #BTN_Ok  
;           Break 
;       EndSelect 
    EndSelect 
  ForEver 
EndIf ; OpenWindow 

End 
Steps to do:
1. Add the resource file (here: stringtable.rc) to the PB source (here: Test_my_stringtable_resource.pb)
use the Menu: Compiler | Compiler Options ... Tab: Resources
filetype is default PORC resource scripts (*.rc)
2. compile the pb-source file as usual
the PB tool chain is taking care of the rc file and compiles it to an .res file and linked it statically to the executable. (Thats how I believe its working under the hood.)

You can use the following MSDN help pages to get more information about resource definition (on windows)
https://docs.microsoft.com/en-us/window ... urce-files
and important could be the section about resource definition statements
https://docs.microsoft.com/en-us/window ... statements