[Reprint] Use VS (c #) to create, debug windows Services, and deploy and uninstall windows Services,
Use VS (c #) to create, debug, and deploy and uninstall windows Services
My colleague asked about the windows service stuff. Now I want to sort it out, use c # To create a windows service, and debug, deploy, and uninstall it.
1. Create a windows service
1. Open VS2008, create a Project, select Visual C #-Windows for the Project type, select Windows Service for the Templates, and click OK for others.
2. in Solution Explorer, three files are automatically generated: app. config, Program. cs, Service1.cs, where app. config allows you to add custom configuration information for use in code files. Program. cs provides the Main () method, which generally does not need to be modified as the program population. Service1.cs is the service to be implemented.
3. double-click Service1.cs to open Service1.cs [Design] by default. You can drag a component on this interface. In addition, some attribute settings about this service are displayed in Properties, for example, AutoLog (True: use windows event log to record the service Log; False: You can customize your own event log), CanPauseAndContinue (True: you can pause or continue the Service. False: you cannot pause or continue the Service. ServiceName is the name of the Service recognized by Service Control Manager ).
4. Go to the Code view and we can see that a constructor is generated by default, and two methods OnStart and OnStop to be rewritten are generated. We need to override these two methods. The OnStart method is simply rewritten below.
Protected override void OnStart (string [] args) {string path = @ "d: \ share \ test.txt"; StreamWriter sw; sw = new StreamWriter (path ); foreach (string arg in args) sw. writeLine (arg); sw. close (); sw. dispose ();} is the most basic windows service, but debugging is required if it runs normally.
Ii. debug windows service
Generally, the debugging method we use is to deploy the service, attach to process..., find the corresponding service process, and debug it. However, the operation is cumbersome. After the service is modified, You Need To uninstall the service and redeploy the service, and the OnStart method is difficult to debug. Find a good debugging method on the network and try it. It is very useful. We recommend it to you :)
1. Add the public method to Service1.cs and encapsulate the protected method to be debugged for calling Main (). Take OnStart () as an example:
Public void start (string [] args) {this. OnStart (args );}
2. Add the following code in Main () and execute it during Human-Computer Interaction:
If (Environment. userInteractive) {Service1 s = new Service1 (); string [] args = {"a", "B"}; s. start (args); Console. writeLine ("the service is started"); Console. readLine (); s. stop (); Console. writeLine ("the service is stopped");} else {ServiceBase [] ServicesToRun; ServicesToRun = new ServiceBase [] {new Service1 ()}; ServiceBase. run (ServicesToRun );}
3. Change the output type of the Project.
Right-click the Project and click Properties. In the Application tag, set Output Type to Console Application.
Okay, so that you can press F5 for debugging :)
Iii. Deployment
1. in the design view of Service1.cs, right-click and click Add Installer. A project install is automatically created. cs file. In the design view, there are two components serviceProcessInstaller1: this component has an attribute named Accout, which is a User by default. In this way, you need to enter the User name and password during deployment, here we can select the Localservice with the minimum permission. Of course, to obtain more permissions, we can set the other two network services and local
ServiceInstaller1: information related to the service, such as the service name, service description, display name, and Startup type.
2. If you use the default event log, you can compile and deploy it now. If you use a Custom event log, You need to modify some code in ProjectInstaller.
3. Use installutil.exe to deploy windows service
(Pay attention to permissions. here you need to deploy them with the Administrator user. Otherwise, some error messages will be prompted, such as An exception occurred during the Install phase. system. security. securityException: The source was not found, but some or all eve nt logs cocould not be searched. inaccessible logs: Security. or access is denied)
Runas/user: Administrator cmd
Cd C: \ Windows \ Microsoft. NET \ Framework \ v2.0.50727
Installutil D: \ project \ WindowsService1 \ WindowsService1 \ bin \ Debug \ windowsservice1.exe
Now the deployment is complete.
4. Uninstall the service
Installutil/u D: \ project \ WindowsService1 \ WindowsService1 \ bin \ Debug \ windowsservice1.exe
5. Start the service
1. when starting a service, an error message is displayed, indicating that the service may not be used. You can view eventvwr, which provides sufficient error information, you can adjust the service Code according to the error message. It may also be because the service requires file operations and has no permissions. In this case, you can open the file permissions to be operated.
2. When starting the service, the startup failed due to permission issues, we can set the startup account.
3. Parameters of windows service are transmitted once at startup. parameters can be passed in the form of an interface, or in the form of a command line: SC start service1 arg0 arg1...
Reference link:
Http://nuigroup.com/forums/viewthread/9829/
Http://stackoverflow.com/questions/593454/easiest-language-to-create-a-windows-service/593803#593803
Http://topic.csdn.net/u/20090313/13/7c32b0e3-6e13-4226-8da4-392e31514454.html
Http://www.cnblogs.com/guoyinghai/archive/2008/04/16/1156943.html
How to use C language in vs2010
Create a win32 console application project, create a new file, select the c/c ++ header file, and name it ". c" in the file name format.
In addition, if you want to use c for compiling in the cpp file, you can use the following:
# Ifdef _ cplusplus
Extern "C "{
# Endif
// Add your code here, these code compile by c
# Ifdef _ cplusplus
}
# Endif
Which environment should be configured for c program development with vs? Only c ++ and c
C ++ in VS is fully compatible with C. You can directly create a c ++ file, write the pure c code, and compile it directly.
For example, to create a new test. cpp file and write the following content, the file can be compiled and run perfectly:
# Include "stdio. h"
Void main ()
{
Printf ("C ++ file input c code! \ N ");
}