Recent

Author Topic: Manipulating Console Window  (Read 28413 times)

j0x

  • Full Member
  • ***
  • Posts: 126
Manipulating Console Window
« on: February 23, 2011, 02:22:10 pm »
is their a way to manipulate the console window that is been called through TProcess? i want to disable the close button of the console window so that the abort button i put on the main form will be the only way to close the console window, i do not want to hide the console window too as it gives running info while it process something

im on windows specifically windows XP, thanks in advance for any help  :)


Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Manipulating Console Window
« Reply #1 on: February 23, 2011, 07:15:20 pm »
Only on Windows, TProcess have some special properties for the running process, but disable the close button is not possible as AFAIK Windows API itself doesn't provide such functionality. I assume you're writing a GUI app, so use pipes instead of real console and write the output to a component (TMemo for instance). Read the wiki for an example.

j0x

  • Full Member
  • ***
  • Posts: 126
Re: Manipulating Console Window
« Reply #2 on: February 24, 2011, 05:17:50 am »
ye im writing a GUI app a front end for a CLI app

and ye my first idea is to use pipes and redirect the standard output to TMemo in real time, i followed the wiki that has TMemoryStream instructions but i cannot get it to work it always fails, it seems their is a error on trying to load the streams in real time or TProcess with poUsePipes has problems with TMemo in real time

so now im asking to just disable the close button on the console window that is been called by the GUI app im making via TProcess call, actually i found a code to do this on Delphi here it is
http://www.vbforums.com/showthread.php?t=342885
and this one is more on target as it disables the close button of a console app on windows -> http://support.microsoft.com/kb/818361 but its in VB, i found another one http://www.codeguru.com/forum/showthread.php?p=443650 and its in C++

so i do not know how to transform this codes to Lazarus, so if you can help me with this one i appreciate it a lot!
« Last Edit: February 24, 2011, 05:33:10 am by ios »

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Manipulating Console Window
« Reply #3 on: February 24, 2011, 09:26:42 am »
ye im writing a GUI app a front end for a CLI app

and ye my first idea is to use pipes and redirect the standard output to TMemo in real time, i followed the wiki that has TMemoryStream instructions but i cannot get it to work it always fails, it seems their is a error on trying to load the streams in real time or TProcess with poUsePipes has problems with TMemo in real time

The article about capturing standard output and sending data to it's standard input worked fine for me on XP (http://wiki.lazarus.freepascal.org/Executing_External_Programs#How_to_redirect_output_with_TProcess).
I used threading though to make it run in parallel with the main thread.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

j0x

  • Full Member
  • ***
  • Posts: 126
Re: Manipulating Console Window
« Reply #4 on: February 24, 2011, 09:40:55 am »
The article about capturing standard output and sending data to it's standard input worked fine for me on XP (http://wiki.lazarus.freepascal.org/Executing_External_Programs#How_to_redirect_output_with_TProcess).
I used threading though to make it run in parallel with the main thread.

im new to this standard streams, ill try to explain this to another thread to make it more easy for me, and thanks for the help

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Manipulating Console Window
« Reply #5 on: February 24, 2011, 06:51:45 pm »
Quote
so now im asking to just disable the close button on the console window that is been called by the GUI app im making via TProcess call, actually i found a code to do this on Delphi here it is
http://www.vbforums.com/showthread.php?t=342885
and this one is more on target as it disables the close button of a console app on windows -> http://support.microsoft.com/kb/818361 but its in VB, i found another one http://www.codeguru.com/forum/showthread.php?p=443650 and its in C++
The Delphi code is for GUI app. I haven't tried neither the C++/VB code but it seems possible, maybe I didn't dig Windows API deep enough.

j0x

  • Full Member
  • ***
  • Posts: 126
Re: Manipulating Console Window
« Reply #6 on: February 25, 2011, 04:02:19 am »
is their some examples on how to program Windows API for Lazarus Free Pascal?

EDIT:

i found another method Windows API's GetConsoleWindow()
Code: [Select]
Windows 2000 and later support GetConsoleWindow(). Grab the HWND and disable the close button

source: http://www.codeguru.com/forum/showpost.php?p=712677&postcount=11

so i just need an example on how to program windows API on Lazarus
« Last Edit: February 25, 2011, 04:29:28 am by ios »

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1947
Re: Manipulating Console Window
« Reply #7 on: February 25, 2011, 11:42:52 am »
is their some examples on how to program Windows API for Lazarus Free Pascal?

I think there is no general answer to this question.
Show a specific problem.

j0x

  • Full Member
  • ***
  • Posts: 126
Re: Manipulating Console Window
« Reply #8 on: February 25, 2011, 04:00:18 pm »
is their some examples on how to program Windows API for Lazarus Free Pascal?

I think there is no general answer to this question.
Show a specific problem.

