logo
vbRad Home
Source Code
Book Reviews
Forum
Links
About Us
Contribute

Compare Databases with SQL Effects Clarity
 
 The Easiest Way to Create an NT Service With VB6

Posted on
5/30/2002
Author:
Robert Gelb
Email:
Not Shown
Applies To OS:
NT, 9x, 2000
Product:
5, 6



Download... necessary files (43 kb)

The .NET hype is now almost deafening. One of the features touted is the ease of creating an NT (or Win2k, XP) service. But guess what? It is still easier to create an NT service with VB6. This method has been available since 1996 and the coolest thing is that you can make any old VB6 exe a service with NO CHANGES TO YOUR CODE. The general process works in the following manner. First you need to obtain the following files (included as a zip in the download at the top): Srvany.exe, Instsrv.exe, Srvinstw.exe and place them into the same folder. These files are normally located on the NT Resource Kit CD. Then you crank up the wizard, set some parameters and voila - you have a no hassle NT Service.

Don't be scared by the 16 steps below. Most of them is just walking through a wizard.

  1. Like I said, obtain the files listed above and place them into the same folder. I normally create a folder called c:\winnt\system32\Service Installation Kit.
  2. Run SrvInstw.exe
  3. Check Install Service radio box, click Next
  4. Check Local Machine radio box, click Next
  5. Enter the name for your service, click Next
  6. Click Browse and select srvany.exe (which is in the same folder as SrvInstw.exe, remember?), then click Next
  7. Click Service is its own Process radio box, click Next
  8. Select what user account you want this service to run under. If this service is to have its UI, check the Allow Service to Interact with Desktop checkbox. Normally the services run under System Account
  9. Select the startup option, click Next
  10. Click Finish. But you are not finished just yet
  11. Open RegEdit. Navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
  12. Locate the service you just created in the Services branch, click on it.
  13. Create a new key called Parameters
  14. Click on Parameters and create a new string value called Application
  15. Double-click on Application and set it equal to the full path of your VB executable (e.g. c:\...\MyApp.exe)
  16. Now go to Services applet and you can start your service.

FAQ

Q. I logged off. Is there a way to determine from a different computer whether the service is running or alive?
A. In our context, that's two different questions. One is whether the service is running, the other is whether the application is alive or just hanging there. The reason these are two different things is because the actual service is running SRVANY.EXE, not your VB6.EXE. So the service can be running while your EXE could be hung.

So let's address whether the service is running. There are utilities out there that do that but Visual Studio .NET has a pretty nifty way to manage services on other computers. Open Server Explorer. Right-click on Servers, select Add Server... After that select the added server and navigate to Services branch. From there you can view the service state.

Now let's address a more complicated question of whether your application is alive. This is a pretty tricky task since you are logged off and have no visual cues. One method that I use is simply add Winsock control and have it listen on some port that no other app will use. Then create a client test app that tries to connect (using Winsock again) to the port on the computer where your service is. If the connection is rejected your app is dead.

Notes

More information on this technique may be obtained at the following sources:
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q137890
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q152460

Troubleshooting

There have been reports of VB6 based services terminating when the user logs off. The explanation given was that some apps incorrectly interpret the WM_QUERYENDSESSION and WM_ENDSESSION messages and terminate. Right off the bat, I have not seen this behavior with VB6 apps. Maybe it happens with VB5 apps - I don't know, since I don't use VB5. But if that is the case, all that needs to be done is subclass WM_QUERYENDSESSION and WM_ENDSESSION messages and junk them when they are received by the app. There is a pretty good explanation on how to do this and more here.

Again, I have not seen this problem, so it may be a non-issue for VB6 apps.

Download... necessary files (43 kb)





Add Your Comment  

Name: Email Address: all fields optional
Notify me via email when someone responds to this message (valid email required).

Enter the word:
 



Comments
#1. By Adrian. Posted on 3/9/2006 4:14:51 PM
Hi!
Why if my VB programs ends (due an error, by instance) the service remains active?
Thanks!

#2. By rizzo. Posted on 3/10/2006 7:33:33 AM
#1. The reason it ends is because your program is not the service, srvany.exe is. So make sure you catch all the exceptions and not allow the app to crash.

#3. By mamad. Posted on 6/7/2006 4:26:02 PM
Thanks a lot for your guide.

#4. By Niraj Mehta. Posted on 7/28/2006 11:46:34 AM
Thanks for your great support.

I have very little problem, when i start my service at that time it will give following error:

The System Tray Application - Server service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service.

#5. By Author. Posted on 7/31/2006 4:34:11 PM
To Niraj Mehta. The problem you described usually occurs when the app crashes on startup or it exits after startup.

