How to fix the error 0xc0000006 External Exception in a Delphi Application — Part 2

PE (portable executable) header flags for Delphi

Sergio Govoni
CodeX
Published in
2 min readApr 9, 2022

--

In this article I talked about the error known as External Exception associated to the code C0000006 (0xc0000006). It is a generic error that hides the true “low level error” associated the resource (file) the operating system is trying to load from the network.

The error is typically reported by customers who uses a Delphi Application in a Windows Terminal Server environment.

This solution solved the problem in my scenario: Add the PEflag IMAGE_FILE_NET_RUN_FROM_SWAP in all Delphi packages and projects (dpk, dpr, …) involved in the Application.

In this article I want to focus your attention on two details: the first one is related to the drag & drop of Delphi projects with PEflags into a project group and the second one is related to third party modules (BPL) used by the Application.

Drag & drop of Delphi project into a project group

We noticed an issue that occurs when a Delphi package DPKs that contain PEFlags are added to a Delphi project group by a drag & drop action (dragging the project of the package into the project group). In this case it may happen that the local path of Winapi.Windows.pas (C:\Program files (x86)\Embarcadero\…) is added into the DPROJ file. If the project is saved, the path is also added to the DPK file. To avoid this behavior it is necessary to activate the PEflag in a different way than the one I explained in the first part of this article. The tick is to use the value $0800 instead of the constant name IMAGE_FILE_NET_RUN_FROM_SWAP as you can see in the following piece of code:

{$SetPEFlags $0800}

I know this usage of PEFlags directive is not so clear as the use of constant IMAGE_FILE_NET_RUN_FROM_SWAP but it is clearer for Delphi IDE and this way does not require the addition of Winapi.Windows.pas to the contains section of the project.

To clean up projects where this behavior has already occurred, it is necessary open the DPROJ and DPK project files and remove the line:

<DCCReference Include = "C:\Program files (x86)\Embarcadero\Rad Studio\10.0\source\rtl\win\Winapi.Windows.pas"/>

Third party modules (BPL)

At a customer where the compilation directive was set on all Application modules, the error kept appearing. The case study led us to suspect that the error was due to external modules (not compiled by us) with no IMAGE_FILE_NET_RUN_FROM_SWAP flag, so we thought about using the EDITBIN tool to edit those external modules. This tool allows you to edit files such as BPL, EXE, etc. to add various options, including /SWAPRUN: NET (which corresponds to the IMAGE_FILE_NET_RUN_FROM_SWAP directive) without having to recompile the project. We also thought that it would be better to run EDITBIN right away not only on external BPL, EXE etc. but also on Delphi ones (rtl, vcl, etc.).

Nowadays we have installed the modules modified with EDITBIN at two customers. The external exception seems to be permanently resolved!

--

--

Sergio Govoni
CodeX
Writer for

CTO at Centro Software, Microsoft Data Platform MVP