peflagsTop

Sets PE Header Flags. Must be a 32 bit PE file, PE64 is not supported.

UsageTop

peflags version 0.2, (c) 2000 Petr Vones
                     (c) 2012 Stefan Ascher
Syntax : peflags <filename> [-CD][-NET][-LAA] [-INFO]
Options: CD   - on removable media, copy and run from the swap file.
         NET  - on the network, copy and run from the swap file.
         LAA  - set IMAGE_FILE_LARGE_ADDRESS_AWARE flag.
         INFO - do not change (reset) flags, display current flags only.

If the IMAGE_FILE_LARGE_ADDRESS_AWARE header flag is set, even 32 Bit programs get more than 2 GB of Memory on a 64 Bit Windows. But make sure that program does not mix signed and unsigned Integers, it must really support addresses greater than 2^32 or it will crash.

DownloadTop

SourceTop

The source is very easy, basically it uses the ImageHlp unit:

program peflags;
{$APPTYPE CONSOLE}

// Original code http://cc.embarcadero.com/Item/15402

uses
  Windows, ImageHlp, SysUtils;

const
  IMAGE_FILE_LARGE_ADDRESS_AWARE = $0020;

procedure ChangeFlags;
var
  FileName, FlagsText: string;
  CDFlag, NetFlag, DisplayOnly, LargeAddress: Boolean;
  LI: LoadedImage;
begin
  FileName := ParamStr(1);
  CDFlag := FindCmdLineSwitch('cd', ['-', '/'], True);
  NetFlag := FindCmdLineSwitch('net', ['-', '/'], True);
  LargeAddress := FindCmdLineSwitch('laa', ['-', '/'], True);
  DisplayOnly := FindCmdLineSwitch('info', ['-', '/'], True);
  WriteLn('FileName: ', FileName);

  if not MapAndLoad(PChar(FileName), nil, @LI, False, DisplayOnly) then begin
    if GetLastError = ERROR_SUCCESS then
      WriteLn('Error: File is not a valid PE Executable.');
    Halt(1);
  end else begin
    with LI.FileHeader^.FileHeader do begin
      if not DisplayOnly then begin
        Characteristics := Characteristics and
          not (IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP or IMAGE_FILE_NET_RUN_FROM_SWAP or IMAGE_FILE_LARGE_ADDRESS_AWARE);
        if CDFlag then
          Characteristics := Characteristics or IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP;
        if NetFlag then
          Characteristics := Characteristics or IMAGE_FILE_NET_RUN_FROM_SWAP;
        if LargeAddress then
          Characteristics := Characteristics or IMAGE_FILE_LARGE_ADDRESS_AWARE;
      end;
      FlagsText := '';
      if Characteristics and IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP <> 0 then
        FlagsText := 'CD ';
      if Characteristics and IMAGE_FILE_NET_RUN_FROM_SWAP <> 0 then
        FlagsText := FlagsText + 'NET ';
      if Characteristics and IMAGE_FILE_LARGE_ADDRESS_AWARE <> 0 then
        FlagsText := FlagsText + 'LAA';
      WriteLn(Format('Current image flags: [%s]', [Trim(FlagsText)]));
    end;
    if not(UnMapAndLoad(@LI)) then
      Halt(1);
  end;
end;

begin
  WriteLn('peflags version 0.2, (c) 2000 Petr Vones');
  WriteLn('                     (c) 2012 Stefan Ascher');
  if ParamCount > 0 then
    ChangeFlags
  else begin
    WriteLn('Syntax : peflags <filename> [-CD][-NET][-LAA] [-INFO]');
    WriteLn('Options: CD   - on removable media, copy and run from the swap file.');
    WriteLn('         NET  - on the network, copy and run from the swap file.');
    WriteLn('         LAA  - set IMAGE_FILE_LARGE_ADDRESS_AWARE flag.');
    WriteLn('         INFO - do not change (reset) flags, display current flags only.');
  end;
end.