Recent

Author Topic: How to get pid & handle of current application.  (Read 34111 times)

anna

  • Sr. Member
  • ****
  • Posts: 426
How to get pid & handle of current application.
« on: September 05, 2012, 06:15:00 am »
I need to know a handle of current process (project1.exe). Is there any way without using enumprocesses and CreateToolhelp32Snapshot?

Function createprocess returns handle. So when my application is running , it must already know handle . But I did not fing application.handle.

I need to terminate all processes project1.exe except current process. So everything (path, process name, window class ) is the same and so it's imposible to use enumetation of processes.
« Last Edit: September 05, 2012, 06:23:36 am by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to get pid & handle of current application.
« Reply #1 on: September 05, 2012, 09:14:36 am »
there is 1 function in system unit named GetProcessID that returns the current proccess id although I do not understand why you need it to terminate the current process when application.terminate will do the job

Sorry I should have paid attention when reading the 1st  time
« Last Edit: September 05, 2012, 09:51:38 am by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

v.denis

  • Guest
Re: How to get pid & handle of current application.
« Reply #2 on: September 05, 2012, 09:19:13 am »
I guess

Code: [Select]
var
  hdl: THandle;
  pid: DWORD;
begin
  hdl := GetCurrentProcess;
  pid := GetCurrentProcessId;

But to close all other processes you have to scan processes with CreateToolhelp32Snapshot
« Last Edit: September 05, 2012, 09:26:16 am by v.denis »

v.denis

  • Guest
Re: How to get pid & handle of current application.
« Reply #3 on: September 05, 2012, 09:39:32 am »
I think here's actually what you need:

Code: [Select]
uses
  TlHelp32;

procedure DeleteOtherProcesses;
var
  hShot: THandle;
  pe: TProcessEntry32;
  curPid: dword;
  hProc: THandle;
begin
  hShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if hShot <> INVALID_HANDLE_VALUE then
  begin

    curPid := GetCurrentProcessId;

    try
      ZeroMemory(@pe, sizeof(pe));
      pe.dwSize := sizeof(pe);
      if Process32First(hShot, pe) then
        repeat
          if LowerCase(pe.szExeFile) = 'project1.exe' then
          begin
            if pe.th32ProcessID <> curPid then
            begin
              hProc := OpenProcess(PROCESS_TERMINATE, false, pe.th32ProcessID);
              if hProc <> 0 then
              begin
                TerminateProcess(hProc, 0);
                CloseHandle(hProc);
              end
              else
                  ; // failed
            end;
          end;
        until not Process32Next(hShot, pe);
    finally
      CloseHandle(hShot);
    end;
  end;
end;

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: How to get pid & handle of current application.
« Reply #4 on: September 05, 2012, 01:42:24 pm »
GetCurrentProcess returns $FFFFFFFF always. It does not equal real handle and it is not valid process handle at all. MDSN says that GetCurrentProcess gives (handle -1). So real handle must eqaual GetCurrentProcess+1. But if so , real handle =0.  It's imposible.
GetCurrentProcessID works right , so i can now use

handle = OpenProcess(
 PROCESS_ALL_ACCESS,
 FALSE,
 GetCurrentProcessID);


But why GetCurrentProcess does not work??




I'm using next code. This code close current project
Code: [Select]
procedure TStarsServcDlg.actDeleteUpdate(Sender: TObject);
var
   hour2ready,min2ready,sec2ready:string;

label OLLYDBG0047529B_C02,OLLYDBG004751DB_C02,Quit_C02,OLLYDBG00475220_C02;
var hProcess ,hProcess2,hProcessID          : THandle;
    lpFilename_C44     : LPSTR;
    lphModule_C44      : PHMODULE;
    lpcbNeeded_C44     : DWORD;
    pProcessIds_C44    : LPDWORD;
    EAX,EDX,EDI        : DWORD;
    {opera6INI_path     : String;}
    P                  : Integer;

begin



     begin
       GetMem( lphModule_C44  , $4   );
       GetMem( lpFilename_C44 , $104 );lpFilename_C44^:=#$00;
       GetMem( pProcessIds_C44, $400 );
       EnumProcesses(pProcessIds_C44,$400,lpcbNeeded_C44);
       EAX:=lpcbNeeded_C44;
       EDX:=0;
       EAX:=EAX SHR 2;
       EDI:=Ofs(pProcessIds_C44^);

