Ver Mensaje Individual
  #7  
Antiguo 06-07-2007
l30 l30 is offline
Miembro
 
Registrado: jun 2007
Posts: 36
Reputación: 0
l30 Va por buen camino
Gracias

Gracias mensana, no sabia que Windows Vista avía cerrado la posibilidad de que un servicio pueda ejecutar una aplicación, bueno entonces e decidido pasar todo el código para el servicio y me pasa lo siguiente, le digo que me muestre una listado da las aplicaciones que están corriendo en mi maquina cada cierto tiempo.

Este es el listado que me muestra:

CSRSS.EXE TPUtilWindow 2240
SVCHOST.EXE HidPhoneNotifClass 816
CSRSS.EXE TPUtilWindow 2240
SVCHOST.EXE Tapi32WndClass 816

Entonces cuando le pongo al servicio que interactué con el sistema, me da el listado real, de las aplicaciones que están corriendo, pero no quiero que mi servicio interactué con el sistema.

Que puedo hacer para resolver este problema, por si es de ayuda, paso el código que me da, las aplicaciones que están corriendo en ese momento.

Gracias por adelantado.
***********************************************************************
Código Delphi [-]
unit Unit7;

interface
uses
 Windows, Dialogs, SysUtils, Classes, ShellAPI, TLHelp32, Forms, PsApi;
const
 SleepForReCheck=5000;
type TProcessInfo=record
 FileName: string;
 Caption: string;
 Visible: boolean;
 Handle: DWord;
 PClass: string;
 ThreadID: DWord;
 PID: DWord;
end;
 TProcessInfo2= array of TProcessInfo;    
var
 DateiList,CaptionList,VisibleList,HandleList,ClassList,ThreadIdList,PIDList: TStringList;
 ProcessInfo: TProcessInfo2;
 contador67: byte;
 cantidad:integer;
function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): Bool; stdcall;
function KillProcessByPID(PID: DWord): boolean;
function KillProcessByFileName(FileName: string; KillAll: boolean): boolean;
procedure GetProcessList;
function GetFileNameFromHandle(Handle: hwnd):string;
function IsFileActive(FileName: String): boolean;
function GetProcessName(ProcessId: DWORD): String;
implementation

uses Unit1;

procedure GetProcessList;
var
 i, Laenge: integer;
begin
   DateiList.Clear;
   HandleList.Clear;
   ClassList.Clear;
   CaptionList.Clear;
   VisibleList.Clear;
   ThreadIdList.Clear;
   PIDList.Clear;
   
   EnumWindows(@EnumWindowsProc, 0);
   Laenge:=DateiList.Count;
   SetLength(ProcessInfo,Laenge);
   for i:=0 to Laenge-1 do
      begin
         DateiList[i]:=UpperCase(DateiList[i]);
         with ProcessInfo[i] do
            begin
               FileName:=DateiList[i];
               Caption:=CaptionList[i];
               Visible:=VisibleList[i]='1';
               Handle:=StrToInt64(HandleList[i]);
               PClass:=ClassList[i];
               ThreadID:=StrToInt64(ThreadIdList[i]);
               PID:=StrToInt64(PIDList[i]);
               // cantidad de tipos metidos dentro del arreglo
               cantidad:=i;
            end;

      end;
end;

function IsFileActive(FileName: String): boolean;
var
 i: integer;
begin
result:=false;
if FileName='' then exit;
GetProcessList;
FileName:=UpperCase(ExtractFileName(FileName));
for i:=0 to Length(ProcessInfo)-1 do
begin
 if Pos(FileName,ProcessInfo[i].FileName)>0 then
 begin
  result:=true;
  break;
 end;
end;
end;
function GetFileNameFromHandle(Handle: hwnd):string;
var
 PID: DWord;
 aSnapShotHandle: THandle;
 ContinueLoop: Boolean;
 aProcessEntry32: TProcessEntry32;
