22

While investigating external exception C0000006 errors reported by some users running a Delphi 7 application on a Windows 2008 terminal server, I found several questions on this and related issues already. I'm wondering if anyone has a definitive list of these settings that would be appropriate for a standard Delphi 7 database application running on a terminal server.

The questions I've looked at include:

So far from reading these I'm thinking I should add the following to the .dpr file:

const
  IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE = $8000;
  // Following are already defined in D7's Windows.pas
  IMAGE_FILE_RELOCS_STRIPPED               = $0001;
  IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP       = $0400;
  IMAGE_FILE_NET_RUN_FROM_SWAP             = $0800;

{$SetPEOptFlags IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE}

{$SetPEFlags IMAGE_FILE_RELOCS_STRIPPED 
  or IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP 
  or IMAGE_FILE_NET_RUN_FROM_SWAP}

Edit: Stripping the relocation section is probably unnecessary, so here's the revised version:

const
  IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE = $8000;
  // Following are already defined in D7's Windows.pas
  IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP       = $0400;
  IMAGE_FILE_NET_RUN_FROM_SWAP             = $0800;

{$SetPEOptFlags IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE}

{$SetPEFlags IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP 
  or IMAGE_FILE_NET_RUN_FROM_SWAP}
Community
  • 1
  • 1
dougwoodrow
  • 1,111
  • 12
  • 22
  • 2
    The main one is the TSAWARE flag. I'm not sure why you want IMAGE_FILE_RELOCS_STRIPPED - Are you combining that with stripreloc because you want to make the EXE smaller? Why would that be specific to Terminal Server? – Warren P Jan 17 '13 at 19:33
  • @Warren: Good point, it's not specific to terminal services but yes reducing the EXE size seemed like a good thing to me. Is there any reason NOT to strip the relocation section? – dougwoodrow Jan 17 '13 at 21:38
  • 1
    You inhibit windows from relocating the executable, which, on an NT operating system is less of a problem than on the ancient Win95/98 systems. I mostly think of it as a "thing you should only do if you HAVE to". For installers, it makes sense. For a regular app, it's just one more thing that could be part of a chain of badness. In the era of terabyte hard drives, I find it odd that anyone cares if an EXE is 2 megs larger, if the cure involves running a closed source tool that modifies what my linker built. Who do ya trust, in other words. – Warren P Jan 18 '13 at 14:18
  • 1
    According to [Jordan Russell](http://www.jrsoftware.org/striprlc.php) the relocation section "is actually not necessary since EXEs never get relocated." But I accept your point, leaving the relocation section in is unlikely to cause any problems. – dougwoodrow Jan 18 '13 at 17:20
  • 3
    You are safe to strip relocation table for an exe – David Heffernan Jun 10 '13 at 19:24
  • Just for completeness, David's statement is correct even for Windows 9x. The reason is that the EXE is always the first module to be loaded into a new process' memory space by the loader, except for a couple of shared, pre-loaded OS modules, like kernel32.dll, which are always loaded at a high address. This means that the default load address of 0x400000 is always available, so there is never any reason for the OS to have to relocate the EXE image. – 500 - Internal Server Error Jan 15 '14 at 22:46
  • Just a keyword: ASLR – chksr Jan 23 '18 at 08:47
  • @chksr: yes, but ASLR wasn't supported in versions of Delphi before Delphi 2007, AFAIK. https://community.embarcadero.com/blogs/entry/delphi-2007-supports-aslr-and-nx-33777 – dougwoodrow Jun 02 '21 at 18:12

1 Answers1

9
{$SetPEOptFlags IMAGE_DLLCHARACTERISTICS_TERMINAL_ SERVER_AWARE}
{$SetPEFlags IMAGE_FILE_RELOCS_STRIPPED   
  or IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP   
  or IMAGE_FILE_NET_RUN_FROM_SWAP}

is fine for your needs. I suspect that IMAGE_FILE_NET_RUN_FROM_SWAP is what you need to deal with C0000006. That error typically happens when you run from a network drive and the drive is not able to satisfy a page in request for the executable.

The other PE flags are fine for you either way. I would have it as you do.

As for whether or not IMAGE_DLLCHARACTERISTICS_TERMINAL_ SERVER_AWARE is right, only you know. Does you app meet the requiresments to be considered a non-legacy app for terminal services, as documented on MSDN? If so, use IMAGE_DLLCHARACTERISTICS_TERMINAL_ SERVER_AWARE.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks David, and good point about the /TSAWARE setting. I haven't had any reports of the C0000006 errors since introducing these settings. – dougwoodrow Jan 17 '14 at 19:42