Groups | Blog | Home
all groups > dotnet interop > january 2006 >

dotnet interop : my service stops when a user logs off


Rob Latour
1/30/2006 4:08:23 PM
I am trying to run my app as a service using srvany.

It loads and runs fine when windows starts. All is well when the user logs
on. However, when the user logs off the app unloads from memory and is no
longer running (but the service appears to be running).

From what I've been able to determine to date, I need to handle the
CTRL_LOGOFF_EVENT so that my program doesn't unload when the user logs off.
I just can't seem to get it working:

Here are some code snippets I am working with modeled after a good article
found at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemgcclasskeepalivetopic.asp

Any help would be appreciated!

With thanks, Rob

Public Class MyWin32

' Declare the SetConsoleCtrlHandler function as external

' and receiving a delegate.

<DllImport("Kernel32")> _

Public Shared Function SetConsoleCtrlHandler(ByVal Handler As
HandlerRoutine, _

ByVal Add As Boolean) As Boolean

End Function



' A delegate type to be used as the handler routine

' for SetConsoleCtrlHandler.

Delegate Function HandlerRoutine(ByVal CtrlType As CtrlTypes) As [Boolean]

' An enumerated type for the control messages

' sent to the handler routine.

Public Enum CtrlTypes

CTRL_C_EVENT = 0

CTRL_BREAK_EVENT = 1

CTRL_CLOSE_EVENT = 2

CTRL_LOGOFF_EVENT = 5

CTRL_SHUTDOWN_EVENT = 6

End Enum

End Class






****************************
the handler routine

Shared Function ConsoleCtrlCheck(ByVal dwctrltype As MyWin32.CtrlTypes) As
Boolean

Select Case dwctrltype

Case MyWin32.CtrlTypes.CTRL_C_EVENT

Debughandler("CTRL+C received!")

Return True

Case MyWin32.CtrlTypes.CTRL_BREAK_EVENT

Debughandler("CTRL+BREAK received!")

Return True

Case MyWin32.CtrlTypes.CTRL_CLOSE_EVENT

Debughandler("CLOSE received!")

Return True

Case MyWin32.CtrlTypes.CTRL_LOGOFF_EVENT

Debughandler("LOGOFF received!")

Beep()

Beep()

Beep()

Return True

Case MyWin32.CtrlTypes.CTRL_SHUTDOWN_EVENT

Debughandler("SHUTDOWN received!")

Return True

End Select

End Function





***********************

in my main program (vb.net vs 2005) ....



Dim hr As New MyWin32.HandlerRoutine(AddressOf ConsoleCtrlCheck)

MyWin32.SetConsoleCtrlHandler(hr, True)

System.Windows.Forms.Application.Run()

Mattias Sjögren
1/31/2006 12:33:44 AM
Rob,

[quoted text, click to view]

I'm not familiar with Srvany, but it seems like a bit of a hack. Why
not make a proper service?


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Skjoldmø
1/31/2006 10:35:45 AM
If the computer is member of a domain, or configured for CTRL+ALT+DELETE log
on all progams but Windows will be closed at Log off.

Skjoldmø


[quoted text, click to view]

Rob Latour
1/31/2006 9:01:28 PM
two reasons:
1. the program was not written as an service - so if I can get this to
work it would be easier
2. I am using vs 2005 standard which doesn't support writing service (so
it would also be less expensive then upgrading)


[quoted text, click to view]

Rob Latour
1/31/2006 9:04:51 PM
I have other service that run, for example real vnc, that don't shut down
when a user logs off?


[quoted text, click to view]

TDC
2/1/2006 6:54:12 AM
While I agree he should write this as a Service, I'd like to comment on
this:

"More, you are using Windows forms in your application, and you expect
it to keep runing when the user logs off? Well, that's not possible"

He used this code:

System.Windows.Forms.Application.Run()

This just sets up a message pump on the current thread. This can be
done just fine within a service. I didn't see any place else in his
code where he was creating any forms, etc.

Tom
Willy Denoyette [MVP]
2/1/2006 1:54:46 PM
If it's not written as a service it won't behave as a service.
More, you are using Windows forms in your application, and you expect it to
keep runing when the user logs off? Well, that's not possible, windows
services can/should not have a UI ( unless you set the service to interact
with the desktop, something you should do only for debugging), windows
services do not survive a logoff when they are attached to the interactive
desktop.

Willy.



