Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2001-04

Re: Obtaining Windows API handle of a new console. (Marco van de Voort)

1 message · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Sergey Kosarevsky

Hello fpc-pascal-admin,

you wrote
> I've looked all through the Windows API help system but I've failed to find > a method for obtaining the handle of the current console process.

The Win32 API provides no direct method for obtaining the window handle associated with a console application. However, you can obtain the window handle by calling FindWindow(). This function retrieves a window handle based on a class name or window name. Call GetConsoleTitle() to determine the current console title. Then supply the current console title to FindWindow(). Because multiple windows may have the same title, you should change the current console window title to a unique title. This will help prevent the wrong window handle from being returned.

> Eg. I start prog.exe. I want prog.exe to find out the handle that points to > itself, that Windows has created for it.
Function GetConsoleHwnd:hWnd;
Const Title='unique_title'#0;
Var hwndFound:hWnd;
Begin
   SetConsoleTitle(pChar(@Title[1]));
   Sleep(40);             { <- let Windows update the title }
   hwndFound:=FindWindow(Nil,pChar(@Title[1]));
   Exit(hwndFound);
End;

---------------------------------
Sergey Kosarevsky
[FC Development Team]
http://www.freecommander.boom.ru

Reply by email