LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Running LabVIEW app as Windows Service - Win7

Yes, I'm fully aware of the various white papers and forum questions on this topic.  However, many of the items that I've read are pretty dated.  So, here we are in 2016 (I'm using LV2011) and I'm just curious about a couple items.

 

From what I've read, the ideal solution to this is to make a batch file that runs this important command: "INSTSRV *theService* C:\Go\To\File\srvany.exe" and then set up the registry in that batch file.  This all needs to be taken care of in a single installation and I haven't really read anything on that.  LabVIEW installation has a registry section.  Why can I not set up the registry portion there?  In terms of order of operations, does srvany.exe need to take place before the registry set up which is why you maybe shouldn't do that in a LabVIEW installation?  Or does that not matter?  How can I build a LabVIEW installation that calls a batch file?

0 Kudos
Message 1 of 16
(4,047 Views)

There is  installer Properties -> Advanced -> Run executable (or bat file) at end of installation

 

The order of operations matters if

1) operation 2 requires result of operation 1. 

2) operation 2 overwrites result of operation 1 with correct data.

 

In your case:

Does "INSTSRV *theService* C:\Go\To\File\srvany.exe" write anything wrong to registry that is overwritten by batch file? If not, then what prevents you from writing into registry using LV installer?

0 Kudos
Message 2 of 16
(4,038 Views)

Does "INSTSRV *theService* C:\Go\To\File\srvany.exe" write anything wrong to registry that is overwritten by batch file? If not, then what prevents you from writing into registry using LV installer?


That's basically what I'm asking.  I haven't much knowledge of what it is that srvany.exe is or what it does exactly.  But every article I've read has mentioned srvany.exe first, then registry.  I have yet to see reverse order.

0 Kudos
Message 3 of 16
(4,021 Views)

Here's an article on how to create a batch file.  Alexander has provided the correct method of running a batch file after installation above. 

 

Looking at this knowledge base article on NI's website, it appears that calling the instsrv utility with the srvany.exe windows command creates a new registry value in your system services registry subdirectory. Inside this new registry value, you can specify which application you want to run as a service. Thus, you have to run srvany.exe to create the registry value before you edit it. 

 

As far as I know, using srvany.exe is a kind of a hackish approach to creating a windows service, although it works reasonably well for simpler applications. Here's a list of downsides associated with srvany.exe, although note that the source is a company trying to sell you an alternative to using srvany, so it may be a bit biased! The proper and more involved way is to utilize the sc.exe command, although it may not be necessary if you are deploying a relatively simple service. 

 

Message 4 of 16
(3,943 Views)

I have had success using srvstart.exe - I used the windows command (sc.exe) to install/register the service (which basically calls srvstart and it configures the service with a config file) using a batch file called after my installation completes and then srvstart takes over and manages starting/stopping/restarting the application executable. One of the neat things about it is that it has different options to shutdown the application (e.g. sending the WM_CLOSE message) so you can shutdown gracefully rather than just killing the process.


LabVIEW Champion, CLA, CLED, CTD
(blog)
Message 5 of 16
(3,892 Views)

So looking further through LabVIEW's installer options. there's an Advanced section and in this section is a check box, "Run executable at end of installation."  Underneath this is Command Line Arguments that only becomes available after check the "Run executable at end of installation."   This section I don't know much about.  Is it possible to use that command line to do the srvany.exe?  Or, could I make a VI that basically is completely scripted and make an .exe out of that and have the installer run that file which will then run a premade batch file?  I want to make LabVIEW my installation service and not use something like BitRock.  Am I making sense?

0 Kudos
Message 6 of 16
(3,868 Views)

Yes, you should be able to do it in the way that you described, creating a batch file that is called by a VI that is made into an EXE that you call from the installer.

0 Kudos
Message 7 of 16
(3,858 Views)

My installer build specification calls a batch file post-install to register the service & start it.

 

Here's what I have in my build specification:

undefined

 

Notice how I use [INSTALLDIR] to pass the installation directory of the software into the batch file - there are a whole load of other parameters I think you can pass in too.

 

And here's roughly what's in my batch file:

 

@echo off
set _var=%~1
echo [Rig-Service]>"%_var%service.cfg"
echo startup="%_var%Rig-Service.exe">>"%_var%service.cfg"
echo shutdown_method=command>>"%_var%service.cfg"
echo shutdown="%_var%ServiceShutdown.exe">>"%_var%service.cfg"
echo auto_restart=y>>"%_var%service.cfg"
echo restart_interval=10>>"%_var%service.cfg"
echo startup_delay=10>>"%_var%service.cfg"
echo wait_time=15>>"%_var%service.cfg"
net stop Rig-Service >> service_log.txt
sc create Rig-Service binPath= "%_var%srvstart.exe svc Rig-Service -c \"%_var%service.cfg\"" start= auto DisplayName= "Rig Service" >> service_log.txt
sc config Rig-Service binPath= "%_var%srvstart.exe svc Rig-Service -c \"%_var%service.cfg\"" start= auto DisplayName= "Rig Service" >> service_log.txt
net start Rig-Service >> service_log.txt

This is using srvstart:

- The first line reads in the command line parameter

- The echo lines create the configuration file for srvstart

- I then stop the service (in case it was already running (e.g. upgrades)), create/configure the service and then finally start it.


LabVIEW Champion, CLA, CLED, CTD
(blog)
Message 8 of 16
(3,827 Views)

So, let's say I run a .bat file that sets up the service.  What needs to come first and what will come first?  I can set up the registry in the LabVIEW installation, but the app is not a service yet.  Does that matter?  Will the .bat file be run first in the installation process setting up the service and then LabVIEW install will set up the registry.  Or will this .bat file be run after the registry.  And again, does that even matter?  Does the service have to "exist" via srvany.exe before I can mess with registry?  If so, then I can probably just put in the .bat file to set up the registry there.  But I'm trying to do as much in the LabVIEW install as possible.

0 Kudos
Message 9 of 16
(3,799 Views)

Running your batch file after the installation happens after the NI installer has completed. So anything that happens in your installer will have happened by the time your batch file runs.

 

 


LabVIEW Champion, CLA, CLED, CTD
(blog)
0 Kudos
Message 10 of 16
(3,789 Views)