How to make a Win 32 App run as a Windows Service? - winapi

Our company just completed a Win 32 Application project, and we have a executable program and it works fine in Windows. Now we have to run it as a windows service under Network account. I am pretty new to Windows Service, so please advice, what should I do to transform this program to a Windows Service? Thank you!

If by "Win32 Application project" you mean "GUI program", then do not run that as a service. Otherwise, here's what you need to do:
Register a service control handler using RegisterServiceCtrlHandler as soon as your program starts. This is required so you can handle the start and stop events. If you do not do this the service manager will kill your process because it thinks it's not a real service.
If you have an installer, you can use the sc command to create the service. For example: sc create MyService binPath= "C:\PathToExe\MyService.exe" type= own start= auto.
Alternatively you can call CreateService if you have code to do this.

If you mean that you want to run your application as a windows service, there is a utility in Windows Resource Kits. It's name is srvany.exe and you can find documentation about it here:
http://search.microsoft.com/search/results.aspx?st=b&View=en-us&s=1&c=0&qu=137890
Srvany.exe can be downloaded as part of Windows Server 2003 Resource Kit Tools here:
http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en

Srvany.exe is a fine, free solution (as described by alemjerus), but please check out AlwaysUp if you are working in a professional environment and need a robust/advanced feature set.
Good luck!

Related

How to force reboot Windows 7 from inside a C# Windows Service

I need to (force) reboot Windows from inside a Windows Service written in C#.
The ordinary way of doing it, from a desktop application, shutdown.exe, will probably not work at all? I assume I cannot run an EXE file from inside a service..
Look at the Win32 API InitiateSystemShutdown() and/or InitiateSystemShutdownEx() function.
Also refer to this MSDN article: Shutting Down.
I have tested it, but it does not work inside a Windows Service, it seems..

Install windows service without using InstallUtil or Setup Installation

I would like to send a windows service program to our client that does not have InstallUtil (no rights to distribute) and this one will be multiple installations in the same machine.
I found something at this point: Inno Setup for Windows service?
But I am not clear how to:
add the InnoSetup script and where to add this script?
For the if and else: System.Environment.UserInteractive? if a service is not installed, then it will be going inside this if?
Thanks in advance.
I have provided a step-by-step solution for how to add command-line install/uninstall to your Windows service using C#. This solution lets you avoid requiring the use of InstallUtil.
How to make a .NET Windows Service start right after the installation?
You need to add an installer to your service setup project, and a custom action. This article will get you started.

UAC giving problems with my application

i was making an installer for my app its working fine on xp but on vista the UAC is giving problem unless i do a run as administrator the Unexpected error appears when i run my app afters installation, any idea?
i am installing the application in C:\xfolder\x
There is nothing you can do. You MUST run the installer as administrator.
As for the application, you will also need to run it as administrator with elevated priviledges but thre are options to make it ask automatically for elevation via application manifest. You can do a search on stackoverflow.com because there are more post related to this issue.
Here is a link to a post that might help.
Here are a couple more usefull link on app manifest and UAC:
App Manifest (1)
App Manifest (2)
UAC technology.
They are in C# but then again translating to VB.net is like a walk in the park.
You can make a windows service. And put all operations that require elevated rights into that service. You install the service as SYSTEM account and you communicate with the client via .net remoting or any other way for vb6.
if your app exe is an active x exe then you will need to register all the ocx file using regsvr command and then register your exe with regsvr32 command, for an active class to be used in win vista it first must be registered. make a batch to do these registration.

Create Windows service from executable