{r-------}  if EDX>=EAX then goto Quit_C02;
{|r----->} OLLYDBG004751DB_C02:
{||      }  hProcess:=OpenProcess(PROCESS_ALL_ACCESS,FALSE,pDWORD(EDI)^);
{||r-----}  if hProcess=0 then goto OLLYDBG0047529B_C02;
{|||r----}  if EnumProcessModules(hProcess,lphModule_C44,4,lpcbNeeded_C44) then goto OLLYDBG00475220_C02;
{||||    }  CloseHandle(hProcess);
{|||| r--}  goto OLLYDBG0047529B_C02;
{|||L--->} OLLYDBG00475220_C02:
{|||  |  }  GetModuleFileNameExA(hProcess,lphModule_C44^,lpFilename_C44,$104);
{|||  |  }
{|||  |  }  P:=0;
{|||  |  }  begin
{|||  |  }    P:=P or pos(upcase('\'+ExtractFileName(paramstr(0))),upcase(strpas(lpFilename_C44)))
{|||  |  }         or pos(upcase('/'+ExtractFileName(paramstr(0))),upcase(strpas(lpFilename_C44)));
{|||  |  }         hProcessID:=GetCurrentProcessID;
{|||  |  }         hProcess2:=OpenProcess( PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessID);
{|||  |  }    if hProcess=OpenProcess( PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessID) then P:=0;
{|||  |  }  end;
{|||  |  }
{|||  |  }  if (lpFilename_C44='')and(pDWORD(EDI)^<>0)and(pDWORD(EDI)^<>4) then inc(P);
{|||  |  }  while P<>0 do
{|||  |  }  begin
{|||  |  }   TerminateProcess(hProcess,0);
{|||  |  }   Break;
{|||  |  }  end;
{|||  |  }  CloseHandle(hProcess);
{||L->L->} OLLYDBG0047529B_C02:
{||      }  inc(EDX);
{||      }  EDI:=EDI+4;
{|L------}  if EDX<EAX then goto OLLYDBG004751DB_C02;
{L------>} Quit_C02:
{        }  FreeMem( pProcessIds_C44, $400 );
{        }  FreeMem( lpFilename_C44 , $104 );
{        }  FreeMem( lphModule_C44  , $4   );
     end;

end; 

I think, that OpenProcess function returns different handle each call. I my loop i have already use OpenProcess. So second OpenProcess (even if PID is same) gives another handle . 
« Last Edit: September 05, 2012, 02:20:14 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: How to get pid & handle of current application.
« Reply #5 on: September 05, 2012, 03:01:11 pm »
Quote
GetCurrentProcess returns $FFFFFFFF always. It does not equal real handle and it is not valid process handle at all. MDSN says that GetCurrentProcess gives (handle -1). So real handle must eqaual GetCurrentProcess+1. But if so , real handle =0.  It's imposible.
The MSDN says that GetCurrentProcess returns (HANDLE)-1 which is the constant -1 cast to the HANDLE type. So yes, it always returns the pseudo handle $FFFFFFFF

v.denis

  • Guest
Re: How to get pid & handle of current application.
« Reply #6 on: September 05, 2012, 03:05:56 pm »
GetCurrentProcess works only inside current process, e.g.
TerminateProcess(GetCurrentProcess, 0) will close current process

@anna
your code looks like ripped from ollydbg :)
« Last Edit: September 05, 2012, 03:12:45 pm by v.denis »

Knipfty

  • Full Member
  • ***
  • Posts: 232
Re: How to get pid & handle of current application.
« Reply #7 on: September 05, 2012, 03:17:04 pm »
anna,

Thinking a little outside the box, couldn't you use inter process communication and have the process that needs to remain, send a kill message to the other processes?  Then just let the other processes kill themselves.  I haven't done this myself, but I believe the tools are available in Lazarus.

Knipfty
64-bit Lazarus 2.2.0 FPC 3.2.2, 64-bit Win 11

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: How to get pid & handle of current application.
« Reply #8 on: September 05, 2012, 03:30:43 pm »

TerminateProcess(GetCurrentProcess, 0) will close current process

And so what? I need comparison , not a terminate current process. In my process it return wrong handle. But as you said , in current process it must work.
WinXP SP3 Pro Russian 32-bit (5.1.2600)

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: How to get pid & handle of current application.
« Reply #9 on: September 05, 2012, 05:03:15 pm »
anna,

Thinking a little outside the box, couldn't you use inter process communication and have the process that needs to remain, send a kill message to the other processes?  Then just let the other processes kill themselves.  I haven't done this myself, but I believe the tools are available in Lazarus.

Knipfty
Nope. I want instantly process termination. If I would use process communication, it will take long time (seconds). And how I know the processes are terminated? To know than I will have to use enumerate process loop. It also slows my app down.

I know that terminate process is not recommended (but I don't know why). In my case there is no loosing data, so I can terminate processes.
« Last Edit: September 05, 2012, 05:05:52 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: How to get pid & handle of current application.
« Reply #10 on: September 05, 2012, 05:07:07 pm »

your code looks like ripped from ollydbg :)
Nope. Labels' names may be different.
WinXP SP3 Pro Russian 32-bit (5.1.2600)

TurboRascal

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: How to get pid & handle of current application.
« Reply #11 on: September 06, 2012, 09:53:13 pm »
I know that terminate process is not recommended (but I don't know why).

I think it's simply regarded as bad practice in general...
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

 

TinyPortal © 2005-2018