Solved

Windows NT Service Shutdown Problem

Posted on 2004-04-28
5
209 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
[X]
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
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

636 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