Is there any quick way to, given an executable file, create a Windows service that, when started, launches it?
To create a Windows Service from an executable, you can use sc.exe:
sc.exe create <new_service_name> binPath= "<path_to_the_service_executable>"
You must have quotation marks around the actual exe path, and a space after the binPath=.
More information on the sc command can be found in Microsoft KB251192.
Note that it will not work for just any executable: the executable must be a Windows Service (i.e. implement ServiceMain). When registering a non-service executable as a service, you'll get the following error upon trying to start the service:
Error 1053: The service did not respond to the start or control request in a timely fashion.
There are tools that can create a Windows Service from arbitrary, non-service executables, see the other answers for examples of such tools.
Use NSSM( the non-Sucking Service Manager ) to run a .BAT or any .EXE file as a service.
http://nssm.cc/
Step 1: Download NSSM
Step 2: Install your sevice with nssm.exe install [serviceName]
Step 3: This will open a GUI which you will use to locate your executable
Extending (Kevin Tong) answer.
Step 1: Download & Unzip nssm-2.24.zip
Step 2: From command line type:
C:\> nssm.exe install [servicename]
it will open GUI as below (the example is UT2003 server), then simply browse it to: yourapplication.exe
More information on: https://nssm.cc/usage
these extras proved useful.. need to be executed as an Administrator
sc create <service_name> binpath= "<binary_path>"
sc stop <service_name>
sc queryex <service_name>
sc delete <service_name>
If your service name has any spaces, enclose in "quotes".
Many existing answers include human intervention at install time. This can be an error-prone process. If you have many executables wanted to be installed as services, the last thing you want to do is to do them manually at install time.
Towards the above described scenario, I created serman, a command line tool to install an executable as a service. All you need to write (and only write once) is a simple service configuration file along with your executable. Run
serman install <path_to_config_file>
will install the service. stdout and stderr are all logged. For more info, take a look at the project website.
A working configuration file is very simple, as demonstrated below. But it also has many useful features such as <env> and <persistent_env> below.
<service>
<id>hello</id>
<name>hello</name>
<description>This service runs the hello application</description>
<executable>node.exe</executable>
<!--
{{dir}} will be expanded to the containing directory of your
config file, which is normally where your executable locates
-->
<arguments>"{{dir}}\hello.js"</arguments>
<logmode>rotate</logmode>
<!-- OPTIONAL FEATURE:
NODE_ENV=production will be an environment variable
available to your application, but not visible outside
of your application
-->
<env name="NODE_ENV" value="production"/>
<!-- OPTIONAL FEATURE:
FOO_SERVICE_PORT=8989 will be persisted as an environment
variable to the system.
-->
<persistent_env name="FOO_SERVICE_PORT" value="8989" />
</service>
Same as Sergii Pozharov's answer, but with a PowerShell cmdlet:
New-Service -Name "MyService" -BinaryPathName "C:\Path\to\myservice.exe"
See New-Service for more customization.
This will only work for executables that already implement the Windows Services API.
I've tested a good product for that: AlwaysUp. Not free but they have a 30 days trial period so you can give it a try...
I created the cross-platform Service Manager software a few years back so that I could start PHP and other scripting languages as system services on Windows, Mac, and Linux OSes:
https://github.com/cubiclesoft/service-manager
Service Manager is a set of precompiled binaries that install and manage a system service on the target OS using nearly identical command-line options (source code also available). Each platform does have subtle differences but the core features are mostly normalized.
If the child process dies, Service Manager automatically restarts it.
Processes that are started with Service Manager should periodically watch for two notification files to handle restart and reload requests but they don't necessarily have to do that. Service Manager will force restart the child process if it doesn't respond in a timely fashion to controlled restart/reload requests.
You can check out my small free utility for service create\edit\delete operations. Here is create example:
Go to Service -> Modify -> Create
Executable file (google drive): [Download]
Source code: [Download]
Blog post: [BlogLink]
Service editor class: WinServiceUtils.cs
Probably all your answers are better, but - just to be complete on the choice of options - I wanted to remind about old, similar method used for years:
SrvAny (installed by InstSrv)
as described here:
https://learn.microsoft.com/en-us/troubleshoot/windows-client/deployment/create-user-defined-service
I have another method, using the open-source library called Topshelf.
I used it in a c# project, but maybe its available in different programming languages.
Here's a video that explains how to use it a little.
https://www.youtube.com/watch?v=y64L-3HKuP0
The crux of this issue for a lot of people is that you can't install any old .exe as a service unless you use the old method that Tomeg used. I couldn't find the windows nt toolkit that's needed to get that to work.
I was stuck in a corner and this was my way out.

Installing services remotely

Is there a way to install services remotely, without necessarily resorting to .msi packages or full-blown installers? I'm currently using a method similar to the one discussed here:
How to install a windows service programmatically in C#?
to install a service locally, and it works fine. However, I also need to be able to do the same thing remotely. I appreciate any insight.
If you can copy the service binary (the exe file) to the destination computer you can install the service exactly in the same way as you do this locally. The only distinguish is that during the usage of OpenSCManager function (see http://msdn.microsoft.com/en-us/library/ms684323.aspx) you should use destination computer as the first parameter (lpMachineName) and in CreateService (see http://msdn.microsoft.com/en-us/library/ms682450.aspx) as a lpBinaryPathName you should place the path to your service exe how it looks like on the remote computer.
You can use sc.exe utility to do the installation (type" sc create /?" in the command prompt to receive help). The remote installation of the service which you can do with sc.exe you can implement with native Windows API like I short explain above.

Resources