The WinAPI function AttachConsole always returns true when the program is compiled as 64-bit.

First of all, I've declared the function as following:

function AttachConsole(dwProcessId: DWORD): Bool; stdcall; external KERNEL32 name 'AttachConsole';

Then I call my function:

if AttachConsole(DWORD(-1)) then

This works fine when compiled as a 32-bit application, but when compiled as 64-bit it always returns true.

The documentation doesn't mention doing something special for a 64-bit application.

How to reproduce:

  1. Create a new VCL Application
  2. Set Target platform to Win64
  3. Edit the DPR file to look like this:

program Project1;

  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

function AttachConsole(dwProcessId: DWORD): Bool; stdcall; external KERNEL32 name 'AttachConsole';

  if AttachConsole(DWORD(-1)) then
    writeLN('Hello world');

  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);

When running under Win64, AttachConsole retruns true even when run from Explorer.

  • 1
    Maybe, just maybe, the 64-bit compiler does something weird with DWORD(-1) - have you tried it with $FFFFFFFF? – 500 - Internal Server Error Apr 17 at 11:14
  • Yes same result: False positive. – Jens Borrisholt Apr 17 at 11:16
  • more useful will be if you attach binary code instead src – RbMm Apr 17 at 11:40

From the documenation link, dwProcessId [in] can take two types of values - either a PID of the target process or the special argument :


Use the console of the parent of the current process.

Here you are using the value ATTACH_PARENT_PROCESS.

In the case of a 64-bit debug, it appears that the IDE is creating a console for the 64-bit debugger, which hosts the debugged application as a child, and so electing to attach to the parent console succeeds.

For a 32-bit debug session the application is spawned as a child of the IDE. We can guess that this is because the IDE itself is a 32-bit application and can hook into the 32-bit process directly while the 64-bit application must be attached to a 64-bit debugger.

Using process explorer we can see the difference in the proces hierarchy when launching the process for debugging within the IDE :

enter image description here

enter image description here

Here we can see the 64-bit process is hosted as a child in the debugger kernel while the 32-bit process is not.

Running the (64-bit) application outside of the debugger produces the expected result where AttachConsole(ATTACH_PARENT_PROCESS) fails.

  • Sorry my bad. My question wasn't clear. You should use a GUI program. I've updated the question. – Jens Borrisholt Apr 17 at 11:38
  • 1
    @JensBorrisholt I've updated the answer. I think this is what is going on. – J... Apr 17 at 12:20
  • Ok so in Short: It won't work under 64 bit when running from the debugger – Jens Borrisholt Apr 17 at 12:25
  • 1
    @JensBorrisholt I suppose, although it's not clear what "work" means - in a 64-bit debug there will always be a parent console available to attach to. If you need to distinguish this particular console from other potentially present parent consoles then that may be possible, of course. – J... Apr 17 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.