Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   API de Windows (https://www.clubdelphi.com/foros/forumdisplay.php?f=7)
-   -   Service – Desktop (https://www.clubdelphi.com/foros/showthread.php?t=45432)

l30 03-07-2007 06:45:09

Service – Desktop
 
Antes de empezar quisiera agradecerles la ayuda que me han brindado.

Bueno tengo el siguiente problema, tengo un servicio que me ejecuta una aplicación, por problemas de seguridad o por lo menos así lo veo yo, lo puse que no me interactuara con el Desktop y desde ese momento me sigue ejecutando la otra aplicación, pero es como si no hiciera nada, parece que la aplicación que es llamada coge las propiedades, de no interactuar con el Desktop y me deja de funcionar correctamente esa otra aplicación, lo que hace es cerrar algunas aplicaciones, que tengo en un a lista negra, la pregunta es entonces, como puedo llamar a ese ejecutable y que interactué con el Desktop.

Muchas gracias por adelantado.

mensana 03-07-2007 08:07:24

Cita:

Empezado por l30
... tengo un servicio que me ejecuta una aplicación...
...parece que la aplicación que es llamada coge las propiedades, de no interactuar con el Desktop
¿...cómo puedo llamar a ese ejecutable y que interactué con el Desktop ?.

mira este enlace http://delphi.newswhat.com/geoxml/forumhistorythread?groupname=borland.public.delphi.vcl.components.using.win32&messageid=41efdb06$1@n ewsgroups.borland.com

l30 03-07-2007 20:26:48

Gracias por tu respuesta
 
Gracias por tu respuesta, estuve leyendo el enlace que por cierto esta en ingles y no se me da muy bien el ingles, pero bueno con la ayuda de un traductor, pude hacerlo todo, aunque hay otros enlaces que te sugieren allí que ya no están disponibles, tal ves por eso no funciono, pero repito la idea, es un servicio, que no interactúa con el sistema, que llama una aplicación que esa si me hace falta que interactué y no lo esta haciendo, en el enlace que me diste, estaba la respuesta parece en uno de los link, que no funcionan, probé con las dos funciones que vienen allí InitServiceDesktop; // Aquí pongo mi código para que llame a la aplicación que va a interactuar DoneServiceDesktop; Y nada a lo mejor no entendí bien.

mensana 04-07-2007 08:11:01

Cita:

Empezado por l30
... aunque hay otros enlaces ... que ya no están disponibles, tal ves por eso no funciono

Yo utilizo esto:

Código Delphi [-]
uses
  Windows;

var
  HStation:HWINSTA;
  HDesktop:HDESK;

initialization

  HStation := OpenWindowStation(PChar('WinSta0'), False, MAXIMUM_ALLOWED);
  SetProcessWindowStation(HStation);

  HDesktop := OpenDesktop(PChar('Default'), 0, False, MAXIMUM_ALLOWED);
  SetThreadDesktop(HDesktop);

end.

IMPORTANTE: Debe ser una de las primeras units en inicializarse, debes ponerla una de las primeras en tu "uses".

l30 04-07-2007 21:23:00

Gracias
 
Gracias por tu respuesta, hice lo que me dijiste, pero no me funciono te pongo un código de ejemplo para que lo veas, a ver que tengo mal.

Código Delphi [-]
  unit Unit2;
  interface
  uses windows;
   
   procedure dino;
   var
      HStation:HWINSTA;
      HDesktop:HDESK;
   
  implementation
   
  // Aqui pongo lo que me diste
  procedure dino;
  begin
    HStation := OpenWindowStation(PChar('WinSta0'), False, MAXIMUM_ALLOWED);
    SetProcessWindowStation(HStation);
    HDesktop := OpenDesktop(PChar('Default'), 0, False, MAXIMUM_ALLOWED);
    SetThreadDesktop(HDesktop);
  end;
   
  end.

Código Delphi [-]
 
  unit Unit1;
   
  interface
   
  uses
  Unit2, Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
    ExtCtrls, shellapi;
   
  type
    Tjulio = class(TService)
      Timer1: TTimer;
      procedure Timer1Timer(Sender: TObject);
      procedure ServiceStart(Sender: TService; var Started: Boolean);
    private
      { Private declarations }
    public
      function GetServiceController: TServiceController; override;
      { Public declarations }
    end;
   
  var
    julio: Tjulio;
   
  implementation
   
   
  {$R *.DFM}
   
  procedure ServiceController(CtrlCode: DWord); stdcall;
  begin
    julio.Controller(CtrlCode);
  end;
   
  function Tjulio.GetServiceController: TServiceController;
  begin
    Result := ServiceController;
  end;
   
  procedure Tjulio.ServiceStart(Sender: TService; var Started: Boolean);
  begin
     // aqui llamo a la function que tiene tu codigo
     dino;
   
      //  activo el relog que va a llamar una aplicación cada cierto tiempo.  
     Timer1.Enabled:= true;
  end;
   
  procedure Tjulio.Timer1Timer(Sender: TObject);
  begin
     // llamo al bloc de notas pero nada en los proceso aparece pero no se ve y supongo que sea por que no esta interactuando con el desktop


     WinExec('notepad', SW_SHOWNORMAL);
  end;
   
   
  end.

mensana 05-07-2007 08:16:26

Cita:

Empezado por l30
... hice lo que me dijiste, pero no me funciono .

El código era para ejecutarlo en la aplicación que interactua con el desktop, es decir, la que arranca el servicio (en tu caso Notepad.exe), así que no te servirá.

Ojo: He leído que en Windows Vista se ha cerrado la posibilidad que un servicio pueda ejecutar una aplicación.

l30 06-07-2007 05:31:10

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.

l30 09-07-2007 23:10:54

Ayuda
 
Por favor que alguien me ayude...????


La franja horaria es GMT +2. Ahora son las 08:56:02.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi