How does UpdateResource work?

I tried for several times to update a string table with UpdateResource in plain c++.
But it didn't work.
It looked like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
hResource = BeginUpdateResource(path, FALSE);
    char uChar[10]="MOI";
    wchar_t wChar[10];
     cout<<wChar<<"\r\n";
    mbstowcs(wChar, uChar, sizeof(uChar));
    if (NULL != hResource)
    {
        if (UpdateResource(hResource, 
            RT_STRING, 
            MAKEINTRESOURCE(40000), 
            MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), 
            (void*)wChar, 
            10) != FALSE)
        {
            cout<<"SUCCESS";
            EndUpdateResource(hResource, FALSE);
        }
    }

It always say SUCCESS, but in the Resource there is no change.
closed account (z05DSL3A)
EndUpdateResource(hResource, FALSE); Writes the changes to the resource and then close it. you should check to see if the write succeeds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    ...
    MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), 
    (void*)wChar, 
    10) != FALSE)
    {
        if (EndUpdateResource(hUpdateRes, FALSE)) 
        { 
            cout<<"SUCCESS";
        }     
        else
        {
            cout<<"Failed";
        }  
    }
    ...

Last edited on
It still say Success
closed account (z05DSL3A)
Hmm, In that case I don't know.

Here is some code from MSDN, maybe you can see somthing different...

The following example copies a dialog box resource from one executable file, Hand.exe, to another, Foot.exe, by following these steps:

Use the LoadLibrary function to load the executable file Hand.exe.
Use the FindResource and LoadResource functions to locate and load the dialog box resource.
Use the LockResource function to retrieve a pointer to the dialog box resource data.
Use the BeginUpdateResource function to open an update handle to Foot.exe.
Use the UpdateResource function to copy the dialog box resource from Hand.exe to Foot.exe.
Use the EndUpdateResource function to complete the update.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
HRSRC hResLoad;     // handle to loaded resource 
HANDLE hExe;        // handle to existing .EXE file 
HRSRC hRes;         // handle/ptr. to res. info. in hExe 
HANDLE hUpdateRes;  // update resource handle 
char *lpResLock;    // pointer to resource data 
BOOL result;

// Load the .EXE file that contains the dialog box you want to copy. 
hExe = LoadLibrary("hand.exe"); 
if (hExe == NULL) 
{ 
    ErrorHandler("Could not load exe."); 
} 
 
// Locate the dialog box resource in the .EXE file. 
hRes = FindResource(hExe, "AboutBox", RT_DIALOG); 
if (hRes == NULL) 
{ 
    ErrorHandler("Could not locate dialog box."); 
} 
 
// Load the dialog box into global memory. 
hResLoad = LoadResource(hExe, hRes); 
if (hResLoad == NULL) 
{ 
    ErrorHandler("Could not load dialog box."); 
} 
 
// Lock the dialog box into global memory. 
lpResLock = LockResource(hResLoad); 
if (lpResLock == NULL) 
{ 
    ErrorHandler("Could not lock dialog box."); 
} 
 
// Open the file to which you want to add the dialog box resource. 
hUpdateRes = BeginUpdateResource("foot.exe", FALSE); 
if (hUpdateRes == NULL) 
{ 
    ErrorHandler("Could not open file for writing."); 
} 
 
// Add the dialog box resource to the update list. 
result = UpdateResource(hUpdateRes,       // update resource handle 
     RT_DIALOG,                   // change dialog box resource 
     "AboutBox",                  // dialog box name 
     MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),  // neutral language
     lpResLock,                   // ptr to resource info 
     SizeofResource(hExe, hRes)); // size of resource info. 

if (result == FALSE) 
{ 
    ErrorHandler("Could not add resource."); 
} 
 
// Write changes to FOOT.EXE and then close it. 
if (!EndUpdateResource(hUpdateRes, FALSE)) 
{ 
    ErrorHandler("Could not write changes to file."); 
} 
 
// Clean up. 
if (!FreeLibrary(hExe)) 
{ 
    ErrorHandler("Could not free executable."); 
} 
Last edited on
I tried to change a bit of this code to get it work with strings, but it still do not change the resource and no Errors are reported.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
HANDLE hResource;
    HRSRC hResLoad;     // handle to loaded resource 
    HANDLE hExe;        // handle to existing .EXE file 
    HRSRC hRes;         // handle/ptr. to res. info. in hExe 
    HANDLE hUpdateRes;  // update resource handle 
    char *lpResLock;    // pointer to resource data 
    BOOL result;
     
    lpResLock = "CHANGEDVALUE"; 
    if (lpResLock == NULL) 
    { 
        cout<<"Could not lock dialog box.\r\n"; 
    } 
     
    hUpdateRes = BeginUpdateResource("C:\\test.exe", FALSE); 
    if (hUpdateRes == NULL) 
    { 
        cout<<"Could not open file for writing.\r\n"; 
    } 
     
    // Add the dialog box resource to the update list. 
    result = UpdateResource(hUpdateRes,       // update resource handle 
         RT_STRING,                   // change dialog box resource 
         MAKEINTRESOURCE(40000),                  // dialog box name 
         MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),  // neutral language
         lpResLock,                   // ptr to resource info 
         sizeof(lpResLock)); // size of resource info. 
    
    if (result == FALSE) 
    { 
        cout<<"Could not add resource.\r\n"; 
    } 
     
    // Write changes to FOOT.EXE and then close it. 
    if (!EndUpdateResource(hUpdateRes, FALSE)) 
    { 
        cout<<"Could not write changes to file.\r\n"; 
    } 


The Resource i try to Update look uncompiled like this:
1
2
3
4
5
6
7
8
9
#include "resource.h"

STRINGTABLE

{
    IDS_STRING1                   "Value1"
    IDS_STRING2                   "Value2"
    IDS_STRING3                   "Value3"
}


And the header:
1
2
3
4
5
6
7
#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif

#define IDS_STRING1                             40000
#define IDS_STRING2                             40001
#define IDS_STRING3                             40002 


Update: I changed the part MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)
to
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) and this creates a new Table inside the File but with a null value. How do i have to convert the string?
Last edited on
Topic archived. No new replies allowed.