电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> VB编程>>How-To Run Your Application as a Service: Part II:

How-To Run Your Application as a Service: Part II

来源:www.vbwire.com | 2006-11-13 | (有2742人读过)

How-To Run Your Application as a Service: Part II
by Chris J. Duke

Services        This article is the third in the "How-To" series exclusively by Advanced Visual Basic.

       Last month I introduced the use of SRVANY (available in the Windows NT resource kit) as a means to run your Visual Basic application as a service. I briefly mentioned that there was another way to do it, in the form of an ActiveX OCX developed by Microsoft. As it turns out, there are two other ways as well. In this article, I'll introduce these additional methods that you can make an educated decision on which method is best for your situation.

       First, let's get a real good overview of what's out there. There are four options, but which one is best?

       What is amazing is that prior to Visual Basic 5.0, there weren't many options -- just one, in fact. Although the new Microsoft OCX should work in the 32-bit version of Visual Basic 4.0, it wasn't available prior to v5.0. Now, with Visual Basic 5.0, there are several other options available to us. Let's take a look at them.

SRVANY

       While I often repeat myself in telephone conversations, especially after staying up until the wee hours of the morning writing articles for AVB, I try not to on-line. So, if you are interested in how SRVANY works, take a look at my July 1997 article. It goes into great detail on using SRVANY to turn your Visual Basic application into a service.

       What I didn't get into, however, are the real downsides to using SRVANY. To begin with, Microsoft's licensing for distribution of the two key elements (SRVANY.EXE and INSTSRV.EXE) are a little hazy. Since SRVANY is part of the NT Resource Kit (RK), it included with Microsoft's rather harsh licensing terms:

REDISTRIBUTION LICENSES
-----------------------
Unfortunately the Resource Kit team is unable to provide licenses to redistribute one or more Resource Kit utilities to customers who do not have the same Resource Kit. Remember that the Resource Kit already provides a site license, which means that you can freely copy the Resource Kit utilities within your company (see license agreement for legal details).

Love,
Bill

       Reading that, it is clear that in order to use SRVANY in your own application, you have to make sure every user (outside of your own company) has a copy of the NT RK. This seems a little harsh to me, since prior to Visual Basic 5.0 it was the only way to get around a Microsoft limitation -- where service applications have to be so tightly bound to the service control manager (SCM). Note that this was the redistribution clause for the RK from NT 4.0 -- I believe the license agreement prior to this one was a little bit friendlier (Bill was more humble?).

       Also, while SRVANY allowed your application to run as a service, your application was not running as a service. SRVANY was the application that was actually running as the service application, communicating with the SCM. Your application was shelled by SRVANY -- and SRVANY terminated (ugly!) your application when the SCM informed SRVANY to stop. Furthermore, your application could not respond to pause/continue requests from the services control panel applet. Nevertheless, there are other options now available, even for developers who are still using Visual Basic 4.0.

NT Service Control Module

       Microsoft strikes again! But who knew about it? Amazingly enough, MSDN subscribers had access to a very little known gem since January of this year. It still seems like no one knows about it. That's a shame for Visual Basic developers, because I personally feel it's their best bet! One of the reasons might be because it's a multi-step process to get it. Read on!

       Last year, Mauricio Ord篑ez (Microsoft Consulting Services) created an ActiveX control called "NTSVC.OCX" for the purpose of replacing the use of SRVANY, for the reasons stated above. The intended audience? You, the Visual Basic developer. How is it that very few people know about it?

       If you have the MSDN Library CD-ROM, search for "NTSRVOCX". All the information is there, complete with documentation and Visual C++ source code. As opposed to SRVANY's confusing distribution clause, there appears to be nothing stated against redistributing the OCX with your application. The catch? No support. Since you get the full C++ source code, however, you at least have the means to support it yourself. A Knowledge Base article is available at Microsoft which basically says the same thing. It is article Q170883.

       The first hurdle you have to get over is getting the source code. The MSDN license agreement prohibits me from re-distributing it, so you'll have to shell out some money for a MSDN subscription, which includes the MSDN Library on CD-ROM. While it may cost you more than you would like, the MSDN subscription is well worth it for Visual Basic developers. Not only do you get the full Visual Basic 5.0 documentation in electronic form (and searchable), you also get full documentation for all other Microsoft development languages (such as Visual C++ / J++) and hundreds (if not thousands) of technical articles such as the one being referred to right now.

Update: September, 1997: Now that MSDN Online is free (after registering), you can obtain the source code for free.

