How the program runs your application as a service
by Chris J. Duke
Translation: Atai
Original http://vbwire.com/advanced/howto/service.asp
This article is the first part of the "how-to" special series.
If you are already proficient in VB, and now you are trying to post your final masterpiece as commercial software. At the end of the day, your boss told you that your application needs to be run as a service under Windows NT. You tell your boss: "No problem". So you go back to your work location and find that your VB program can't do it. So you go to Microsoft Knowledge Base for answers, you search on the Internet, you search in the newsgroups, and finally you are very excited here, this is how the VB program to serve this series. Now what you think is impossible becomes simpler than you ever imagined.
This article will focus on how to run your visual Basic4.0 or Visual Basic5.0 program as a service in Windows 95 and Windows NT3.51 and later. When your application serves as a service, your application can be started before logging on, so that your program can run at startup. This is useful for network programs, such as Web servers, mail servers, or any other application that needs to run all the time.
Please see the next article, this article is about how to download a free ActiveX control loaded into your form immediately put your VB application very simple set to NT services.
Window95
You may want to ask yourself why you need to run your application as a service on the Windows95. You may also ask if Window95 like Windownt can run your application as a service.
Yes, Window95 can run the application as a service, like Windows NT. The reason why you will be able to do this is that you will start your application before logging in. This is often used on Web servers and mail servers. Because of the lack of security in Windows 95, your application as a service will become quite simple. However, this situation is not appropriate for Windows NT (see below).
It's easy to run your application as a service in Windows95. All that needs to be done is to modify the registry. But for Windows NT it's a hell, and for Windows95, don't think it's no trouble. This section will provide you with an important job for WINDOWS95.
To use your application as a service in Windows95, add a string value to your registry:
For example. Suppose your application is called "Microspud", create a String value of: Mircospud, and enter the full path of the executable file into the registry's data field. Here's the steps:
Step 1: Add a new string value
Step 2: Enter the path to the executable file
Step 3: Complete, reboot, MICROSPUD will be launched before landing
Windows 95 tray icon
Now, do you remember the "quirk" mentioned earlier? It's here. If your application starts and you have a tray, you will want to read the exact details.
When your application starts first, you are most likely to add your tray icon to the Form_Load () or main () event. If the WINDOWS95 is set to log on automatically by restarting, the problem will not exist. However, when a user forces a login, the following things will happen:
Windows95 start
Your application as a service
Your application view loads the tray icon, but fails because no pallet icon has been loaded
The Login dialog box appears
T user enters user name and password
Desktop starts loading
Y your icon will not appear in the tray area
The solution is to call the Shell_NotifyIcon () API repeatedly until it returns a true Boolean value. One of the solutions I found to implement it was to add a timer. The timer is initialized to the start state with a time interval of 5 seconds. Call Shell_notification () when the event (Timer1_timer) of the timer is invoked. Closes the timer once the API returns a true value.
Window NT
You may have found that setting up in Windows95 is just one step, but for WindowsNT, it's totally different from Windows 95. Some people may call it the devil, and this method can be used in WindowNT3.51 or later versions.
Why is Window NT a big difference? One reason for this is that all services under NT are managed by the Service Control Manager, SCM. However, your application information is still in the registration list.
There are more keys that allow applications to serve as NT services.
Microsoft realizes that this problem is not only happening on VB applications, but also on most applications. So Microsoft is proposing a solution, and the solution is doing very well. They developed a small application. This application is published in the Windows NT resource bundle and can be obtained later in the chapter. This application is called
Srvany. EXE, as a host (or service pack) for your application. In other words, it becomes a service program that handles all your hard work (Dirty Work) and communicates with the SCM. When it starts, it finds the path to your application in the registry. If an application is found, it is started. The only drawback to using it is that when the SCM shuts down the Srvany, it will use the TerminateProcess () API to kill your VB application. This is a very unfriendly way to close an application.
The configuration of your application as a service is directly forward (straight forward), but not as simple as Windows95. Before I show you a step-by-step operation. It is very important to understand what is going on.
There are two programs that Microsoft has developed, and these two programs deceive it. First, the Srvany.exe,srvany I mentioned is located under the Winnt\System32 folder. Your application is also under this folder. In the end you want to turn your application into a service, it is really an "essence", and this "essence" is a real service. When your service starts, it actually starts with Srvany,srvany that will start your application.
The second program was developed by Microsoft, called INSTSRV.EXE. This program installs Srvany as a service, instsrv. EXE is a command line that implements its functionality. Your previous use is not required unless you intend to add another service that uses Srvany, or uninstall the previously added service program. Keep her manually!
Srvany provides a document without this document describes the steps you use to it. This will guide you through the step-by-step installation of Srvany (Srvany is based on the program "Mircospud").
Assume:
· You log in as a administator or you will not be able to install a service
· Your VB Application "Microspud" is located in C:\Program Files\microspud\mspud.exe
· You've put Srvany. Exeinsrtsrv. EXE installed in the Microspud folder
Step 1: Use Instsrv. EXE installation Srvany. EXE as a service
Step 2: Verify that your service has been added to the Service Console (Control Panel)
Step 3: Configure your service settings (Figure defaults)
At this point, your service has been created. However, if you are were to try to start it now, it would fail. You are next have to tell Srvany where your VB application resides and so it can start it the SCM starts Srvany. Follow these steps to complete the installation of your new service:
At this point, your service has been created. But when you try to start it, it will fail. Your next step must tell Srvany where your VB application is, so srvany can start your application when the SCM starts Srvany. Follow these steps to teach you to complete the installation of your new service.
Step 1: A add "Parameter" keyword
1. Start Registry Editor
2. In hklm\system\
Currentcontrolset\services\microspud Find your service.
3. Create a "Parameters" keyword
Step 2: Add "Application" value:
1. Open the "Parameters" keyword
2. Create a "application" string value (REG_SZ) in the "Parameters" keyword
3. Edit "Application" and the full path to the application's executable file (including extended paths)
Step 3: Add an optional value (Optional value)
Although not necessary, you may want to add an optional value to the "parameter" keyword (Optional value)
1. Appparameter (string)-describe any parameters of your application
2. Appdirectory (String)-describes the current directory of your application
Congratulations, you have successfully converted your VB program to Windows NT service. At this point, you may be very eager to test it. So, shut down the computer, and then reboot. Your application will start before you log in and start running before your left side appears.
There's still a lot of other information you need to know. These are about Srvany as well as new NT Service applications: (although Microspud uses consistent examples)
The registry value is not case-insensitive
In order to uninstall your service, use INSTSRV. EXE, such as Instsrv Microspud REMOVE
There are three ways to start your service:
1. From the Control Panel desk
2. Use SC.EXE, for example: SC start Microspud
3. Use NET.EXE, such as: NET START microspud
There are three ways to stop the service
1. From the Control Panel desk
2. Use SC.EXE, for example: SC stop Microspud
3. Use NET.EXE, such as: NET STOP microspud
. When the service is stopped. It terminates the application through the Win32 terminateprocess () API. This way of terminating an application is very rude. For example, it will allow the Application pop-up dialog box to remind the user to save. Therefore, it is recommended that you close the application before shutting down the service.
You may install srvany multiple times. EXE, and use different registry parameters (for example, running different target programs)--just use different service names for each instance (e.g., MICROSPUD1,MICROSPUD2, etc.)
If your VB application interacts with the desktop. Be sure to configure it to do this by clicking the "Settings" button for the appropriate service. You should pay attention to these programming considerations. In order to capture the information we mentioned, you learn to use a program such as Spyworks.
For WIN32 graphics applications: After the current user logs off. All WIN32 high level windows will receive Wm_queryendsession and wm_endsession messages. Some WIN32 applications choose to terminate on top of these messages. For your WIN32 application to survive logoff, do not do: Instead, your Windows process (procedure) should invoke the default Windows process in these messages
For WIN32 console programs (such as character mode): When the currently logged in user has logged off. All the console programs receive Ctrl_logoff_even events from the console.
If your console application has registered a console event handler (via SetConsoleCtrlHandler), it must ignore _event in order to survive the logoff.
If your console program has registered a console event (via Settconsolectrlhandler), it will have to ignore the Ctrl_logoff_even event and get it off the table.
Microsoft also recommends that you:
If Srvany fails to start your application, try to create a directory in the registry (see the "Appdirectory" registry key) as the current directory. Srvany may run under an account that is different from the current login, so the environment variable should be set differently, for example, the system may not be able to find the DLL required by the application, and it may be helpful to run it from the directory of the application.
Because of the limitations of the WindowsNT service, applications can interact (with consoles, read keyboard input, and so on) or network access (but not both).
The dependencies of the service
An undocumented feature for Srvany (which applies to any service, actually) you should know about is the "DependOnService" Value. Suppose your service depends on the other services. How does your service know which other services are available when your service starts? For example, let ' s say your service is a Web server. You'll want to make sure that TCP/IP is available before your service starts.
You should know that an informal feature of Srvany (which actually applies to all services) is a "dependonservce" value. Suppose that your service relies on other services. How does your service know that other services can be leveraged when your service is started? For example, let's say your service is a Web server. You will want to make sure that TCP/IP is available before your service starts.
In the service key (key) The binary value can be added to your service, there are other values such as "DisplayName", "objectname" and so on. Let's take a look at the screenshot below. It reflects a reliance on TCP/IP and event logs for the Microspud service. To find the name of the service to enter into the dialog box, use the service's registry key (key). For example, under hklm\system\currentcontrolset\services TCP/IP, its key value is "Tcpip". Or for the event log, you will enter "EventLog". To describe a dependency, use 00 as the delimiter, and a period (.) is displayed in the ASCII display.
Windows and Windows NT Services
This article focuses on configuring your VisualBasic4.0 or 5.0 applications to perform as services in WINDOWS95 and Windows NT3.51 or later. With the release of Visual Basic5.0, but VisualBasic5.0 's new compatibility is never used by VB programmers, you still can't create a local NT service. According to Desaware's ad. Their new SpyWorks5.0 products allow you to bypass Srvany to create visual Basic NT services. I will review this product (OCX service also incidentally) and update this article in the future.
Your comments and feedback are welcome, however I ' m afraid that I cannot provide technical to support for Srvany and INSTSRV. Please contact the Microsoft technical support or search their knowledge base for help.
I hope you find this information useful. He is available free of charge to readers of the further VB website (http://vb/duke.net). All or part of the replicas are prohibited. Your suggestions and feedback are welcome. Still, I'm afraid I can't provide technical support to Srvany and INSTSRV. Contact Microsoft's technical support or ask for help at their knowledge base.
Download Srvany
In order to download SRVANY.EXE, instsrv. EXE, as well as supporting documentation. Click on the link below. When this article was written (Q2 1997) was the latest version. Supports WindowsNT3.51 and Windows NT 4.0
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.