Jump to content
Tuts 4 You

Delete Resource using UpdateResource ???


CodeExplorer

Recommended Posts

CodeExplorer

Delete Resource using UpdateResource ???
reference:
https://binaryworld.net/main/CodeDetail.aspx?CodeId=3778

Already tried:
bool deleteExisting = IsDlgButtonChecked(handle, IDC_CHECK22)==0;
HANDLE hUpdate = BeginUpdateResource(SecondFilename, deleteExisting);

int result1 = UpdateResource(hUpdate,    // update resource handle
    res_types2[i],                       // resource type
    MAKEINTRESOURCE(res_names2[i]),      // resource id
    0,  // neutral language
    NULL,  // ptr to resource info - NULL FOR DELETING
    0); // size of resource info - 0 for deleting

it always return int lasterror = GetLastError();
lasterror = 87

ERROR_INVALID_PARAMETER
87 (0x57)
The parameter is incorrect.

I don't want erasing all resources only specific one.
How to do it?
 

 

 

Link to comment

For the language, try passing the proper default/natural value via:

MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)

 

Link to comment
CodeExplorer

Thank you guys.

Quote

MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)

doesn't work for deleting, I enumerate languages instead:
 

    langs_count = 0;
    // Find the languages of all resources of type
    // lpType and name lpName.
    EnumResourceLanguages(hm2,
        res_types2[i],
        res_names2[i],
        (ENUMRESLANGPROC)EnumLangsFunc,
        0);

for (int j=0;j<langs_count;j++)
{
int result1 = UpdateResource(hUpdate,    // update resource handle
    res_types2[i],                       // resource type
    MAKEINTRESOURCE(res_names2[i]),      // resource id
    langs[j], // language
    NULL,  // ptr to resource info - NULL FOR DELETING
    0); // size of resource info - 0 for deleting

int laster = GetLastError();
}
int langs_count = 0;
WORD langs[20000];

//    FUNCTION: EnumLangsFunc(HANDLE, LPSTR, LPSTR, WORD, LONG)
//
//    PURPOSE:  Resource language callback
BOOL CALLBACK EnumLangsFunc(
    HMODULE hModule,  // module handle
    LPCTSTR lpType,  // address of resource type
    LPCTSTR lpName,  // address of resource name
    WORD wLang,      // resource language
    LONG lParam)     // extra parameter, could be
                     // used for error checking
{

    langs[langs_count] = wLang;
    langs_count++;
    return true;
}

No error (GetLastError) any more,
wondering what lang I should use for adding resources with UpdateResource ???,
some opinions will be great: I mean original language from source exe program or MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)  or other ???
 

Edited by CodeExplorer
Link to comment
CodeExplorer

One last mistakes I've made: you should free the LoadLibrary handle
hm2 = LoadLibraryEx(SecondFilename, NULL, DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE);
when edit was done you have to do:
FreeLibrary(hm2);  // we finished the work with hm2

Now finally works like it should.
I still have some question:
hUi = LoadLibraryExA(lpFileName,NULL,DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE);
1. What's the difference between LOAD_LIBRARY_AS_DATAFILE and LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE???
2. What's the use of LockResource api:  https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-lockresource???
HGLOBAL hResLoad = LoadResource(hm, hRes);
//LPVOID lpResLock = LockResource(hResLoad);
It works OK even without LockResource.
3. What lang I should use for adding resources with UpdateResource ???,
some opinions will be great: I mean original language from source exe program or MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)  or other ???
 

Link to comment
18 hours ago, CodeExplorer said:

wondering what lang I should use for adding resources with UpdateResource ???,

Generally depends on how the resource is being used. The language ids are generally for if the application is setup to support lookups based on the system language. Generally used for string tables or similar kinds of resources that can be easily setup to be loaded based on the system language information. If you are just injecting things to be used yourself, any should work fine, such as the default value for it. If the application expects a specific value or values for multiple resources, then you should follow what the application is expecting.

16 hours ago, CodeExplorer said:

1. What's the difference between LOAD_LIBRARY_AS_DATAFILE and LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE???

LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE opens the file with exclusive write access as well. So your process will hold a write lock to the file and be able to edit it.

16 hours ago, CodeExplorer said:

The naming of the API is misleading for what it does on more modern OS's since XP. It just returns a pointer to the resource data now, but does not actually lock it anymore. It's kept as-is and with its old name for compatibility since it was named/used from Windows XP. Generally before, it would load, lock, and return a pointer for the memory to the resource. But now it just returns the pointer.

16 hours ago, CodeExplorer said:

3. What lang I should use for adding resources with UpdateResource ???,

As mentioned above, that generally depends on what the application is doing with the resource and how the application is loading it. If they are using specific language id lookups to load the resource then you will need to match what they expect when replacing the originals. If you are just injecting new stuff you will load yourself at any other time, then you can use any you wish and just match your loading code to be what it is.

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...