Stop Service

Hello everyone !!
I have a problem
Since i am newbie to this driver development,
i hope i can get some answers without any frustration
I have a legacy driver
i want to load the driver, and note down the debug information
i have created some services and made some registry entries
i am able to install and start the service
but not able to stop the service

I have defined SERVICE_ALL_ACCESS but could not get to know how i could
not stop the service
when i try this through net stop…it gives a message saying… U dont have
access to stop or pause the service
my code looks somehow like this …

BOOL InstallFunc(IN SC_HANDLE SCManager, IN LPCTSTR DriverName, IN LPCTSTR
DriverBinary)
{
SC_HANDLE SCService;

printf(“\n Entering InstallFunc() “);
printf(”\nthe DriverName is %s”, DriverName);
printf(“\n the DriverBinary is %s”,DriverBinary);
SCService = CreateService(SCManager,DriverName, DriverName,
SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
DriverBinary, NULL, NULL, NULL, NULL, NULL );
if (!SCService)
{

return FALSE;

}

CloseServiceHandle(SCService);

return TRUE;
}

BOOL StartFunc(IN SC_HANDLE SCManager, IN LPCTSTR DriverName)
{
SC_HANDLE SCService;
BOOL bStart;
SCService = OpenService(SCManager,DriverName,SERVICE_ALL_ACCESS);
if (!SCService)
{
printf(“\n Service cannot be opened”);
return FALSE;
}

bStart = StartService( SCService, 0, NULL )
|| GetLastError() == ERROR_SERVICE_ALREADY_RUNNING
|| GetLastError() == ERROR_SERVICE_DISABLED;

if (bStart)
{
printf(“\n The Service is started”);
}

CloseServiceHandle(SCService);

return bStart;
}

BOOL StopFunc(IN SC_HANDLE SCManager, IN LPCTSTR DriverName)
{
SC_HANDLE SCService;
SERVICE_STATUS schStatus;
BOOL bStop;

SCService = OpenService(SCManager, DriverName,SERVICE_ALL_ACCESS ); if
(SCService==NULL)
return FALSE;

bStop = ControlService(SCService, SERVICE_CONTROL_STOP,
&schStatus );

if (bStop)
{
printf(“\n Service Stopped”);
}

CloseServiceHandle(SCService);
return bStop;
}

i am sorry , if i am asking something silly
but pls help me out

thanx in advance
Maneel

One reason could be that your legacy driver does not have an unload routine.

=====================
Mark Roddy

-----Original Message-----
From: Maneel [mailto:xxxxx@lycos.com]
Sent: Wednesday, December 03, 2003 7:30 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] Stop Service

Hello everyone !!
I have a problem
Since i am newbie to this driver development, i hope i can
get some answers without any frustration I have a legacy
driver i want to load the driver, and note down the debug
information i have created some services and made some
registry entries i am able to install and start the service
but not able to stop the service

I have defined SERVICE_ALL_ACCESS but could not get to know
how i could not stop the service when i try this through net
stop…it gives a message saying… U dont have access to stop
or pause the service my code looks somehow like this …

BOOL InstallFunc(IN SC_HANDLE SCManager, IN LPCTSTR
DriverName, IN LPCTSTR
DriverBinary)
{
SC_HANDLE SCService;

printf(“\n Entering InstallFunc() “);
printf(”\nthe DriverName is %s”, DriverName);
printf(“\n the DriverBinary is %s”,DriverBinary);
SCService = CreateService(SCManager,DriverName,
DriverName,
SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
DriverBinary, NULL, NULL, NULL, NULL, NULL );
if (!SCService)
{

return FALSE;

}

CloseServiceHandle(SCService);

return TRUE;
}

BOOL StartFunc(IN SC_HANDLE SCManager, IN LPCTSTR DriverName) {
SC_HANDLE SCService;
BOOL bStart;
SCService =
OpenService(SCManager,DriverName,SERVICE_ALL_ACCESS);
if (!SCService)
{
printf(“\n Service cannot be opened”);
return FALSE;
}

bStart = StartService( SCService, 0, NULL )
|| GetLastError() ==
ERROR_SERVICE_ALREADY_RUNNING
|| GetLastError() == ERROR_SERVICE_DISABLED;

if (bStart)
{
printf(“\n The Service is started”);
}

CloseServiceHandle(SCService);

return bStart;
}

BOOL StopFunc(IN SC_HANDLE SCManager, IN LPCTSTR DriverName) {
SC_HANDLE SCService;
SERVICE_STATUS schStatus;
BOOL bStop;

SCService = OpenService(SCManager,
DriverName,SERVICE_ALL_ACCESS ); if
(SCService==NULL)
return FALSE;

bStop = ControlService(SCService, SERVICE_CONTROL_STOP,
&schStatus );

if (bStop)
{
printf(“\n Service Stopped”);
}

CloseServiceHandle(SCService);
return bStop;
}