well i want to disable the console windows or command prompt windows close button, im on windows xp and this is the only idea i have "Windows 2000 and later support GetConsoleWindow(). Grab the HWND and disable the close button" so if anyone can give a good example of windows API programming using GetConsoleWindow() as an example then that will be awesome

PAEz

  • New Member
  • *
  • Posts: 29
Re: Manipulating Console Window
« Reply #9 on: February 25, 2011, 08:08:22 pm »
I tried to do something like this a LONG time ago in Delphi and it wasnt nice.
If I was you Id go with the other method of capturing the output, that worked nice for me as I remember.
If you want to try and get rid of the close button heres some links you might like to look at......
http://evilsoft.web.my-ct.de/getconsolewindow-with-delphi/
http://delphi.about.com/od/windowsshellapi/l/aa080304a.htm - compare the windows PID with the processid thats part of TProcess
http://www.esanu.name/delphi/Win%20API/Disabling%20close%20button%20of%20a%20window.html
Lazarus 0.9.29  Windows XP sp3
E6400 2gig ram 7900GTX 512meg

I have a shocking memory and am a crap coder, so take everything I say with a grain of doubt

Lush

  • New Member
  • *
  • Posts: 15
Re: Manipulating Console Window
« Reply #10 on: February 25, 2011, 11:20:11 pm »
Maybe you can find what you need inside that Delphi component?

http://www.delphi-central.com/components/delphi-spawn.aspx

Lush

j0x

  • Full Member
  • ***
  • Posts: 126
Re: Manipulating Console Window
« Reply #11 on: March 01, 2011, 04:01:00 am »
thanks Paez and Lush, i tried coding what i want but its not working here is the code

Code: [Select]
uses
   Windows;

function GetConsoleWindow(): HWND; stdcall; external 'kernel32.dll' name 'GetConsoleWindow';   

var
   hwndHandle: THANDLE;
   hMenuHandle: HMENU;

begin
hwndHandle := GetConsoleWindow();
   if (hwndHandle <> 0) then
   begin
      hMenuHandle := GetSystemMenu(hwndHandle, FALSE);
      if (hMenuHandle <> 0) then
         DeleteMenu(hMenuHandle, SC_CLOSE, MF_BYCOMMAND);
   end;       
end.

i got no compile errors and no run-time errors either, its just do not work the console window still have the close button enabled

j0x

  • Full Member
  • ***
  • Posts: 126
Re: Manipulating Console Window
« Reply #12 on: March 01, 2011, 08:09:08 am »
slight variation to the code but still no luck, it has no compile errors or run-time errors, it just does not work
Code: [Select]
uses
   Windows;

function GetConsoleWindow(): HWND; stdcall; external 'kernel32.dll' name 'GetConsoleWindow';  

var
   hMenuHandle: HMENU;  
begin
   hMenuHandle := GetSystemMenu(GetConsoleWindow(), FALSE);
   DeleteMenu(hMenuHandle, SC_CLOSE, MF_BYCOMMAND);
   SetConsoleTitle('Handbrake CLI!!!');
   DrawMenuBar(GetConsoleWindow());
end.
« Last Edit: March 01, 2011, 08:23:00 am by ios »

PAEz

  • New Member
  • *
  • Posts: 29
Re: Manipulating Console Window
« Reply #13 on: March 01, 2011, 03:20:54 pm »
I haven't really looked at it but I get the feeling that getconsolewindow will only work if its run in an app that is acutally running in a console window.
Thats why I gave those other links...I think your going to have to do it like...run a console window with tprocess and use those links to determine the windows handle and then disable its close button.
But like I also said, its actually better to go down the other avenue of capturing the output and redisplaying it in your app.  You posted about this elsewhere, how did that go?  Like I said I did this a long time ago in Delphi and that worked great for me....makes your app look more professional when it doesn't have dos box's popping up all the time and the output is allways in the same spot.
Lazarus 0.9.29  Windows XP sp3
E6400 2gig ram 7900GTX 512meg

I have a shocking memory and am a crap coder, so take everything I say with a grain of doubt

j0x

  • Full Member
  • ***
  • Posts: 126
Re: Manipulating Console Window
« Reply #14 on: March 02, 2011, 01:54:13 am »
I haven't really looked at it but I get the feeling that getconsolewindow will only work if its run in an app that is acutally running in a console window.
Thats why I gave those other links...I think your going to have to do it like...run a console window with tprocess and use those links to determine the windows handle and then disable its close button.

ye the TProcess is their too, i just add part of the codes that involves deleting the close button, after the TProcess.Execute i added this codes

But like I also said, its actually better to go down the other avenue of capturing the output and redisplaying it in your app.  You posted about this elsewhere, how did that go? 

yes it did go well eny answered it good, but my problem is that i have to make a TThread for the TProcess again which involves TTimer now so its overwhelming to start coding that one, that is why i asked this another option to disable the close button, but if i cannot make this thing work then no choice but to go to the redirection of stdout to TMemo

thanks for advices btw

 

TinyPortal © 2005-2018