#6. By al. Posted on 8/30/2006 10:51:43 AM
This is great,
I have a VB6 app I need to make in to a service, it is actually a Solomon module named application server it is an executable that needs to authenticate to the Solomon databases prior to running. Have you got any Idea haw I might be able to do this?

#7. By al. Posted on 8/30/2006 10:54:32 AM
Also forgot to mention, is this going to work with windows 2003

#8. By Author. Posted on 8/31/2006 8:48:21 PM
Set the service to run under certain username that has access to the resource you need.

#9. By Lord MyKee. Posted on 9/22/2006 4:18:43 PM
I followed the steps but the service cannot start my vb6 app...
I've got an error message saying "Error 1053: The service did not respond to the start or control request in a timely fashion"

The exe itself works fine (was correctly compiled and can be launched properly)
Are there any limitations on the type of things that the application features so that it cannot be transformed to a service?

PS:I am running W2K Pro.
Thanks a lot for your app and help.
Regards,

MD.

#10. By Author. Posted on 9/24/2006 5:39:49 AM
To #9. The only difference really is that there shouldn't be a UI and if the app accesses something that cannot be it may choke. Another issue are the permissions of the service account it is running under. See if you can log your app and figure out where it chokes?

#11. By Andrew. Posted on 2/2/2007 6:58:46 AM
another way to test if its alive, set a timer for 1000ms and put as the following code in it "Beep"

#12. By Dana. Posted on 2/20/2007 9:04:02 PM
Hi!

I've encountered the same error message as Lord MyKee (error 1053), using W2K Professional. My (most simple) application has no UI and nothing to access. It's just:

Sub Main()
Beep
End Sub

The service account is System.
When I start the service using the 16 steps above, I hear the beep but then I get the error message.

Thank you for your support

#13. By Anonymous. Posted on 2/20/2007 10:13:45 PM
To Dana and Lord MyKee.

The service is behaving as it should. The point of the service is that your application should not end. When you say

sub main
been
end main

this ends the application and thus the service. You must place your app into some sort of waiting state, so that it would not quit. Your app should be responding to events and serving the clients. Thus, it is a service.

#14. By Alonso. Posted on 3/15/2007 9:26:11 AM
i also got the error 1053, and i have readed your solutions to this error.

but i don't know you to let the application running.

could yoou help me??

#15. By Dana. Posted on 3/15/2007 3:02:09 PM
Hi, Alonso

Try vb .net, it works much better in what concerns services

#16. By Jalal. Posted on 5/24/2007 4:00:36 PM
I am using vb6 and create a service program to make it automatic. the same program i try to invoke from system
tray but unfortunately the user logoff the server then my system tray icon disappeared. what is wront with my code
thanking u in advance

#17. By jalal. Posted on 5/29/2007 4:43:28 AM
any fix for srvany.exe when i logoff the pc (GUI is gone)

#18. By Anonymous. Posted on 5/29/2007 3:56:53 PM
To #17. Jalal, the solution posted here works for my app - it does not terminate when I log off.

#19. By malmee. Posted on 8/2/2007 10:05:44 AM
I followed the steps but the service cannot start my vb6 app...
I've got an error message saying "Error 1053: The service did not respond to the start or control request in a timely fashion"

OS is windows xp

#20. By holly. Posted on 8/28/2007 8:07:39 PM
ok it says to select to start up option. but when im done installing it, it closes out. how do i use the start up option?

#21. By Walter. Posted on 9/5/2007 7:39:26 AM
Worked like a dream, thanks for the info!

#22. By Joel. Posted on 9/5/2007 4:03:09 PM
HI, my app is a WebService in vb.net and when de app goes to stop, start, reset de Service i have a error message:
Cannot open SIServices service on computer '.'

The computer system is in Win2000, can you help me??

thanks
Joel

#23. By Author of the Article. Posted on 9/5/2007 4:17:09 PM
To #22.

Joel, a WebService should be hosted in IIS. If you wrote your own hosting environment, then the service is probably not set up right. Try going through the steps again.

#24. By hussain. Posted on 10/8/2007 4:15:31 AM
Thanks for your support.

When i try to run our VB application service with local system account, it is giving error, "my application service on local computer started and stopped. Some services stop automatically if they have no work to do, for example, performance logs and alerts service". We are using Window XP Pro.
What could be the issue, pls guide me. Thanks in advance

#25. By hussain. Posted on 10/8/2007 4:15:42 AM
Thanks for your support.

When i try to run our VB application service with local system account, it is giving error, "my application service on local computer started and stopped. Some services stop automatically if they have no work to do, for example, performance logs and alerts service". We are using Window XP Pro.
What could be the issue, pls guide me. Thanks in advance

#26. By Anonymous. Posted on 10/11/2007 6:43:38 PM
To Hussain, #25. When you start your application on its own does it do something and then quit? If so, it will behave the same way if you start it from the services applet.