How to run your app as a service (1)

zhaozj2021-02-16  84

How to run your app as a service

By Chris J. Duke

             

Original http://vbwire.com/advanced/howto/service.asp

This article is the first part of the "How-to" special series.

If you have been proficient in VB, and now you want to post your last masterpiece as business software. In the last time, your boss tells you: Your application needs to run as a service in Windows NT. You tell your boss: "No problem". So you returned to your work position and then found that your VB program can't do it. So you seek an answer from Microsoft Knowledge Base, you search online, you search in the newsgroup, and finally you are particularly excited here, this is how the VB program is served as a service. Now what you think is impossible to make it more simply than you have imagined.

This article will focus on how to run your Visual Basic4.0 or Visual Basic 5.0 program as a service in Windows 95 and Windows NT3.51 and or higher. When your application is a service, your application can start before logging in so that your program can run when starting. This is useful for network programs (such as web servers, mail servers, or any other applications that need to be running).

Please refer to the next article, this article is about how to download a free ActiveX control is loaded on your Form, which is simple to set your VB app very simple to NT service.

WINDOW95

You may have to ask yourself, why you need to run your application as a service on Windows 95. You may also ask if Window95 is like WINDOWNT to run your application as a service.

That's right, Window95 can run the application as a service as a service like Windows NT. Why you will be able to do this is that you will start your app before logging in. This often uses on the web server and the mail server. Because Windows 95 security lack of lack, putting your application as a service will become quite simple. But this situation is not suitable for Windows NT (see below).

It is very simple to run your application as a service in Windows95. All needs to be done is to modify the registry. But for Windows NT, it is a hell, for Windows95, don't think it is unreasonable. This section will give you an important work for Windows95.

In order to use your application as a service in Windows95, add a string value to your registry:

HKLM / Software / Microsoft / Windows / CurrentVersion / RunServices

E.g. Suppose your application is called "Microspud", create a string value, value: mircoSprud, and enter the full path of the executable to the data field of the registry. Below is its steps:

STEP 1: Add a new string value

STEP 2: Enter the executable path

Step 3: All completed, restart, Microspud will start before landing

Windows 95 tray icon

Now how do you remember "quirk" mentioned earlier? It is here. If your application starts, and if you have a tray, you will want to read this pair of detailed descriptions.

When your application starts, you most likely add your pallet icon in form_load () or main () event. If the Windows95 is set to automatically login, then this problem will not exist. Despite this, when a user is forced to land, the following things will happen:

Windows95 starts your app as a service

Your application view loads the pallet icon, but failed because there is no tray icon to be loaded.

Login dialog box appears

T user input username and password

Desktop start loading

Y Y your icon does not appear in the tray area

The solution is duplicate call shell_notifyicon () API until it returns a True Boolean value. I found a solution to implement it to add a timer. The timer is initialized to start state, and the interval is 5 seconds. The shell_notification () is called when the timer is called (Timer1_timer). When the API returns the TRUE value, turn off the timer.

WINDOW NT

You may have discovered that you only need one step in Windows95, but for WindowsNT, it is totally different from Windows 95. Some people may be called the devil, which can be used on Windownt3.51 or higher.

Why is WINDOW NT a big difference? One reason is that all services under NT have a service control manager, scm. But your application information is still in the registry.

More Keys allows applications as NT services.

Microsoft realized that this problem did not only occur on VB applications, but also occurred in most applications. So Microsoft propose a solution and this solution is very good. They have developed a small app. This application is released in the Windows NT resource package, they can get in the chapter. This app is called

Srvany.exe, which is a host (or service package) for your application. In other words, it becomes a service program that handles all of the hard work (Dirty Work) and and communicates with SCM. When it starts, it finds the path to your application to the registry. If you find an app, start it. The only disadvantage of using it is that when SCM turns off Srvany, it will use the TerminateProcess () API to kill your VB application. This is a very unfriendly way to close an app.

The configuration of your application as a service is directly forward forward, but it is not as simple as Windows95. Before I show you step by step. It is very important to understand what you continue.

This has two Microsoft developed programs, which deceives it. The first is the SRVANY.EXE I mentioned, and SRVANY is located under Winnt / System32 mesh folder. Your application is also under this folder. In the end you want to turn your app to the service, it is indeed a "essence", this "essence" is a real service. When your service starts, it actually starts SRVANY, SRVANY will start your application.

The second program is Microsoft, called INSTSRV.EXE. This program installs srvany as a service, INSTSRV.EXE is a communication through the command line. You are not needed before, unless you intend to add another service using SRVANY, or uninstall the previously added service programs. Keep her manually!

SRVANY provides a document without this document describes the steps you use it. Here will guide you step by step (SRVANY is based on program "mircoSpruse").

Assumption:

· You log in as an administator, otherwise you will not be able to install a service

· Your VB application "Microspud" is located in C: / Program files / microspud / mspud.exe

· You have already put Srvany. Exeinsrtsrv. EXE installed under the Microspud folder

Step 1: Using INSTSRV. EXE installs SRVANY. EXE as a service Step 2: Confirm your service is added on the service console (control panel)

Step 3: Configure your service settings (the picture is default)

