Solved

Windows NT Service Shutdown Problem

Posted on 2004-04-28
5
205 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pairs challenge 5 77
When i run adoquery my application freezes 26 166
Programatically extract date from website 8 77
What does != "" mean in programming 8 34
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

778 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