How to fix the error 0xC0000006 External Exception in a Delphi Application

PE (portable executable) header flags for Delphi

Sergio Govoni
2 min readMay 7, 2021

--

If you develop (or manage) an application written in Delphi language and used in Windows Terminal Server environments, you have probably faced the error known with the code 0xC0000006 External Exception.

This is a rather generic error code that hides the true “low level error” associated with the loading a resource file from the network.

I spent some time debugging this error in a large Application developed in Delphi. The error is typically reported by Customers who use the Application in a Windows Terminal Server environment configured in a way that users don’t have direct access to the Application Server but only to the Server that provides the Terminal Services.

The Windows service that manages the Terminal Server, with the aim of optimizing the memory required for loading resources (files), “transfers” the memory of the loaded resource from one session to another. When the session that loaded the resources (the owner of the memory image) is released, the error 0xC0000006 External Exception can occur in other sessions that have received the resource as a memory image, without having loaded it.

This solution solved the problem in my scenario: Add the PEflag IMAGE_FILE_NET_RUN_FROM_SWAP in all Delphi projects (dpk, dpr, …) related to the Application modules as you can see here:

{$SetPEFlags IMAGE_FILE_NET_RUN_FROM_SWAP}

If isn’t already specified you have to add the uses of Winapi.Windows in the Delphi project.

If an EXE, a BPL or a DLL has the IMAGE_FILE_NET_RUN_FROM_SWAP bit set, the Windows loader copies the disk image to the swapfile and builds the in-memory representation out of the swap file when it loads the module for situations where the executable is on a network rather than a disk on the local machine.

The feedback received from the first “pilot” Customer was good, the 0xC0000006 External Exception errors are gone.

You can find additional details here:

I hope this solution is also decisive for you!

--

--

Sergio Govoni

CTO at Centro Software, Microsoft Data Platform MVP