//---------------------------------------------------------------------------
#include
#include
#include
//---------------------------------------------------------------------------
bool __fastcall TForm1::InstallService(AnsiString SrvName, AnsiString DispName, AnsiString FullPathToExeService, AnsiString SrvDesc)
{
bool ret = false;
SC_HANDLE scm;
SC_HANDLE svc;
UnicodeString blad;
scm = OpenSCManager(
NULL,
SERVICES_ACTIVE_DATABASE,
SC_MANAGER_ALL_ACCESS
);
if( scm==NULL ){
int retError = GetLastError();
switch(retError){
case ERROR_ACCESS_DENIED: blad = "Brak uprawnień do menadżera usług"; break;
case ERROR_DATABASE_DOES_NOT_EXIST: blad = "The specified database does not exist"; break;
default: blad = "Nie udało się otworzyć menadżera usług, kod błędu: " + UnicodeString(GetLastError());
}
Vcl::Forms::Application->MessageBox(blad.c_str(), L"Błąd", MB_ICONERROR | MB_OK);
return (false);
}
svc = CreateService(
scm,
SrvName.c_str(),
DispName.c_str(),
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
FullPathToExeService.c_str(),
NULL,
NULL,
NULL,
NULL,
NULL
);
if( svc==NULL ){
int retError = GetLastError();
switch(retError){
case ERROR_ACCESS_DENIED: blad = "Brak uprawnień do menadżera usług"; break;
case ERROR_CIRCULAR_DEPENDENCY: blad = "A circular service dependency was specified."; break;
case ERROR_DUPLICATE_SERVICE_NAME: blad = "The display name already exists in the service control manager database either as a service name or as another display name."; break;
case ERROR_INVALID_HANDLE: blad = "The handle to the specified service control manager database is invalid."; break;
case ERROR_INVALID_PARAMETER: blad = "A parameter that was specified is invalid."; break;
case ERROR_INVALID_SERVICE_ACCOUNT: blad = "The user account name specified in the lpServiceStartName parameter does not exist."; break;
case ERROR_SERVICE_EXISTS: blad = "The specified service already exists in this database."; break;
case ERROR_SERVICE_MARKED_FOR_DELETE: blad = "The specified service already exists in this database and has been marked for deletion."; break;
default: blad = "Nie udało się zainstalować usługi, kod błędu: " + UnicodeString(GetLastError());
}
Vcl::Forms::Application->MessageBox(blad.c_str(), L"Błąd", MB_ICONERROR | MB_OK);
return (false);
}
else if(!SrvDesc.IsEmpty()){
SERVICE_DESCRIPTION sd;
sd.lpDescription = SrvDesc.c_str();
ChangeServiceConfig2( svc, SERVICE_CONFIG_DESCRIPTION, &sd );
}
CloseServiceHandle(svc);
CloseServiceHandle(scm);
return (true);
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
InstallService(ServiceName->Text, DisplayName->Text, PathService->Text, Description->Text);
}
//---------------------------------------------------------------------------