LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

stopping windows service

I have a LabVIEW executable running as a windows service per the instructions in NI Zone with the Microsoft srvany.exe.

The problems is that we need to execute some shutdown code in the LabVIEW executable when you exit out of the LabVIEW app. This is not being executed when the executable is running as a service. As far as I can tell, the LabVIEW executable is just killed off. Does anyone have have any experience linking the OnStop() call that windows when stopping a service to a VI call?


0 Kudos
Message 1 of 3
(3,320 Views)
The trick here is to get a callback function to communicate with LabVIEW. This is possible, but not necessarily easy. You will need to write a DLL that has the callback function in it. The callback function should fire an occurrence (or notifier, or queue). The VI, meanwhile, must create the occurrence and call a function that configures the service to use the callback function in your DLL. Then, the VI must have a branch of the code that waits for the occurrence to fire, after which it can do whatever is necessary to shutdown. Here's a rough skeleton of the DLL code.

#inlude "extcode.h"

//forward declare
DWORD WINAPI MyHandler(
  DWORD dwControl,
  DWORD dwEventType,
  LPVOID lpEventData,
  LPVOID lpContext
);

//Called by the VI. Should pass in the handle from GenerateOccurrence
int ConfigureOnStop(Occurrence *o)
{
  //Setup service callback function
  RegisterServiceCtrlHandlerEx(name, MyHandler, o)
}

//The event handler
DWORD WINAPI MyHandler(
  DWORD dwControl,
  DWORD dwEventType,
  LPVOID lpEventData,
  LPVOID lpContext
)
{
  Occurrence *o=(Occurrence*)lpContext;

  if(dwControl==SERVICE_CONTROL_STOP)
  {
    //fire the occurrence
    Occur(*o);
  }
}

What I'm not too sure about is how all of this will interact with the srvany executable, which presumably installs it's own service handler function. My guess is that you will replace that function with your own. As long as srvany didn't have anything important left to do, that shouldn't be a problem. If it did... well, more work may need to be done.
0 Kudos
Message 2 of 3
(3,304 Views)
I finally was able to create a Windows service by linking in a Win32 DLL containing the service dispatcher and control handler code to LabVIEW code. http://members.cox.net/kuanchen/lvservice/index.html outlines one way that this could work.
0 Kudos
Message 3 of 3
(3,261 Views)