首页 > 开发 > 综合 > 正文

create and Control Windows Services--introduction[

2024-07-21 02:21:28
字体:
来源:转载
供稿:网友
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • windows services (formerly known as nt services) can seem overwhelming and daunting to the uninitiated because of a vast array of callback functions and long parameter lists. even though creating services in c++ almost ensures top-notch performance, the complexity factor might outweigh that fact. however, with the recent release of the .net framework sdk, the complexity of creating and controlling services has diminished. if development time is of the essence, visual c#.net and the .net framework prove invaluable assets.

    what you need:
    .net framework sdk, windows nt4 or windows 2000   



    in this article, i'll demonstrate the necessary steps to create a windows service, then explain how to control your service by creating a useful utility that could prove invaluable as you test and debug your service. but first let me give you some background on windows services.

    most of us remember terminate-and-stay-resident (tsr) programs—ones that remained in memory after being executed but gave control back to the user. as useful as they were, you faced the occasional compatibility problem in the environment you ran them in. system architectures inevitably increased in reliability and multitasking capabilities, and the tsr gave rise to what we now call the service.

    a service is an executable that gives the operating system nonintrusive functionality. a service installed and configured to run automatically begins execution before a user logs on to the computer. some common uses for services include hardware control and monitoring applications; system-level utility, diagnostic, and reporting tools; and communication software such as a web or file-system server.

    although a service is an executable, you can't start and terminate it through traditional means. the first and most common way to start one: register it with the service control manager as an automatic service. this ensures the service starts as soon as the machine has finished the boot cycle and before a user logs on to the system. you can also start a service manually using the console command net start <servicename> or through the service applet within the windows administrative tools group.

    services also can allow you to pause and continue during execution, either manually or programmatically. but providing this functionality is up to the service designer—it's not required.

    just as a service doesn't start as other executables do, it also can't terminate the same way many other executables can. the service itself generally has no user interface associated with it, so it terminates when the user turns off or restarts the system, or manually stops the service using the service applet again or with the net stop <servicename> console command.

    you must follow special rules while creating a service to get it to run correctly within the system environment. the most important: you must install or register a service within the system it intends to run in. in other words, you can't just run a service by double-clicking on its executable file. furthermore, a service shouldn't provide a user interface. rather, it should do its reporting with the system event log or performance counters. i'm not saying services can't have a user interface, but each service is created within in its own windows station, so ui-related api calls might not succeed. finally, a service must support a number of methods i'll discuss later to control its execution life cycle.
    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表