i am sorry , if i am asking something silly but pls help me out

thanx in advance
Maneel


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as:
xxxxx@stratus.com To unsubscribe send a blank email to
xxxxx@lists.osr.com

Your driver must have an Unload routine implemented if you want it to be
stoppable by “net stop”.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

----- Original Message -----
From: “Maneel”
To: “Windows System Software Devs Interest List”
Sent: Wednesday, December 03, 2003 3:29 PM
Subject: [ntdev] Stop Service

> Hello everyone !!
> I have a problem
> Since i am newbie to this driver development,
> i hope i can get some answers without any frustration
> I have a legacy driver
> i want to load the driver, and note down the debug information
> i have created some services and made some registry entries
> i am able to install and start the service
> but not able to stop the service
>
> I have defined SERVICE_ALL_ACCESS but could not get to know how i could
> not stop the service
> when i try this through net stop…it gives a message saying… U dont have
> access to stop or pause the service
> my code looks somehow like this …
>
> BOOL InstallFunc(IN SC_HANDLE SCManager, IN LPCTSTR DriverName, IN LPCTSTR
> DriverBinary)
> {
> SC_HANDLE SCService;
>
> printf(“\n Entering InstallFunc() “);
> printf(”\nthe DriverName is %s”, DriverName);
> printf(“\n the DriverBinary is %s”,DriverBinary);
> SCService = CreateService(SCManager,DriverName, DriverName,
> SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER,
> SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
> DriverBinary, NULL, NULL, NULL, NULL, NULL );
> if (!SCService)
> {
>
> return FALSE;
>
> }
>
> CloseServiceHandle(SCService);
>
> return TRUE;
> }
>
> BOOL StartFunc(IN SC_HANDLE SCManager, IN LPCTSTR DriverName)
> {
> SC_HANDLE SCService;
> BOOL bStart;
> SCService = OpenService(SCManager,DriverName,SERVICE_ALL_ACCESS);
> if (!SCService)
> {
> printf(“\n Service cannot be opened”);
> return FALSE;
> }
>
> bStart = StartService( SCService, 0, NULL )
> || GetLastError() == ERROR_SERVICE_ALREADY_RUNNING
> || GetLastError() == ERROR_SERVICE_DISABLED;
>
>
> if (bStart)
> {
> printf(“\n The Service is started”);
> }
>
> CloseServiceHandle(SCService);
>
> return bStart;
> }
>
> BOOL StopFunc(IN SC_HANDLE SCManager, IN LPCTSTR DriverName)
> {
> SC_HANDLE SCService;
> SERVICE_STATUS schStatus;
> BOOL bStop;
>
> SCService = OpenService(SCManager, DriverName,SERVICE_ALL_ACCESS ); if
> (SCService==NULL)
> return FALSE;
>
> bStop = ControlService(SCService, SERVICE_CONTROL_STOP,
> &schStatus );
>
> if (bStop)
> {
> printf(“\n Service Stopped”);
> }
>
> CloseServiceHandle(SCService);
> return bStop;
> }
>
> i am sorry , if i am asking something silly
> but pls help me out
>
> thanx in advance
> Maneel
>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@storagecraft.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com

Thnx Mark & Maxim
I have my Unload routine too !!
while calling my unload routine , i am trying to first stop the service,
for that i have written my stop service routine.
But i could not able to stop the service

Help pls

Thanx in advance
Maneel

Are you talking about a service or a driver? These are two different things.
Your kernel driver must have an unload routine in order to support a stop
service request. You do not have to write a program to use the service
manager api to load and unload your driver. Use a command window and the
commands ‘net start driver_service_name’ and ‘net stop driver_service_name’.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Maneel
Sent: Wednesday, December 03, 2003 1:02 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Re: Stop Service

Thnx Mark & Maxim
I have my Unload routine too !!
while calling my unload routine , i am trying to first stop
the service, for that i have written my stop service routine.
But i could not able to stop the service

Help pls

Thanx in advance
Maneel


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as:
xxxxx@hollistech.com To unsubscribe send a blank email to
xxxxx@lists.osr.com

{ // Start of function
// Here is what I have and works.
// Hope you can make sense out of it
// Open the SCM.
SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if(hSCM == NULL)
{
// return error here
return E_FAIL;
}

// Open the service.
SC_HANDLE hService = ::OpenService(hSCM, m_szMyServiceName, SERVICE_STOP);
if(hService == NULL)
{
return E_FAIL;
}

// Tell the SCM to stop the service.
SERVICE_STATUS ServiceStatus;
BOOL bResult =
::ControlService(hService, SERVICE_CONTROL_STOP, &ServiceStatus);
if(bResult == FALSE)
{
::CloseServiceHandle(hService);
::CloseServiceHandle(hSCM);
return E_FAIL;
}
::CloseServiceHandle(hService);
::CloseServiceHandle(hSCM);

return S_OK;
} //End of function