Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

How to start and stop a Windows Service

Posted on 1999-01-25
13
Medium Priority
?
166 Views
Last Modified: 2010-05-03
Hello Experts,

I recently asked a question about scheduling a Windows task at a future time.

http://www.experts-exchange.com/topics/comp/lang/visualbasic/Q.10121083

I have since found out how to do this by executing a simple DOS command called AT. That answered my question.

However, this command requires the "Schedule" service to be started.

Can someone tell me how to
1 Find out whether a service has started
2 Start a service
3 Stop a service

Through VB. If the answer is excellent I will increase the points. Please answer with a complete and self-contained answer as I have no access to literature.

Thanks
0
Comment
Question by:caraf_g
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
13 Comments
 
LVL 5

Expert Comment

by:carmine
ID: 1470679
NET START

will list all the services currently running.

NET START servicename
starts a service

NET STOP servicename
stops a service

Simple if you know!
0
 
LVL 5

Expert Comment

by:carmine
ID: 1470680
Ah. Sorry I missed the "through VB" bit!
0
 
LVL 10

Author Comment

by:caraf_g
ID: 1470681
Hey carmine...

That's excellent - don't worry about the "Through VB" bit - I can work that bit out.

That's 2 out of 3; the only thing left is, how can you find out whether a service is already running?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:waty
ID: 1470682
Give you e-mail, I have several long source code for that.
waty.thierry@usa.net
0
 
LVL 10

Author Comment

by:caraf_g
ID: 1470683
carmine,

Actually, I think that's worth some points so there's a question just for you:
http://www.experts-exchange.com/topics/comp/lang/visualbasic/Q.10121537

waty,

Thanks,

You can send me an e-mail at
carafa@tinet.ie
0
 
LVL 5

Expert Comment

by:carmine
ID: 1470684
From the command prompt/batch

net start|find /i /c "servicename"

will return either 0 or 1 depending on whether the service is started or not.
0
 
LVL 14

Expert Comment

by:waty
ID: 1470685
/*
|*  Query NT Services
\*

Description
-----------

Sample files for Using Visual Basic Classes to Query Windows NT Services
svcvb.dll provides the query classes
src.zip contains VB5 source for the server
tst.zip contains VB5 source for a test application

0
 
LVL 10

Author Comment

by:caraf_g
ID: 1470686
Thierry,

I've just tried carmine's suggestion
net start|find /i /c "servicename"

and it works fine - except it returns 1 when it's stopped and 2 when it's started... soit!

I'm therefore going to reject your answer so that carmine can put in a token answer so that I can accept carmine's answer.

I will however try out your suggestion and if it works better than carmine's I will put up a dummy question for you to get some points too (Probably tomorrow or Thursday).

Thanks very much for your help!
0
 
LVL 5

Accepted Solution

by:
carmine earned 200 total points
ID: 1470687
NET START servicename
starts a service

NET STOP servicename
stops a service

To check if a service is started in batch:

@echo off
for /f "tokens=*" %%a in ('net start') do if /i "%%a"=="servicename" (echo Service started) else (echo Service Stopped)



or if the servicename is a unique substring:

net start|find /i /c "servicename"

0
 
LVL 10

Author Comment

by:caraf_g
ID: 1470688
carmine and waty,

Thank you both very much.

waty - if your method works I will post a question especially for you. Hopefully I get to try it out tonight.
0
 
LVL 10

Author Comment

by:caraf_g
ID: 1470689
carmine and waty,

Thank you both very much.

waty - if your method works I will post a question especially for you. Hopefully I get to try it out tonight.
0
 
LVL 10

Author Comment

by:caraf_g
ID: 1470690
carmine and waty,

Thank you both very much.

waty - if your method works I will post a question especially for you. Hopefully I get to try it out tonight.
0
 
LVL 10

Author Comment

by:caraf_g
ID: 1470691
OOO NNNooo,,, III'''mmm rrreeepppeeeaaatttiiinnnggg mmmyyyssseeelllfff aaagggaaaiiinnn...

Sorry lads and lasses - our proxy server is working in fits and starts and is falling over most of the time...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question