Article View: borland.public.cppbuilder.nativeapi
Article #7896Re: Controlling Windows service
From: forums.borland.com
Date: Tue, 14 Dec 2004 09:29
Date: Tue, 14 Dec 2004 09:29
145 lines
6045 bytes
6045 bytes
Hi Deepak,
Simple create a Service Application and build it.
For register / unregister the service:
ServiceName.exe /INSTALL
ServiceName.exe /UNINSTALL
To Start / Stop the service use the Service Manager (look at MSDN library)
Here's an example:
//---------------------------------------------------------------------------
bool ServiceActive;
SC_HANDLE hServiceControl;
SC_HANDLE hService;
// Open Service Manager
hServiceControl = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (hServiceControl != NULL)
{
// Open Service with name "ServerWatchdog"
(ServiceApplication Name)
hService =
OpenService(hServiceControl,"ServerWatchdog",SERVICE_ALL_ACCESS);
if (hService != NULL)
ServiceControlActive = true;
else
CloseServiceHandle(hServiceControl);
}
//---------------------------------------------------------------------------
// Close Service Manager
void __fastcall TfrmMain::CloseServiceControl()
{
if (ServiceControlActive)
{
CloseServiceHandle(hService);
CloseServiceHandle(hServiceControl);
}
}
//---------------------------------------------------------------------------
// Get Service Status Info
void __fastcall TfrmMain::UpdateServiceStatus()
{
SERVICE_STATUS ServiceStatus;
if (ServiceControlActive)
{
memset(&ServiceStatus,0,sizeof(SERVICE_STATUS));
QueryServiceStatus(hService,&ServiceStatus);
if ((ServiceStatus.dwCurrentState & SERVICE_STOPPED) ||
(ServiceStatus.dwCurrentState & SERVICE_STOP_PENDING))
{
btnServiceStart->Enabled = true;
btnServiceStop->Enabled = false;
return;
}
if ((ServiceStatus.dwCurrentState & SERVICE_RUNNING) ||
(ServiceStatus.dwCurrentState & SERVICE_START_PENDING))
{
btnServiceStart->Enabled = false;
btnServiceStop->Enabled = true;
return;
}
btnServiceStart->Enabled = false;
btnServiceStop->Enabled = false;
}
}
//---------------------------------------------------------------------------
// Start Service
void __fastcall TfrmMain::btnServiceStartClick(TObject *Sender)
{
if (ServiceControlActive)
{
StartService(hService,0,NULL);
btnServiceStart->Enabled = false;
btnServiceStop->Enabled = false;
Application->ProcessMessages();
Sleep(500);
UpdateServiceStatus();
}
}
//---------------------------------------------------------------------------
// Stop Service
void __fastcall TfrmMain::btnServiceStopClick(TObject *Sender)
{
SERVICE_STATUS ServiceStatus;
if (ServiceControlActive)
{
memset(&ServiceStatus,0,sizeof(SERVICE_STATUS));
ControlService(hService,SERVICE_CONTROL_STOP,&ServiceStatus);
btnServiceStart->Enabled = false;
btnServiceStop->Enabled = false;
Application->ProcessMessages();
Sleep(500);
UpdateServiceStatus();
}
}
Best Regards,
Iacovozzi Sandro
(Italy)
P.S.:
The first parameter of "OpenSCManager" must be the machine name where the
service is installed. (So you can control a service running in a different
PC)
"Deepak Mishra" <deepak.dmishra@gmail.com> ha scritto nel messaggio
news:41bd5410$1@newsgroups.borland.com...
>
> Hi All,
>
> I am developing a multi-user system
> It will have a background process (Windows service, running on server
> machine) which can be triggered
> in two ways:
>
> The background processes is needed for uploading and downloading files
> to/from remote server thru ftp.
> Now the requirement is such that this process should be triggered whenever
> the first user logs in to the
> system and should stop when the last user logs out of the system.
> Also, there should be some mechanism by which the end-user can control
> this process.
> That is, the client application should also have a button for initiating
> and terminating the process
>
> Now my concern is when the process is triggered automatically, then the
> client application will neither
> have the "PID" nor the "Process Handle".
> In such a scenario, can that process be terminated by its process name?
> Also, can we check the status
> of a process by its name so that we can restrict the user from running
> multiple instances of the same process?
>
> Regards,
>
> Deepak Mishra
Technical Details
Message-ID:
<41beb1b4@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<41bd5410$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,2580bf3649196f02 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit Reply-To: "forums.borland.com" <progettazione@infoitaliasrl.com> From: "forums.borland.com" <progettazione@infoitaliasrl.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <41bd5410$1@newsgroups.borland.com> Subject: Re: Controlling Windows service Date: Tue, 14 Dec 2004 10:29:12 +0100 Lines: 147 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 82.91.94.86 Message-ID: <41beb1b4@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1103016372 82.91.94.86 (14 Dec 2004 01:26:12 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail