Page 1 of 1

Help with ressources files (.RC)

Posted: Sun Jan 15, 2023 8:46 am
by loulou2522
Hello,
I am a beginner in the use of resource files (.RC)
Here is my problem ; I would like to create an ini file which would be included in the executable, and which would be able to be modified in memory and then re-entered in the executable
I don't know how to do this or how to create a .RC file in the first place
I'm not sure how to create an RC file in the first place, but I'd like to know if someone can give me the basics so I can start programming.
Thanks in advance

Re: Help with ressources files (.RC)

Posted: Mon Jan 16, 2023 5:20 pm
by Axolotl
I am pretty sure you have your reasons, because this is not the easiest way with PB.
Anyways, first you need a resource script file like:

Code: Select all

// ResDlg.rc 
// Einfache AboutBox für PureBasic 
// by TS-Soft
1000 DIALOG 0, 0, 109, 52
//LANGUAGE LANG_NEUTRAL, 0
//STYLE DS_CENTER | DS_MODALFRAME | DS_SETFONT | WS_BORDER | WS_DLGFRAME | WS_POPUP | WS_SYSMENU | WS_VISIBLE
//FONT 9, "Arial"
CAPTION "Über..."
BEGIN
    CONTROL         "PureBasic 3.94  Beta 4                   Feel the ...Pure... Power", 1001, "Static", WS_GROUP | WS_VISIBLE, 19, 7, 102, 20
    CONTROL         "&Okay", 1002, "Button", BS_BOTTOM | BS_CENTER | BS_DEFPUSHBUTTON | BS_TOP | WS_MAXIMIZEBOX | WS_VISIBLE, 35, 30, 50, 15
END

Your PB-Code looks like that:

Code: Select all

; Simples Beispiel für eine AboutBox als Resource
; erstellt von TS-Soft

;/############################################################################
Procedure GetResDialog(DialogId, *DialogProcedure)
  ProcedureReturn DialogBoxParam_(GetModuleHandle_(0), DialogId, 0, *DialogProcedure, 0)
EndProcedure
;/############################################################################

;/ Deklaration von Konstanten
; #define IDD_DLG1 1000
; #define IDC_LBL1 1001
; #define IDC_BTN1 1002
#IDD_DLG1 = 1000
#IDC_LBL1 = 1001
#IDC_BTN1 = 1002

; Beispiel einer DialogProcedure
Procedure DlgProc(hDlg, uMsg, wParam, lParam)
  Select uMsg
    Case #WM_INITDIALOG
      MessageBeep_(#MB_ICONINFORMATION)
    Case #WM_CLOSE
      EndDialog_(hDlg, 0)
    Case #WM_COMMAND
      ID = wParam & $FFFF
      Select ID
        Case #IDC_BTN1
          EndDialog_(hDlg, #IDC_BTN1) ; 1002 als Rückgabewert bei okay
      EndSelect
  EndSelect
EndProcedure

; Aufruf der DialogResource
Select GetResDialog(#IDD_DLG1, @DlgProc())
  Case 0
    MessageRequester("Dialog-Test", "Dialog wurde geschlossen")
  Case #IDC_BTN1
    MessageRequester("Dialog-Test", "Benutzer hat Okay gedrückt zum beenden")
EndSelect
Don't forget to add the resource script file to Compiler | Compiler Options | Ressource
This example is very old and from the german forum, i guess,
HINT: There are also so-called Resource-Editors.
BTW: I have never tried and work still with the PB standard.

Happy coding and stay healthy.

Re: Help with ressources files (.RC)

Posted: Thu Jan 19, 2023 12:17 pm
by Denis
Hi loulou2522

when an executable file is running, it's possible to read the data in resource (string, Datas etc.), but not to modify them.
You have to use the API :
But EndUpdateResource_() returns error 5 (via GetlastError), I did some tests.
error 5 means
ERROR_ACCESS_DENIED

5 (0x5)

Access is denied.
BeginUpdateResource API indicates for its first parameter pFileName:
The binary file in which to update resources. An application must be able to obtain write-access to this file; the file referenced by pFileName cannot be currently executing. If pFileName does not specify a full path, the system searches for the file in the current directory.
May be a solution will be to create a thread which will launch the main exe file, and and having sharing memory to store INI values.
When you close the main exe, the thread will update resources from main exe file.
In that case, you will have 2 exe files.
I do not try this.

in addition, the way to store the ini value depends of how to use them.