Code
Procedure AddSection(SecName:string;R_Size,V_Size:dword);
const
Ziro: Byte = $00;
var
VirAddr,PhyAddr,Sect_Align,SzofImg: DWORD;
i:integer;
Sec1,Sec2:Psection;
Secnum : word;
begin
if (length(SecName)>8) or (R_Size=0) or (V_Size=0) then exit;
Secnum:=nt.FileHeader.NumberOfSections;
Sect_Align:=nt.OptionalHeader.SectionAlignment;
Sec2:=PSection(SectionList[Secnum-1]);
PhyAddr:=sec2.physical_size+sec2.physical_offs;
VirAddr:=((Sec2.rva+Sec2.virtual_size+Sect_Align-1) div Sect_Align) * Sect_Align;
new(Sec1);
with Sec1^ do
begin
rva:=VirAddr;
virtual_size:=V_Size;
physical_size:=R_Size;
physical_offs:=PhyAddr;
StrPCopy(name,SecName);
flags:=$60000020;
end;
SzofImg:=nt.OptionalHeader.SizeOfImage;
nt.OptionalHeader.SizeOfImage:=SzofImg+sec2.virtual_size;
Move(Sec1^,Pointer(Dword(MEMPTR)+PE_HEADEROFFSET+$18+PE_NT_HEADERSIZE+ (Secnum*40))^,40);
nt.FileHeader.NumberOfSections:=Secnum+1;
SetFilePointer(PE,Sec1.physical_offs+Sec1.physical_size-R_Size, nil, 0);
for i:=1 to R_Size do WriteFile(PE, Ziro, SizeOf(Ziro), SzofImg, nil);
end;