Satyr Cdt

Registrado: 13 Feb 2003Mensajes: 22Ubicación: Panama
|
Publicado: Mie 29 Dic, 2004 10:00 pm Asunto: Crear Servicios en windows |
|
|
hola a todos los foristas!!!!
mi consulta es la siguiente, estoy creando una aplicacion q corra como un servicio de windows (mi win es xp home), aki añado el codigo de lo q estoy haciendo...
hay q agregar 3 botones al Form
y el Unit se llama Uservices
en el .cpp
| Código: |
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Uservice.h"
#include "Winsvc.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
SERVICE_STATUS m_ServiceStatus;
SERVICE_STATUS_HANDLE m_ServiceStatusHandle;
BOOL bRunning=true;
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD Opcode);
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(InstallService()){
ShowMessage("Servicio Instalado...");
SERVICE_TABLE_ENTRY DispatchTable[]={{"Servicio1",ServiceMain},{NULL,NULL}};
StartServiceCtrlDispatcher(DispatchTable);
}
else ShowMessage("Servicio no fue Instalado...");
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::InstallService()
{
char strDir[1024];
HANDLE schSCManager,schService;
AnsiString p =ExtractFilePath(Application->ExeName)+"Service1.exe";
strcpy(strDir,p.c_str());
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
return false;
LPCTSTR lpszBinaryPathName=strDir;
schService = CreateService(schSCManager,"Servicio1","JAG_Service",
SERVICE_ALL_ACCESS,
SERVICE_WIN32_SHARE_PROCESS, //SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START ,
SERVICE_ERROR_NORMAL,
lpszBinaryPathName,
NULL,
NULL,
NULL,
NULL,
NULL);
if (schService == NULL)
return false;
CloseServiceHandle(schService);
return true;
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::DeleteService1()
{
HANDLE schSCManager;
SC_HANDLE hService;
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
return false;
hService=OpenService(schSCManager,"Servicio1",SERVICE_ALL_ACCESS);
if (hService == NULL)
return false;
if(DeleteService(hService)==0)
return false;
if(CloseServiceHandle(hService)==0)
return false;
else
return true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(DeleteService1())ShowMessage("Servicio Desinstalado...");
else ShowMessage("Servicio no fue Desinstalado...");
}
//---------------------------------------------------------------------------
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
DWORD status;
DWORD specificError;
m_ServiceStatus.dwServiceType = SERVICE_WIN32;
m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwServiceSpecificExitCode = 0;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
m_ServiceStatusHandle = RegisterServiceCtrlHandler("Servicio1",ServiceCtrlHandler);
if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
{
return;
}
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus);
bRunning=true;
while(bRunning)
{
Sleep(3000);
//Espera mientras se procesa el codigo
}
return;
}
void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
switch(Opcode)
{
case SERVICE_CONTROL_PAUSE:
m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
break;
case SERVICE_CONTROL_CONTINUE:
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
break;
case SERVICE_CONTROL_STOP:
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
bRunning=false;
break;
case SERVICE_CONTROL_INTERROGATE:
break;
}
return;
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
SERVICE_TABLE_ENTRY DispatchTable[]={{"Servicio1",ServiceMain},{NULL,NULL}};
StartServiceCtrlDispatcher(DispatchTable);
}
//---------------------------------------------------------------------------
|
en el .h
| Código: |
//---------------------------------------------------------------------------
#ifndef UserviceH
#define UserviceH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TButton *Button3;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
bool __fastcall InstallService();
bool __fastcall DeleteService1();
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
|
logro crear el servicio, presionando el boton 1 y con el boton puedo Eliminarlo,
pero cuando intento inicializarlo con el boton 3 no sucede nada, la verdad no c q es lo q estoy haciendo mal
tambien intente iniciarlo dede panel de control - Herramientas Administrativas - Servicios y me envia el siguiente error:
| Código: |
No se puede iniciar el servicio JAG_Service en Equipo local
Error 1053: El Servicio no ha respondido a la peticion o inicio del control en un tiempo adecuado.
|
donde JAG_Service es el nombre de la etiqueta del Servicio, el nombre del servicio es Servicio1.
mis preguntas son la siguientes:
1)Las funciones
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD Opcode);
sean unicamente para aplicaciones de modo consola????
2) Alguien tiene documentacion o algo que sea ayuda para utilizar el Service Application del builder???
cualkier ayuda o comentario seria util en estos momentos
Gracias de antemano ... |
|