Link to home
Start Free TrialLog in
Avatar of ebi1
ebi1Flag for Israel

asked on

Delete resources of other EXE

Hey all,

I want to build a program that will delete the resources of another EXE file.
Meaning, i want program A to delete program B's resources like cursors(all), Bitmap Folder and String Table numbers like 3851, 4094 and so on.

Like other programs (like Resource Hacker) are doing but i want it to be done automatically.

A. How to do it...?
B. Are the strings in the String Table always the same.
What i mean is if String number 3829 contain "X" - will it ALWAYS contain "X" ?

Please give a good code example.

ThanX
Avatar of Slavak
Slavak

If you want delete all resources from the file, you can use:
  H := BeginUpdateResource(FileName, True);
  EndUpdateResource(H, False);


 
listening
ebi1,

You can use this code for Win2000/NT/XP:
(place Button and OpenDialog on the form)

procedure TForm1.Button1Click(Sender: TObject);
var
  h: THandle;
  Victim: String;
begin
  // Resource name:
  Victim:='MYBMP';
  OpenDialog1.Filter:='Executible files|*.EXE;*.DLL';
  if not OpenDialog1.Execute then Exit;
  h := BeginUpdateResource(PChar(OpenDialog1.FileName), LongBool(FALSE));
  if h <> 0 then
    if not UpdateResource(h,                // Resource handle
                          RT_BITMAP,        // Resource type
                          PChar(Victim),    // Resource name
                          LANG_NEUTRAL,     // Language
                          nil, 0) then      // Delete resource
       begin
         MessageDlg('Oops: '+IntToStr(GetLastError), mtError, [mbOk], 0);
         EndUpdateResource(h, LongBool(TRUE));
       end
       else EndUpdateResource(h, LongBool(FALSE))
  else EndUpdateResource(h, LongBool(TRUE));
end;



This example is for RT_BITMAP type, but you can
replace it with whatever type you like. Available
types are listed in WinAPI help; search for
UpdateResource function.

Also, if you don't know names of the exe resources,
I can post some code that enumerates resources.
Avatar of ebi1

ASKER

Hey there,

sorry about the delay.

can you also answer about question B of the same question?
"
B. Are the strings in the String Table always the same?
What i mean is if String number 3829 contain "X" - will it ALWAYS contain "X" ?
"

Cynna, if RT_BITMAP is for Bitmaps, what are the names for Icons, StringTables and so on...?

ThanX
What you mean "always"?
There is constants for starndard resources:


RT_ACCELERATOR     Accelerator table
RT_ANICURSOR     Animated cursor
RT_ANIICON     Animated icon
RT_BITMAP     Bitmap resource
RT_CURSOR     Hardware-dependent cursor resource
RT_DIALOG     Dialog box
RT_FONT             Font resource
RT_FONTDIR     Font directory resource
RT_GROUP_CURSOR     Hardware-independent cursor resource
RT_GROUP_ICON     Hardware-independent icon resource
RT_ICON             Hardware-dependent icon resource
RT_MENU             Menu resource
RT_MESSAGETABLE     Message-table entry
RT_RCDATA     Application-defined resource (raw data)
RT_STRING     String-table entry
RT_VERSION     Version resource
Avatar of ebi1

ASKER

Hey Slavak,

For example :

Entry 3829 from the String Table (RT_STRING) shows:"
STRINGTABLE
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
{
61254,      "Details"
61255,      "Could not load '%s' library"
61256,      "File specified is not an executable file, dynamic-link library, or icon file"
61257,      "Function not yet implemented"
61258,      "Previous Month|"
61259,      "Previous Year|"
61260,      "Next Month|"
61261,      "Next Year|"
61262,      "Select a Date"
61263,      "All files (*.*)|*.*"
}
"

So will 3829 ALWAYS contain the SAME text?

ThanX
Always for same executable file.
Avatar of ebi1

ASKER

I wrote about 3829 but i mean for all other resource types.

Cynna gave the code for Win 2k/NT/XP and for Bitmaps, so what is the code for Win 9x/ME and how do i delete resources for other types like RT_GROUP_CURSOR?
can i delete all RT_GROUP_CURSOR together and not 1 after the other for the same type?

I don't want to delete ALL the resources.

ThanX
Avatar of ebi1

ASKER

Hey Slavak,

You said :"Always for same executable file"

Does it means that 3829 (for example) will be different in other EXE...?
If you don't want delete all resources from the file, you should enumerate them and delete one by one like in Cynna comment.

Windows API functions for update file resources works only on Win NT/2000 systems. If you want it on Win 9x system it much more difficult. You can look in Delphi ResExplor example to see, how to READ the resources without API.
--- Does it means that 3829 (for example) will be different in other EXE...?

Sure, all executables have its own resources. But, for example, if you check programs from same compiler, you can find some common resources.  
Avatar of ebi1

ASKER

What's with the ResExplor? everytime i try to open a file i get a message "range check error"...
Does it also show how to delete a type in a resouce?

I didn't understand your last comment:
Did you meant that if i see 2 programs made on different compilers but both have resource number 4091(for the example) then 4091 on compiler A will look different then 4091 on compiler B...?

ThanX
You can get it on same compiler too.

Generally, different executable files have DIFFERENT
resources.

"ResExplor" delphi demo project that can read and show resources stored in the executable files not using Windows API.

Regards.
ebi1,


> B. Are the strings in the String Table always the same?

Yes they are, for the same exe file. Different exe's and dll's have different resources.
If they happen to be the same to a degree, it's a matter of luck, not a rule.




> Cynna gave the code for Win 2k/NT/XP and for Bitmaps, so what is the code for Win 9x/ME...

For Win9x/ME, my advice is: forget it.

Deleting/replacing resources for Win9x/ME requires you to hack into exe file itself.
This requires knowledge of PE file format, which is a whole different story. This
subject is a *much* more complex matter then doing it in NT based systems.




> ... can i delete all RT_GROUP_CURSOR together and not 1 after the other for the same type?

Well, directly (using dedicated API) - no. You can delete only one resource at the time.

But, there is a workaround: you iterate through all resources of the RT_GROUP_CURSOR
type, and delete them. You can use EnumResourceNames() function, with RT_GROUP_CURSOR type.
You'll get the names of all resources of this type in a callback function, one by one.
There you can call code I posted previously, or simply save them and delete them later.
If you get stuck with enumeration, I could write you a simple example.
Avatar of ebi1

ASKER

Yes Cynna, please give me an example.

Cynna and Slavak you have been VERY helpful so i want to give you both the credit/points

Thank you
ASKER CERTIFIED SOLUTION
Avatar of Cynna
Cynna

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ebi1

ASKER

Thank you Cynna.

You REALY helped me a lot.

Eitan