Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Windows NT Service Shutdown Problem

Hi,
   I am coding a Windows NT Service that gathers realtime data from a radio receiver.  On shutdown it must send the command via TCP/IP to the receiver telling it to quit sending realtime data back.  This works fine when shutting down from the admin->Services stop control, but when shutting down it refuses to send the command to the receiver.  I have verified that my code is set up to watch for the SERVICE_CONTROL_SHUTDOWN and it does the same thing it does when it receives SERVICE_CONTROL_STOP, so it should basically work the identical way.  I have tried working with different group names and dependencies to make sure TCP/IP is not shutting down before the service is told to shut down, but to no avail.  Does anyone know why this might not be shutting down properly when windows shuts down?  

Thanks,
Patrick
0
helmespc
Asked:
helmespc
  • 2
1 Solution
 
cookreCommented:
I've had a similar experience with the OnStop code in a c# service (if you don't do c#, that's what gets triggered when a SERVICE_CONTROL_STOP comes in).

I had code to close some files that worked fine EXCEPT when the SERVICE_CONTROL_STOP occurred by virtue of a reboot.

On a lark, I put the code in it's own subroutine, called the subroutine from the OnStop handler, and everything worked just fine.

It's worth a 5 minute try.
0
 
helmespcAuthor Commented:
Yeah I tried that with an "OnShutdown" subroutine.... no dice... thanks though!
0
 
cookreCommented:
Presuming Windows isn't shutting down communications before shutting down everything else, you can either hook WM_ENDSESSION (a royal pain for a service since global hooks have to be in an external DLL), or, create a window you don't show, then watch for a WM_ENDSESSION:

In the OnStart, do normal window setup things:

1) Make sure the service is created to include SERVICE_WIN32_OWN_PROCESS and SERVICE_INTERACTIVE_PROCESS.

2) get hInst:
IntPtr hInst=Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModule("my.exe"));

3) register the class:
WNDCLASS wndclass=new WNDCLASS();
int mm;
wndclass.lpfnwndproc=new WndProc(MyWndProc);
wndclass.hInstance=hInst;
wndclass.hIcon=IntPtr.Zero;
wndclass.hCursor=IntPtr.Zero;
wndclass.hbrBackground=IntPtr.Zero;;
wndclass.lpszMenuName="whatever";
wndclass.lpszClassName="whatever";
... other wndclass= things
mm=RegisterClass(ref wndclass);

4) Create window
MyHwnd=CreateWindowEx(0 ,...,IntPtr.Zero ,hInst ,IntPtr.Zero);

5) Get messages in
int MyWndProc(int hwnd,int message,int wParam,int lParam)
{
...when you get a WM_ENDSESSION, you know th box is going down.
return 1;
}



Here are the DLLImports:
[DllImport("User32.Dll")]
public static extern int RegisterClass(ref WNDCLASS wndcls);

[DllImport("user32.dll")]
public static extern IntPtr CreateWindowEx(int extra
                                         ,string lpClassName
                                         ,string lpWindowName
                                         ,int dwStyle
                                         ,int x
                                         ,int y
                                         ,int nWidth
                                         ,int nHeight
                                         ,int hwndParent
                                         ,HANDLE hMenu
                                         ,HANDLE hInstance
                                         ,IntPtr lpParam);

No delegates needed.



0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now