Results 1 to 38 of 38

Thread: Controling services in vb

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Controling services in vb

    I was wondering if there is a way to turn a service on or off with a vb6 program? I frequently have to turn on and off one of my services.( actually pause and resume) I wont go into details. I know it can be done with vb.net 2003, because ive seen it done. but i dont have that. i just have the old school vb6. If someone could post some snippets of the code that does this, that would be great. Then i can take it from there and design the form to my likings. (I tried to make a batch file in xp to do this, but for some reason it doesnt like the CHOICE command.)Just for the heck of it i attached my batch file, so you can get an idea of what id like todo in vb6.
    Attached Files Attached Files

  2. #2
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Talking Re: Controling services in vb

    Here is a very good example of how to handle this:

    http://www.andreavb.com/tip060002.html
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    wow, i was just expecting a few lines of code. wouldnt it be easier to send commands to the command line from within vb? Because all im doing is pause/resume 1 service. That does seem pretty complicated. I might need a little more help with this than i thought. I took VB6 1&2(2 semesters) in college. we learned a lot but not all that fancy stuff! I attached an example of what my form might look like.
    Attached Images Attached Images  

  4. #4
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Controling services in vb

    Yes, you can just pass command line arguments within VB to handle the starting and stopping or pausing of a service.

    I just thought I would show you some code that would give you a little better control of what happens.
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  5. #5
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Controling services in vb

    Have you tried using Shell? like this:

    vb Code:
    1. 'To start a service
    2. Shell ("Net Start Service")
    3.  
    4. 'To Pause a service
    5. Shell ("Net Pause Service")
    6.  
    7. 'To Stop a service
    8. Shell ("Net Stop Service")

    That should work. If not, we may need to use ShellExecute
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    Thanks, ill try the shell command. How would i use the shell command to get the status of the service in Form_Load(). Im assuming you would have to assign the status to some type of variable.

    It does seem neater to do it like the examle at http://www.andreavb.com/tip060002.html

    Id like todo it that way if i can.

    Im not trying to be a bother and i obviously dont know as much about VB as you do. But would you mind showing me the block of code and declarations i need just to get the status, pause and start a single service?

    if i can get an example and something to work off of i can usually take it from there.

    see, in school we just learned all the fundamentals of vb6: forms, controls in the toolbox, declaring variables, timers, loops, some strings and crystal reports. all of our homework was just making programs that did mathematical calculations. We didnt learn anything outside that scope as far as interacting with the OS. Im just writing this so you have an idea of what i know and where im comming from.

    Thank you much...................

  7. #7
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Controling services in vb

    Quote Originally Posted by lokeycmos
    It does seem neater to do it like the examle at http://www.andreavb.com/tip060002.html

    Id like todo it that way if i can.
    You would like to do it the way provided in the link or by using a Shell method to call a command line action?

    Do not fret over what you do and don't know. You are learning and that is what is important. Remember, it is easier to use a proven method someone else has come up with than it is to try and re-invent the wheel. The code posted above is not something I came up with at all, just a site I happen to frequent when looking at code snippets and I came across something I thought would help you.

    If you are wanting to do this all via Shell and command line, let me know so we can help you with the code.

    If you are wanting to do it via the method in the link, let me know and I'll break it down and explain it for you
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    Thank you very much for understanding. Your right, its nice not to reinvent the wheel. Some of the small apps ive made were my own, while others are based off pieces of prewritten code from one or more websites. Its nice to take someones source code and then modify it for personal preference. Can you please explain the pros and cons of using the shell method vs. the other method? Maybe if i learn to do it both ways, Ill learn a thing or two in the process Thanks for being patient with me

  9. #9
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Controling services in vb

    Always a pleasure

    Using the Shell method is not a bad idea for what you are trying to do. Since you are only trying to pause or start a single service, this might be the best choice for you. The problem you'll run into is trying to determine if the service is already running or not via shell to the command prompt and getting your program to read the responses.

    Using the VB code in the link would allow you total control over the services and allows your program to handle everything within itself. Anytime you can do a task within the program and not have to call on a second program to handle it, the better off you'll be. The problem you'll find with using this method is trying to learn about API calls and where to put all the code. It can be confusing if you don't know what you are looking at.

    I am more than happy to walk you through how to use the code in the link above if you want to use it, of we can work on getting the Shell method to work to your liking..

    Just let me know
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    Thank you Capp. im am actually out of state right now and i wont be back until about the 11th of march. Is that all right with you? Thank you for your patients. We will get this thing going when i get back. How does that sound? Ill post a reply when i get back. Thanx again, lokeycmos

  11. #11
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Controling services in vb

    Sounds good to me. If you want, just PM me when you get back and we'll resume.

    Take care
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    im back from spring break. is it simple as just a few declarations and a few lines of code to pause/continue a service? or is it pretty complicated?

  13. #13
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Controling services in vb

    Telling VB to pause, stop or continue a service is not that much code and isn't that difficult. You could use Shell method I listed above.

    But, getting VB to recognize if the service's status is not that simple.

    My recommendation is to use the code posted in the link above.


    Look at the first section in blue (API Declarations). You can just copy and paste this directly into a module in your program.

    Look at the second section in blue (Module). Copy this and paste it at the bottom of the same module you posted the API Declarations in.

    The 3rd section is the code for 4 separate command buttons. The 1st button Stops the service. In this case, it's the "Scheduling" service. The 2nd button Starts it. The 3rd button Pauses it and the 4th button will tell you the current status of the service.

    You would only need to change the name of the service and viola'.

    Let me know if you have any more questions
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    Thanks Capp, ill post if i have any questions

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    Slowly, but surely. Is this what its sposed to look like? Im getting a compile error about comments in the declare functions line.
    Attached Files Attached Files
    Last edited by lokeycmos; Mar 13th, 2007 at 04:21 PM.

  16. #16
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Controling services in vb

    Quote Originally Posted by lokeycmos
    Slowly, but surely. Is this what its sposed to look like? Im getting a compile error about comments in the declare functions line.

    Not quite...

    What you need to do is click on the Projects menu, Click Add Module.

    Paste the API declarations and the bit titled "Module" in the module you just added. Make sure the API declarations are at the top.

    Take out all the code except for the command button from the form level..
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    Sweet, i got it going. Thanks for all your help Capp! Now ill just modify it for my liking. By the way, Whats a module? we never learned that in school either. How do i change my post to 'Resolved'?.................

  18. #18
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Controling services in vb

    Module
    A Module is a general term for a file containing code or information that you add to your project. Usually, a module contains program code which you write. In VB 6, modules have a .bas extension and there are just three kinds of modules: form, standard, and class. In VB.NET, modules usually have a .vb extension but others are possible, such as .xsd for a dataset module, .xml for an XML module, .htm for a web page, .txt for a text file, .xslt for an XSLT file, .css for a Style Sheet, .rpt for a Crystal Report, and others.


    To change it to resolved, click on the "Thread Tools" menu at the top and click "Resolved"

    Glad I was able to help
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    Here is the project and stuff. (if your interested). I still have to modify it for personal preference. Thnx again. Mabey you could help on future projects of mine
    Attached Files Attached Files

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    Im having some problems with the program after modifying it. I havnt changed anything in the module. all i did was modify the usage and changed the name of the service in the usage. the status says that it is alwaysed paused even when i click on 'start' and 'stop' repeatedly. any ideas?
    Attached Files Attached Files

  21. #21
    Addicted Member kewakl's Avatar
    Join Date
    Oct 2006
    Location
    between keyboard and chair
    Posts
    220

    Re: Controling services in vb

    FYI:

    i started a service manager program. never completed
    i had problems getting some services to respond to my commands
    (I used objWMIService maybe I'll have to try the way that you and Capp have used here -- thanks for this post )

    my interface is basically a list that shows current state (via checkboxes) and allowed options for starting/stopping/disabling (again via checkboxes)
    I never included PAUSE!

    see attached
    Attached Images Attached Images  
    Last edited by kewakl; Mar 13th, 2007 at 07:24 PM. Reason: figuring out how to attach!
    Do not use if shrinkwrap is broken or missing!
    I'm learning how to fish, too!

  22. #22
    Addicted Member kewakl's Avatar
    Join Date
    Oct 2006
    Location
    between keyboard and chair
    Posts
    220

    Re: Controling services in vb

    where are you using SERVICE_PAUSE_CONTINUE?
    Edit: Does StartService unpause a service?
    Last edited by kewakl; Mar 13th, 2007 at 08:05 PM.
    Do not use if shrinkwrap is broken or missing!
    I'm learning how to fish, too!

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    I dont know anything about API, this is still greek to me. This page tells about the windows command prompt service commands. http://commandwindows.com/sc.htm

    for some reason when i run my modified version of the program Capp helped me with it wont unpause.

  24. #24
    Addicted Member kewakl's Avatar
    Join Date
    Oct 2006
    Location
    between keyboard and chair
    Posts
    220

    Re: Controling services in vb

    try remming the
    Code:
    MsgBox ServiceStatus("", "Schedule")
    lines in the start/stop/pause command subs
    Do not use if shrinkwrap is broken or missing!
    I'm learning how to fish, too!

  25. #25
    Addicted Member kewakl's Avatar
    Join Date
    Oct 2006
    Location
    between keyboard and chair
    Posts
    220

    Re: Controling services in vb

    another way to do this

    http://www.devx.com/vb2themax/Tip/19045 by Francesco Balena

    looks like less code

    (if you're interested)
    Do not use if shrinkwrap is broken or missing!
    I'm learning how to fish, too!

  26. #26
    Addicted Member kewakl's Avatar
    Join Date
    Oct 2006
    Location
    between keyboard and chair
    Posts
    220

    Re: Controling services in vb

    i like the commandwindows.com sc page.
    now I may have enough info to continue work on my old service manager

    i cannot pause some services. normal, i guess
    Do not use if shrinkwrap is broken or missing!
    I'm learning how to fish, too!

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    I dont know about 'pausing', but some services cant be 'stopped' because of dependencies.

  28. #28

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    Wheres the usage for the code here: http://www.devx.com/vb2themax/Tip/19045

  29. #29
    Addicted Member kewakl's Avatar
    Join Date
    Oct 2006
    Location
    between keyboard and chair
    Posts
    220

    Re: Controling services in vb

    Code:
    ' start/stop/pause/continue a service
    ' SERVICENAME is the internal name of the service
    ' COMMAND can be   0=Start, 1=Stop, 2=Pause, 3=Continue
    '
    ' returns True if successful, False otherwise
    ' if any error, call Err.LastDLLError for more information
    from the devx page - the green remmed out section

    basically
    to start
    ServiceCommand "Schedule", 0

    to stop
    ServiceCommand "Schedule", 1


    check http://support.microsoft.com/kb/189633
    you can determine StartType for a service - <boot|system|auto|demand|disabled>
    b/c if it is disabled, it can't be started on demand (manual)
    Last edited by kewakl; Mar 13th, 2007 at 10:31 PM.
    Do not use if shrinkwrap is broken or missing!
    I'm learning how to fish, too!

  30. #30
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Controling services in vb

    Quote Originally Posted by lokeycmos
    Im having some problems with the program after modifying it. I havnt changed anything in the module. all i did was modify the usage and changed the name of the service in the usage. the status says that it is alwaysed paused even when i click on 'start' and 'stop' repeatedly. any ideas?
    Sorry it took so long for me to respond. I downloaded the project you uploaded and ran it. I was able to pause,stop,start and get the correct status of the service with no problems.

    When you click on the start/stop button, does it say something like "Start Pending" or "Stop Pending"?
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  31. #31
    Addicted Member kewakl's Avatar
    Join Date
    Oct 2006
    Location
    between keyboard and chair
    Posts
    220

    Re: Controling services in vb

    @Capp
    i had the same problem as lokeycmos.
    something got clobbered.

    now, as you say, it works.
    Do not use if shrinkwrap is broken or missing!
    I'm learning how to fish, too!

  32. #32

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    kewalk, you seem to know what im talking about. Using my modified program, once its paused, it will no longer start or stop. status is always paused.
    im going to try:

    another way to do this

    http://www.devx.com/vb2themax/Tip/19045 by Francesco Balena

    looks like less code

    (if you're interested)
    Yesterday 08:42 PM

  33. #33

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    Capp, it didnt say anything was pending

  34. #34

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    Im not exactly sure what i would use for the usage code just by looking at the remmed code. im assuming i would need 4 command buttons and a text box for the service name. Please help?? Thnx

  35. #35
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Controling services in vb

    Quote Originally Posted by lokeycmos
    Capp, it didnt say anything was pending
    Sorry I couldn't be of more help. If you've found some code that works better, go for it.
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  36. #36
    Addicted Member kewakl's Avatar
    Join Date
    Oct 2006
    Location
    between keyboard and chair
    Posts
    220

    Re: Controling services in vb

    @Capp I did see the Pending status, but I knew that we were just looking at the status too quickly. but i did also see that the service would NOT unpause even with Stop/Start sequence. But now it is OK. go figure.

    I did not intend to hijack your help. I offered that other way to try to help lokey understand the concepts.


    @lokeycmos
    as above
    from the devx page - the green remmed out section

    basically
    to start
    ServiceCommand "Schedule", 0

    to stop
    ServiceCommand "Schedule", 1
    so in your code
    put
    ServiceCommand "Schedule", 0 to start a service

    ServiceCommand "Schedule", 1 to stop a service

    and additionally

    ServiceCommand "Schedule", 2 to pause a service

    ServiceCommand "Schedule", 3 to continue a service
    Do not use if shrinkwrap is broken or missing!
    I'm learning how to fish, too!

  37. #37
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Controling services in vb

    Quote Originally Posted by kewakl
    I did not intend to hijack your help. I offered that other way to try to help lokey understand the concepts.
    I didn't take offense to it at all
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  38. #38

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    St Cloud, Minnesota
    Posts
    20

    Re: Controling services in vb

    Thanks for all your help Capp and kewakl. I ran into an old friend at school who is a VB wizard. I told him about my VB service project. He got back to me with a 2003.net program that he said took 5 minutes. He recommended i should check into VB 2005 express because its better and Free. So i took his advice today and d/l it. I took his 2003.net code and converted it to 2005. I just modified his form a little and added a couple lines of code for personal preference. It works great. Although i learned more doing it myself with your help, than my friend doing it for me. If your interesed in either the 2003.net or my 2005 source, just post. Here is my compiled version.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width