Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Windows NT Service Shutdown Problem

Posted on 2004-04-28
5
206 Views
Last Modified: 2013-11-13
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
Comment
Question by:helmespc
  • 2
5 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 10944722
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
 

Author Comment

by:helmespc
ID: 10945947
Yeah I tried that with an "OnShutdown" subroutine.... no dice... thanks though!
0
 
LVL 22

Accepted Solution

by:
cookre earned 200 total points
ID: 10951049
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
This is an explanation of a simple data model to help parse a JSON feed

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question