Resource editing

Freddy1990
Freddy1990 used Ask the Experts™
on
Hi,

I'm looking for a way to swap out a bmp that's compiled into my program for another bmp after it's been compiled using Delphi code... However, I can't seem to find much information about editing resources. If anyone has some information or experience with this thay could share, it'd be appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kamaraj SubramanianApplication Support Analyst

Commented:
please use reshack to edit your program. once you edited, compile it again using reshack

dowload it from here

http://www.angusj.com/resourcehacker/

screenshot

http://www.angusj.com/resourcehacker/rh_shot.html

tutorial

www.askvg.com/tutorial-all-about-resource-hacker-in-a-brief-tutorial/
Freddy1990Software Engineer

Author

Commented:
I know I can use ResHacker, but I need to do it from my own code.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Freddy1990Software Engineer

Author

Commented:
HEh, you don't get it, I need to modify the resources of another exe file from my code...
Kamaraj SubramanianApplication Support Analyst

Commented:
but do you need in delphi code ?

or any other code is ok for you ?
Geert GOracle dba
Top Expert 2009

Commented:
unless you provide a compiler with your program like the borland compiler (to which you have no license = not included in a developer license), you can't do that

best thing is to separate the resources into a dll and then provide a other dll with the changes

what are you trying to accomplish ?
a bit more indept explanation can help
Freddy1990Software Engineer

Author

Commented:
It doesn't have to be Delphi code, it's preferred...
Hmm, a program of mine has some bitmaps compiled into it as resources, now some of these need to be replaced, which isn't a problem, but some users prefer to use an older version of the program. However, the bitmaps are crucial to the working of the program. So I'd like to write a program that can be ran to replace the depreciated bitmaps in the resources of the program with updated ones...
Geert GOracle dba
Top Expert 2009

Commented:
can't you recompile the older version ?
Delphi source of a res editor, should help you do what you want:

http://delphi.about.com/od/fullcodeprojects/ss/xnresourceedit.htm
Hi Freddy1990,

try using this code.

But you can't change the bitmap of the current executable program, you should pass the name of other module.

I Hope this help.
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;
 
type
  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
var
  BitmapResLanguage: Word;
 
function EnumResLangProc(hModule: HMODULE; lpType, lpName: PChar; wIDLanguage: Word;
  lParam: Longint): BOOL; stdcall;
begin
  BitmapResLanguage := wIDLanguage;
  Result := False;
end;
 
function ChangeBitmapResource(ModulePath, ResourceName, BitmapPath: String): Boolean;
var
  hUpdate: THandle;
  Data: Pointer;
  fs: TFileStream;
begin
  Result := True;
  fs := TFileStream.Create(BitmapPath, fmOpenRead);
  Data := AllocMem(fs.Size - SizeOf(TBitmapFileHeader));
 
  try
    try
      { Starts copying data excluding the bitmap header }
      fs.Position := SizeOf(TBitmapFileHeader);
      fs.Read(Data^, fs.Size);
 
      EnumResourceLanguages(GetModuleHandle(PChar(ModulePath)), RT_BITMAP, PChar(ResourceName),
                            @EnumResLangProc, 0);
 
      hUpdate := BeginUpdateResource(PChar(ModulePath), False);
 
      if hUpdate = 0  then
        RaiseLastOSError;
 
      if not UpdateResource(hUpdate, RT_BITMAP, 'BITMAP1', BitmapResLanguage, Data, fs.Size) then
        RaiseLastOSError;
 
      if not EndUpdateResource(hUpdate, False) then
        RaiseLastOSError;
    except
      Result := False;
    end;
  finally
    FreeMem(Data);
    fs.Free;
  end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  ChangeBitmapResource('C:\TEMP\Project1.exe', 'BITMAP1', 'C:\TEMP\another_bitmap.bmp');
end;
 
end.

Open in new window

Freddy1990Software Engineer

Author

Commented:
Thank you, this is exactly what I needed.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial