No, the Work-Around is not documented - at least as far as I know. And
actually it shouldn't work (although Keith Brown explained how evil
FullTrust code can be
http://msdn.microsoft.com/msdnmag/issues/04/04/SecurityBriefs/default.aspx
which is exactly what I use here) but it does! Before I show the code some
comments. First I hope that some Microsoft guy reads this and comments how
we should write future services which are aware of power broadcast events.
And second to explain what happens:
1. The Service classes constructor (!) finds the CommandCallbackEx
registered by the base class and replaces it by its own stub.
2. The stub forwards all requests other than the power state information
(13) to the base classes method (actually the OnShutdown no longer works
with .NET 2.0, too but I didn't need it)
3. The stub handles OnPowerEvent the way .NET 1.1 does it.
I use this code on different systems for around four months and it seems to
work fine.
Regards
Jochen
PS: The work-around is used in here (german web site only):
http://www.psimarron.net/vcrnet - the installer includes full sources but I
think the extract shown below will fit your needs.
---------------------
Work-Around Code:
---------------------
public class Service : System.ServiceProcess.ServiceBase
{
public Service()
{
// Find the initialisation routine - may break in later versions
MethodInfo init = typeof(ServiceBase).GetMethod("Initialize",
BindingFlags.NonPublic | BindingFlags.Instance);
// Call it to set up all members
init.Invoke(this, new object[] { false });
// Find the service callback handler
FieldInfo handlerEx = typeof(ServiceBase).GetField("commandCallbackEx",
BindingFlags.NonPublic | BindingFlags.Instance);
// Read the base class provided handler
m_Forward = (Delegate)handlerEx.GetValue(this);
// Create a new delegate to our handler
Delegate test = Delegate.CreateDelegate(m_Forward.GetType(), this,
"ServiceCallbackEx");
// Install our handler
handlerEx.SetValue(this, test);
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
}
private int ServiceCallbackEx(int command, int eventType, IntPtr eventData,
IntPtr eventContext)
{
// Call the base class implementation which is fine for all but power and
session management
if (13 != command) return (int)m_Forward.DynamicInvoke(command, eventType,
eventData, eventContext);
// Process and forward success code
if (OnPowerEvent((PowerBroadcastStatus)eventType)) return 0;
// Abort power operation
return 0x424d5144;
}