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.
- 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.
- Run SrvInstw.exe
- Check Install Service radio box, click Next
- Check Local Machine radio box, click Next
- Enter the name for your service, click Next
- Click Browse and select srvany.exe (which is in the same folder as SrvInstw.exe, remember?), then click Next
- Click Service is its own Process radio box, click Next
- 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
- Select the startup option, click Next
- Click Finish. But you are not finished just yet
- Open RegEdit. Navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
- Locate the service you just created in the Services branch, click on it.
- Create a new key called Parameters
- Click on Parameters and create a new string value called Application
- Double-click on Application and set it equal to the full path of your VB executable (e.g. c:\...\MyApp.exe)
- 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)