Hide your applicacion from tasklist (CRTL-ALT-DEL dialog)  

Send By: Q3 Team
Web : http://www.q3.nu
Email: dlib@q3.nu
Date: 20/02/01

Tip accessed 960 times

 


First, put this declaration in the implementation of your form:


 function RegisterServiceProcess (dwProcessID, dwType: DWord) : DWord;
   stdcall; external 'KERNEL32.DLL';



Now, to hide it:


   RegisterServiceProcess(GetCurrentProcessID,1);



Show it again:


   RegisterServiceProcess(GetCurrentProcessID,0);




Lord Trancos communicates me that in Windows 2000 that function doesn't exist in Kernel32.DLL ... so it will fail you the trick.
Take a look to the trick:

[413] Know if a DLL contains a given function

Here you have a new version of this trick, in the one that the procedure RegisterServiceProcess is declared dynamically, avoiding the problem of if it exists or not in Windows the procedure...

First, we should register the type of the function that we want to register, so that Delphi controls its parameters well.
We will make it in the interface of the form:

 unit Unit1;

 interface

 uses
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
   StdCtrls;


 {Esta linea/This Line}
 type
   TRegisterService = function (dwProcessID, dwType:DWord):Dword;stdcall;



Now, we need a global variable to the form to keep the address of the procedure.
For example, we will include it in the private part of the form:


 type
   TForm1 = class(TForm)
     Button1: TButton;
     Button2: TButton;
     procedure Button1Click(Sender: TObject);
     procedure FormCreate(Sender: TObject);
   private
     { Private declarations }
     RegisterServiceProcess: TRegisterService;
   public
     { Public declarations }
   end;



Now, we need to assign the address to the procedure before being able to use it.
We will make it in the event OnCreate of the form. Notice that we will use a function that checks if the procedure exists or not, generating an exception (that can capture or to allow it to happen) if the procedure doesn't exist...:


 procedure TForm1.FormCreate(Sender: TObject);

   function AsignaProcedure(EnQueDll,Procedimiento: string):TFarProc;
   var
     MangoProc,
     MangoLib    : THandle;

   begin
     Result:=nil;
     MangoLib:=GetModuleHandle(Pchar(EnQueDll));
     if MangoLib <> 0 then
       Result:=GetProcAddress(MangoLib,Pchar(Procedimiento));

     {Si la DLL o la procedure no esta disponible... error}
     if (MangoLib=0) or (Result=nil) then
       raise Exception.create( Procedimiento+
                               ' en '+
                               EnQueDll+
                               ' no encontrado.');
   end;

 begin
   try
     @RegisterServiceProcess:=AsignaProcedure( 'KERNEL32.DLL',
                                               'RegisterServiceProcess');
   except
     ShowMessage('Error, funcion RegisterServiceProcess no encontrada...');
   end;
 end;



If you notice, in this example I capture the exception, giving the message that to my it gives me the desire. If removes the ' ShowMessage' could make it silent... that is to say that the user neither it would find out that that problem exists.
It is convenient that we go getting used to the manipulation of errors with exception, at the end they remove you a lot of work.

Finally... we have left the trick in yes, that is to say... to call to the function that so much work has given us to declare:

 procedure TForm1.Button1Click(Sender: TObject);
 begin
   {Ocultar la aplicacion.../Hide the application...}
   if Assigned(RegisterServiceProcess) then
     RegisterServiceProcess(GetCurrentProcessID,1);
 end;



If you notice, I use the If Assigned to make sure that it was declared, to avoid surprises...


Updated at 16/03/2000


Updated at 20/02/2001 Typographic error (Thanks to Lucas)