Hola gente:
antes que nada quiero agradecer por todas sus contestaciones ya que son muy útiles.
Escribo porque estoy teniendo un problema con un servicio que desarrolle en delphi.
El servicio cuando lo corro desde delphi (pongo F9 para ejecutarlo) funciona bien y hace lo que yo quiero.
Ahora bien cuando lo instalo (C:\Mariano\ServicioDelphi\Servicio1.exe /INSTALL) y reinicio la máquina este servicio se pone como "nombre de usuario" SYSTEM (cosa que quiero que pase) en el administrador de sistemas pero no ejecuta nada de lo que programe.
El código es muy simple, lo único que hace es ejecutar el word si se ejecuta primero la calculadora.
Espero que puedan ayudarme porque me estoy volviendo un poco loco con este tema....
Les dejo el código a ver si me pueden dar una mano:
Código Delphi
[-]
program Servicio1;
uses
SvcMgr,
SysUtils,
Servicio in 'Servicio.pas' ;
{$R *.RES}
begin
if not Application.DelayInitialize or Application.Installing then
Application.Initialize;
Application.CreateForm(TService1, Service1);
Application.Run;
while True do
begin
Service1.MatarProceso;
sleep(5000);
end;
end.
unit Servicio;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
ExtCtrls, Psapi, ShellAPI;
type
TService1 = class(TService)
private
procedure KillProcess(hProcess: DWORD);
function GetProcessByFileName(AFileName: TFileName): DWORD;
public
function GetServiceController: TServiceController; override;
procedure MatarProceso;
end;
var
Service1: TService1;
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
Service1.Controller(CtrlCode);
end;
procedure TService1.MatarProceso;
var
hProcessWord: DWORD;
hProcessCalc: DWORD;
begin
hProcessWord := GetProcessByFileName('C:\Archivos de programa\Microsoft Office\OFFICE11\WINWORD.exe'); hProcessCalc := GetProcessByFileName('C:\WINDOWS\system32\calc.exe'); if ((hProcessWord <> 0) and (hProcessCalc = 0)) then
KillProcess(hProcessWord);
if ((hProcessCalc <> 0) and (hProcessWord = 0)) then
WinExec(PChar('C:\Archivos de programa\Microsoft Office\OFFICE11\WINWORD.EXE'),SW_SHOWNORMAL);
end;
function TService1.GetProcessByFileName(AFileName: TFileName): DWORD;
var
pidProcess: array [0..1023] of DWORD;
hProcess: DWORD;
dwCount: DWORD;
hMod: HMODULE;
ModuleFileName: array[0..MAX_PATH] of Char;
i: Integer;
begin
Result := 0;
if EnumProcesses(@pidProcess, SizeOf(pidProcess), dwCount) then
for i := 0 to dwCount div SizeOf(DWORD) do
begin
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, pidProcess[i]);
if (hProcess <> 0) then
try
EnumProcessModules(hProcess, @hMod, SizeOf(hMod), dwCount);
GetModuleFilenameEx(hProcess, hMod, ModuleFileName, SizeOf(ModuleFileName));
if SameFileName(ModuleFileName, AFileName) then
begin
Result := pidProcess[i];
break;
end;
finally
CloseHandle(hProcess);
end;
end;
end;
function TService1.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TService1.KillProcess(hProcess: DWORD);
var
hProc: DWORD;
begin
hProc := OpenProcess(PROCESS_TERMINATE, False, hProcess);
if hProc <> 0 then
try
TerminateProcess(hProc, 0);
finally
CloseHandle(hProc);
end;
end;
end.
Muchas gracias.
Mariano.