Update: March, 1998: The above link keeps changing courtesy of Microsoft (the link now works as of 3/2/98). Also, the link to the source on the page has an error (a space before the .EXE) which makes it rather difficult to download. If you are interested in the source (which includes the documentation) or the compiled OCX, just click away! I promise not to change my links.

       Next, once you get the source code, you have to make the OCX. That's right, VB Kids, you get the Visual C++ source code, but you don't get the OCX (I think this was done to make us Visual Basic developers sweat since we normally have it easier than Visual C++ developers). But wait! All you have is Visual C++ 5.0? It won't compile without some tweaking (I had a friend of mine fix it). An AVB reader noted, "All it took was to change a variable called 'default' (which is now reserved) to something else, e.g. 'def'". If you have Visual C++ 4.0, you'll be okay.

       Now that you have the ActiveX control, NTSVC.OCX, just plop it on one of your forms, follow the excellent documentation, and you're set. It really only takes 10-15 lines of code to get it going and is very straight forward, even if you have never dealt with SRVANY or developing a Visual C++ service application. Like I said, the hardest part of this method is getting the OCX. Should Microsoft make this OCX available for re-distribution, it will be here. The key word there is "should" :)

VB Source Code

       Strike three for Microsoft? With SRVANY, each user supposedly needs a NT Resource Kit License. With the OCX, you need a MSDN Library subscription. Both methods cost money. This third method is completely free! The source code is entirely in Visual Basic 5.0 using callbacks and the Win32 API. The code and documentation is all on-line in a Microsoft Knowledge Base article. It's free to get, and free to use. So, what's the catch this time (there's always a catch)?

       The catch? I'm so glad you asked. Just read the Knowledge Base article. It's article Q175948. Here's an excerpt:

Microsoft does not currently recommend running Visual Basic applications as Microsoft Windows NT Services because they may exhibit unstable behavior when installed and run as Microsoft Windows NT Services. Microsoft Visual Basic 4.0 does not support Callbacks and neither Microsoft Visual Basic 4.0 or Microsoft Visual Basic 5.0 is thread-safe. This behavior is by design.

       There you have it! The article goes on to give you the complete source code (in Visual Basic 5.0) to pull it off. It's interesting that Microsoft starts off telling you it is such a bad idea, but then has complete source code to do it. The article does state the nasty things that can happen (all very minor, in my opinion) when using this method, and does state a work around -- using the ActiveX control as stated above.

       While Microsoft recommends against using the Visual Basic 5.0 method of turning your application into a service, it's actually better than using the ActiveX control. The reasons are because you have total control over the source code, and you don't have to manage a separate (difficult to understand) Visual C++ project. Just play it safe, and your Visual Basic application shouldn't encounter any problems when running as a service. There are some good tips in the Microsoft Knowledge Base article, such as making your application non-interactive, and reporting errors to the NT event logs or your own log files. Should you need to interact with your service application, create a separate application as many other services have done (WebSite, IIS, WebBoard, SQL Server, and many other mail, FTP and chat servers).

SpyWorks 5.0

       Lastly, I have included a method that I'm afraid I know very little about. Daniel Appleman's company, Desaware, recently released an update to their popular Visual Basic add-on called "SpyWorks". SpyWorks, like Dan's popular Visual Basic books, allows Visual Basic developers to take their applications places they never could before.

       An addition to SpyWorks 5.0, Dynamic Export Technology allows you to export functions from your Visual Basic created ActiveX DLL's. With this technology, Visual Basic developers can create export function libraries, control panel applets, Internet ISAPI filters, and make true NT Services.

       I have not had time to explore this last method, but I thought it would be better to mention it than to not mention it at all. If I had to guess, I would say that SpyWorks 5.0 will allow you to achieve results similar to using the Visual Basic source code, or perhaps more stable such as the ActiveX control. Give it a try and let me know how it works out for you!

Services: Part II

       Whew! Needless to say, there are now many ways to turn your Visual Basic 4.0 and 5.0 applications into Windows NT services. The method you decide to choose is up to you, but my recommendation is to use the ActiveX control or take your chances by using the Visual Basic source code.

       I hope you found this information useful. It is provided for free to readers of the Advanced Visual Basic web site. Reproduction in whole or part is prohibited. Your comments and feedback are welcome, however I'm afraid that I cannot provide technical support for any of the methods or products mentioned. Please contact Microsoft technical support, search their knowledge base for help, or check out VBChat.

VB编程热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2024 电脑爱好者 版权所有 备案号:鲁ICP备09059398号