Jump to content

Running LabVIEW executable as a Windows Service


viSci

Recommended Posts

Greetings all - I have a LabVIEW 2014 executable that I am trying to put into a form that can be run as or from a Windows Service.  Of course I removed all possible dialog windows and any need for UI interaction.  I did my testing using a program called FireDaemon which can wrap any window exe as a Windows Service.  I found that I could not get it working until I included the NI Authentication Service (NIAuth) as a dependency within FireDaemon.  If I do not do this my application (which does not use any web services as far as I know) just hangs when the Windows Service activates.  Does anyone have a clue about this?  I would like to create a version of my app that does not require this dependency.

Link to comment
  • 1 month later...

I've using for years the "anyservice.dll" way described in http://forums.ni.com/t5/LabVIEW/running-labview-program-as-system-service/td-p/878148 and it works great. Unfortunately, original Chen's post seems to be gone, but basically you call asynchronously the DispatchService call in that DLL, and in turn when OS needs them it calls methods BeginService and EndService in a server.dll you provide . You can put the code you need in those two methods, I usually just take the EndService to close my application. Then register your exe as a service (thtrough "sc create" command, for example) and it's done.

 

Regards,

Aitor

Link to comment

Thanks for the replies.  I went with a tool called FireDaemon, it is low cost $50 and works very well!

Of course I also refashioned my app to remove any nasty popup's or dialogs that could occur.  I also added a command line interface and a comprehensive log file which was essential to debug various issues with my new LV service.

Link to comment

​I think that has been superseded by sc.exe (comand line) and Srvinstw.exe (GUI version) which means you don't have to hack the registry any more.

 

srvany is not the same as sc.exe or srvinstw.exe. The first is a sort wrapper that allows to wrap a standard executable in a way that it will run as a service and at least properly respond to service control requests (although things like stopping the service really will more or less simply kill the process so it isn't a very neat solution, but it works for many simple things.

 

sc.exe is THE standard Windows command line tool to the service control manager almost for as long as Windows services exist. The interface to the service control manager is really located in advapi32.dll and can be also directly called from applications (but many functions will require elevated rights to work successfully). Not sure about Srvinstw.exe but the standard way of interacting with the service control manager through a GUI is through the MMC snapin for services which you can reach through the Control Panels->Administrative Tools.

 

While srvany works reasonably well for simple tasks, for more complicated services it is really better to go through the trouble of integrating a real interface to the service control manager in an application meant to be executed as a service.

  • Like 1
Link to comment
  • 1 year later...

I'm attempting this today. I have a LabVIEW 2014 executable to be launched as a service on Windows Server 2012 R2.

I followed Microsoft's instructions to run "instsrv.exe <service_name> srvany.exe" (exes are from the resouce toolkit), and then configured srvany to launch my LabVIEW exe as a service. I can see it listed in the Windows Services manager. Success.

However, this appears to be a dirty way to convert any application into a service. What it really does is turn "srvany.exe" into a service, and then you configure srvany to launch your application. This means there's no real communication between my application and the Windows Service manager layer. Consequently, if my application were to be asked to shutdown gracefully, srvany receives the request and simply pulls my application out of memory.

I'd much prefer for my application to be aware of shutdown requests, and then it can close down its resources safely/appropriately. It maintains database connections and TCP pipes, so I don't want these left orphaned.

Does anyone know any more about creating 'actual' Windows services? I'd like to avoid this wrapper based approach which casually spins up and tears down exes.

Link to comment

We used to have a Toolkit that would allow a LabVIEW application to properly interact with the Service manager so it could itself behave like a real service. It consisted of a DLL part that does the real work of interfacing to the Service Control interface in ADVAPI32.DLL. This also involves callbacks so a DLL is really unavoidable here. I'll have to check about the status of this toolkit and if we can make it available to external parties. From what I remember it was a substantial development effort.

Edited by rolfk
  • Like 1
Link to comment
On 8/24/2017 at 7:26 AM, Thoric said:

Does anyone know any more about creating 'actual' Windows services? I'd like to avoid this wrapper based approach which casually spins up and tears down exes.

Try out NSSM:

http://www.nssm.cc/download

It's still a wrapper, but an awesome one. In the pre-release builds, you can create custom actions that run when the service is stopped (i.e. to send your application a command to stop and clean up). It's super easy to use compared to everything else I've tried.

Edited by DTaylor
  • Like 1
Link to comment
  • 7 months later...
On 8/24/2017 at 8:26 AM, Thoric said:

...this appears to be a dirty way to convert any application into a service. What it really does is turn "srvany.exe" into a service, and then you configure srvany to launch your application. This means there's no real communication between my application and the Windows Service manager layer. Consequently, if my application were to be asked to shutdown gracefully, srvany receives the request and simply pulls my application out of memory.

I'd much prefer for my application to be aware of shutdown requests, and then it can close down its resources safely/appropriately. It maintains database connections and TCP pipes, so I don't want these left orphaned.

This is exactly the situation we have found ourselves in.  We need our service to exit gracefully.

On 8/24/2017 at 8:33 AM, rolfk said:

We used to have a Toolkit that would allow a LabVIEW application to properly interact with the Service manager so it could itself behave like a real service. It consisted of a DLL part that does the real work of interfacing to the Service Control interface in ADVAPI32.DLL. This also involves callbacks so a DLL is really unavoidable here. I'll have to check about the status of this toolkit and if we can make it available to external parties. From what I remember it was a substantial development effort.

Any chance you had an opportunity to see if this was something that you were able to share?

Link to comment
2 hours ago, WireWorker said:

Any chance you had an opportunity to see if this was something that you were able to share?

Not for free, sorry. It's not my decision but the powers in charge are not interested to support such a thing for external parties nor give it away for free. And I don't have any copyright on this code.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.