At this point, your service has been created. However, if you were to try to start it now, it would fail. You next have to tell SRVANY where your VB application resides, so it can start it when the SCM starts SRVANY. Follow Thase 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. You must tell SRVANY where your VB app is in your next step, so SRVANY can start your application when SCM starts SRVANY. Here are these steps to teach you to complete your new service.

Step 1: a Add "parameter" keyword

Start registry editor

2. Find your service in HKLM / System / CurrentControlSet / Services / Microspud

3. Create a "parameters" keyword

Step 2: Add "Application" value:

1. Open "Parameters" keyword

2. Create a "Application" string value (reg_sz) in the "parameters" keyword (REG_SZ)

3. Edit "Application" and the full path to the executable of the application "including the extended path)

STEP 3: Increases Optional Value, although it is not necessary, you may want to add an optional value for "parameter" keywords (Optional Value)

1. AppParameter - Describe any parameters for your application

2. Appdirectory (String) - Describe the current directory of your application

Congratulations, you have successfully converted your VB program to Windows NT service. At this point, you may have a very urgent test. Therefore, turn off the computer and restart. Your app will start before logging in and start running before your left.

There are still many other information you need to know here. At this time about SRVANY and the new NT service application: (although Microspud uses the consistent example)

Registration value has no cases

In order to uninstall your service, use INSTSRV. EXE, such as INSTSRV Microspud Remove

There are three ways to start your service:

1. Service desk from the control panel

2. Use sc.exe, for example: SC Start Microspud

3. Using Net.exe, for example: NET Start Microspud

Three ways to stop service

1. Service desk from the control panel

2. Use sc.exe, for example: SC Stop Microspud

3. Using Net.exe, for example: Net Stop MicroPud

When the service is stopped. It will terminate the application via the Win32 TerminateProcess () API. This way to terminate the application is very rude. For example, it will allow the application pop-up dialog to remind the user to save. Therefore, it is recommended to close the app before closing the service. You may install SRVANY multiple times. Exe, use different registry parameters (for example, running different target programs) - only uses different service names for each instance (for example, Microspud1, Microspuds, etc.)

If your VB application and desktop interaction. Be sure to configure this feature by clicking the "Settings" button of the appropriate service. You should pay attention to these programming. In order to capture the news we mentioned, you learn to use a program such as Spyworks.

For Win32 graphics applications: After the current user is logged out of. All Win32 high-level windows will receive a WM_QueryEndSession message. Some Win32 applications are selected on these messages. For your Win32 app, you should not be able to log out, don't do: instead, your Windows process should call the default Windows process in these messages.

For Win32 console programs (such as characters mode): When the user currently logged in is already logged out. All console programs receive Ctrl_logoff_even events from the console.

If your console program has already registered a console event (via settconsolectrlHandler), it will have to ignore Ctrl_Logoff_Even events and even smashed.

Microsoft also recommends you:

If SRVANY starts your application failed, try to develop the directory in the registry (see "AppDirectory" registry keyword) as the current directory. SRVANY may run under an account, this account is different from the current login user, so environment variables should be set to different, for example, the system may not find the DLL needed by the application, and run from the application's directory. It may be helpful.

Because the application can interact (with console, read keyboard input, etc.) or network access (but both cannot be carried out simultaneously).

Service dependence

An informal feature you should know (actually suitable for all services) is the "dependonservce" value. Suppose your service rely on other services. How do your services know other services when your service starts? For example, let's say your service is a web server. You will want to ensure that TCP / IP is available before your service is started.

Under the service key (key), you can add it to your service, and other values ​​such as "DisplayName", "ObjectName", etc. Let's take a look at the screenshot below. It reflects the Microspud service, a dependency on TCP / IP and event logs. In order to find the name of the service to enter to the dialog, use the service's registry key (key). For example, TCP / IP under HKLM / SYSTEM / CURRENTCONTROLSET / SERVICES is "TCPIP". Or for an event log, you will enter "EventLog". For a description of the dependence, use 00 as a separator, one cycle will be displayed in the ASCII display (.)

Windows 95 and Windows NT Services

This article pays attention to configuring your VisualBasic4.0 or 5.0 applications is executed as a service in Windows 95 and Windows NT3.51 or later. With the release of Visual Basic 5.0, new compatibility for VisualBasic 5.0 will never be used for VB programmers. Despite this, you still can't create local NT services. According to Desaware's ad. Their new spyworks5.0 products allow you to bypass SRVANY to create a Visual Basic NT service. I will review this product (OCX service is also the way) and updated this article in the future. I hope that you can find this information is useful. He is available free of charge for a further VB website (http://vb/duke.net). All or part of the replica is prohibited. Welcome your suggestions and feedback. Despite this, I am afraid I can't provide technical support for SRVANY and INSTSRV. Green contact Microsoft's technical support or helps them in their Knowledge base.

Download SRVANY

To download Srvany.exe, INSTSRV.EXE, and support documentation. Click to connect below. When writing this article (Q2 1997) is the latest version. Support WindowsNT3.51 and Windows NT 4.0

SRVANY.ZIP (24K)

转载请注明原文地址:https://www.9cbs.com/read-15270.html

New Post(0)