How to attach a resource file to an existing executable file?

前端 未结 3 1329
猫巷女王i
猫巷女王i 2021-01-02 06:33

I have a resource file(.RES) and i want to add it into an existing executable file without recompiling and using the IDE! is it possible?

Edit

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-02 07:00

    This is my answer : (Thank you PRUZ)

    Uses Classes, Windows, SysUtils, Dialogs;
    
    Type
      TBuffer = Array[0..0] of Byte;
      PBuffer = ^TBuffer;
    
    Var
      FS             : TFileStream;
      ResourceHandle : THandle;
      DataLength     : DWord;
      Data           : PBuffer;
      Ok             : Boolean;
    
    Begin
       ResourceHandle := BeginUpdateResource(pChar('d:\someexefile.exe'), False);
       IF (ResourceHandle <> 0) Then
       Begin
          FS := TFileStream.Create('d:\somebitmap.bmp', fmOpenRead);
          FS.Seek(0, soFromBeginning);
          DataLength := FS.Size;
          GetMem(Data, DataLength);
          FS.Read(Data^, DataLength);
          FS.Free;
    
          Ok := True;
          IF (not UpdateResource(ResourceHandle, RT_RCDATA, pChar('MyNewResource'), LANG_SYSTEM_DEFAULT{MakeLangID(LANG_NEUTRAL, SUBLANG_NEUTRAL)}, Data, DataLength)) Then Ok := False;
    
          IF (not EndUpdateResource(ResourceHandle, False)) Then Ok := False;
    
          IF (Ok) Then ShowMessage('Update of resources successful!')
             Else ShowMessage('Update of resources failed!');
    
          FreeMem(Data);
       End;
    End. 
    

    Reference : http://www.delphi3000.com

提交回复
热议问题