Solved

Windows NT Service Shutdown Problem

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Report events not being triggered 8 100
find a node in VST 2 47
object oriented javascript web form 8 73
recursion example 16 69
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 …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now