begin
GetWindowThreadProcessID(Handle, @PID);
aSnapShotHandle := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
aProcessEntry32.dwSize := SizeOf(aProcessEntry32);
ContinueLoop := Process32First(aSnapShotHandle, aProcessEntry32);
while Integer(ContinueLoop) <> 0 do
begin
 if aProcessEntry32.th32ProcessID = PID then
 begin
  result:=aProcessEntry32.szExeFile;
  break;
 end;
 ContinueLoop := Process32Next(aSnapShotHandle, aProcessEntry32);
end;
CloseHandle(aSnapShotHandle);
end;

function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): Bool;
var
 Capt,Cla: array[0..255] of char;
 Datei: string;
 ident: dword;
begin
   GetWindowText(hWnd, Capt, 255);
   GetClassName(hwnd,Cla,255);
   ThreadIdList.Add(IntToStr(GetWindowThreadProcessId(hwnd,nil)));
   Datei:=GetFileNameFromhandle(hwnd);
   DateiList.Add(Datei);
   HandleList.Add(IntToStr(HWnd));
   inc(contador67);
   if contador67 >=10 then
      begin
         Sleep(1);
         contador67:=0
      end;
      
   if IsWindowVisible(HWnd) then
      VisibleList.Add('1') else VisibleList.Add('0');
   ClassList.Add(Cla);
   CaptionList.Add(Capt);
   GetWindowThreadProcessId(StrToInt(HandleList[HandleList.Count-1]),@ident);
   PIDList.Add(IntToStr(ident));
   Result:=true;
end;
function KillProcessByPID(PID : DWord): boolean;
var
 myhandle : THandle;
 i: integer;
begin
myhandle := OpenProcess(PROCESS_TERMINATE, False, PID);
TerminateProcess(myhandle, 0);
for i:=0 to SleepForReCheck do Application.ProcessMessages; //Genug Zeit geben
GetProcessList;
Result:=PIDList.IndexOf(IntToStr(PID))=-1;
end;
function KillProcessByFileName(FileName: string; KillAll: boolean): boolean;
var
 i: integer;
 FileFound: boolean;
begin
result:=false;
if FileName='' then exit;
FileName:=UpperCase(ExtractFileName(FileName));
result:=true;
GetProcessList;
if KillAll then
begin
 //Kill all
 FileFound:=false;
 repeat
  GetProcessList;
  FileFound:=false;
  for i:=0 to DateiList.Count-1 do
  begin
   if Pos(Filename,DateiList[i])>0 then
   begin
    FileFound:=true;
    break;
   end;
  end;
//  if ithen
  begin
   if not KillProcessByPID(StrToInt64(PIDList[i])) then
   begin
    result:=false;
    exit;
   end;
  end;
 until not FileFound;
end else
begin
 //Kill one
 for i:=0 to DateiList.Count-1 do
 begin
  if Pos(Filename,DateiList[i])>0 then break;
 end;
// if ithen
 begin
  if not KillProcessByPID(StrToInt64(PIDList[i])) then
  begin
   result:=false;
   exit;
  end;
 end;
end;
end;

function GetProcessName(ProcessId: DWORD): String;
var
  Process: THANDLE;
  Buffer: array[0..MAX_PATH] of Char;
begin
// ejemplo esta funcion se usa haci
// GetProcessName(PID));

  Process := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, FALSE,
    ProcessId);
  if (Process<>0) then
    begin
      if (GetModuleFileNameEx(Process, 0, Buffer,Sizeof(Buffer)-1)>0) then
        Result:= String(PChar(@Buffer));
      CloseHandle(Process);
    end;
end;

initialization
DateiList:=TStringList.Create;
HandleList:=TStringList.Create;
ClassList:=TStringList.Create;
CaptionList:=TStringList.Create;
VisibleList:=TStringList.Create;
ThreadIdList:=TStringList.Create;
PIDList:=TStringList.Create;
finalization
DateiList.Free;
HandleList.Free;
ClassList.Free;
CaptionList.Free;
VisibleList.Free;
ThreadIdList.Free;
PIDList.Free;
end.

Última edición por l30 fecha: 06-07-2007 a las 05:36:13.
Responder Con Cita