[quoted text, click to view]
| two reasons:
| 1. the program was not written as an service - so if I can get this to
| work it would be easier
| 2. I am using vs 2005 standard which doesn't support writing service (so
| it would also be less expensive then upgrading)
|
|
[quoted text, click to view]
| > Rob,
| >
| >>I am trying to run my app as a service using srvany.
| >
| > I'm not familiar with Srvany, but it seems like a bit of a hack. Why
| > not make a proper service?
| >
| >
| > Mattias
| >
| > --
| > Mattias Sjögren [C# MVP] mattias @ mvps.org
| > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
| > Please reply only to the newsgroup.
|
|

Rob Latour
2/1/2006 5:15:19 PM
There is a windows ui portion to my app, I had checked Interact with the
desktop as a services option and it works well - except for the log off
thing. From your responses however it would appear there is no way to
survive a windows log off using vb.net however I am left wondering how other
apps that interact with my desktop - such as my UPS monitoring application
and Real VNC seem to be able to survive the logoff just fine.

Thanks, Rob


[quoted text, click to view]

Willy Denoyette [MVP]
2/1/2006 10:10:51 PM
As far as I can see this is not a complete program, so I can't tell if no UI
elements are created and also I don't see a reason why he needs a message
pump.
Note that the OP also registers a console control handler, which is also not
usable from a service. So I have the impression he's trying to run a
windows/console application as a service and thinks he can solve the logoff
issue with a console handler.

Willy.


[quoted text, click to view]
| While I agree he should write this as a Service, I'd like to comment on
| this:
|
| "More, you are using Windows forms in your application, and you expect
| it to keep runing when the user logs off? Well, that's not possible"
|
| He used this code:
|
| System.Windows.Forms.Application.Run()
|
| This just sets up a message pump on the current thread. This can be
| done just fine within a service. I didn't see any place else in his
| code where he was creating any forms, etc.
|
| Tom
|

TDC
2/2/2006 7:52:30 AM
Agreed. The graphical portion of the application must be seperated
from that which you intend to be used as a service.

I have written services in VB.NET that work just fine, including one
that basically wraps what used to be an ActiveX Exe (and that works
fine too).

There are any number of IPC methods you can use to communicate from
your UI to your service, but obviously if your application access the
UI alot then this will require a major restructuring to seperate your
business logic from your user presentation.

Tom
Willy Denoyette [MVP]
2/2/2006 1:25:27 PM
Because they (the UI part) are separate applications that use some form of
IPC mechanism to 'talk' to the service. Such applications are configured as
'autoruns', that is they start when a user logons on and they stop when a
user logs off.
Services that directly interact with the desktop (as SYSTEM) handle the
logon/logoff events and do migrate the service desktop to the active desktop
using Win32 API's, such services are really insecure and they will fail on
Vista.

Willy.

[quoted text, click to view]
| There is a windows ui portion to my app, I had checked Interact with the
| desktop as a services option and it works well - except for the log off
| thing. From your responses however it would appear there is no way to
| survive a windows log off using vb.net however I am left wondering how
other
| apps that interact with my desktop - such as my UPS monitoring application
| and Real VNC seem to be able to survive the logoff just fine.
|
| Thanks, Rob
|
|
[quoted text, click to view]
| > As far as I can see this is not a complete program, so I can't tell if
no
| > UI
| > elements are created and also I don't see a reason why he needs a
message
| > pump.
| > Note that the OP also registers a console control handler, which is also
| > not
| > usable from a service. So I have the impression he's trying to run a
| > windows/console application as a service and thinks he can solve the
| > logoff
| > issue with a console handler.
| >
| > Willy.
| >
| >
[quoted text, click to view]
| > | While I agree he should write this as a Service, I'd like to comment
on
| > | this:
| > |
| > | "More, you are using Windows forms in your application, and you expect
| > | it to keep runing when the user logs off? Well, that's not possible"
| > |
| > | He used this code:
| > |
| > | System.Windows.Forms.Application.Run()
| > |
| > | This just sets up a message pump on the current thread. This can be
| > | done just fine within a service. I didn't see any place else in his
| > | code where he was creating any forms, etc.
| > |
| > | Tom
| > |
| >
| >
|
|

Willy Denoyette [MVP]
2/2/2006 6:20:12 PM

[quoted text, click to view]
| Agreed. The graphical portion of the application must be seperated
| from that which you intend to be used as a service.
|
| I have written services in VB.NET that work just fine, including one
| that basically wraps what used to be an ActiveX Exe (and that works
| fine too).
|
| There are any number of IPC methods you can use to communicate from
| your UI to your service, but obviously if your application access the
| UI alot then this will require a major restructuring to seperate your
| business logic from your user presentation.
|

True, but Services that interact 'heavily' with a UI front-end shouldn't be
written as services in the first place, as they are mainly 'driven' by the
presence of an interactive user, they aren't of great use at all when no
such user is present.
The most obvious problem with services today is that they aren't used for
what they were originally designed for (NT Services, designed back in 1990),
that is run as a 'daemon' in a security constrained context, that is,
running in a 'sandboxed and secured desktop' as a 'non-interactive' user
with elevated 'local security' privileges. If you understand all these
constraints, it should be clear that instantiating full ActiveX COM
components (OCX96) is a big NO and a "not supported" scenario.
Another problem, introduced with .NET, is that everyone seems to need a
bunch of Services running on their boxes, but most don't know that the
number of services on a (Win32) system is restricted by the size of the
non-pooled memory available, add to that, the size of the non-pooled memory
and the handles taken by the GDI when interact with the desktop is enabled
and you know why some systems behave so badly.

Willy.



Rob Latour
2/6/2006 1:58:21 PM
Thanks for all of this.

I guess its back to the drawing board for me :-)

Cheers, Rob

[quoted text, click to view]

AddThis Social Bookmark Button