EricBrown

I've got a service that runs from system, and it needs to reboot the client, but

I can't get any gui to be displayed to the current user.

Has anyone got any suggestions on how I can get some kind of popup to give the

user warning the machine is about to reboot and wait for user input before the reboot takes place

Thanks,

Eric-



Re: Windows Forms General Trying to get GUI to appear for logged in user, from a service running as System.....

OmegaMan

One can send a message to a user to the "Net Send" on the command line. To find out about the parameters use

C:\>net send

The syntax of this command is:

NET SEND{name | * | /DOMAIN[:name] | /USERS} message

To send a message, use the local machine name

NET SEND {ComputerName} Shutdown Occuring






Re: Windows Forms General Trying to get GUI to appear for logged in user, from a service running as System.....

Dan Dittenhafer

You can use one of the many System.Windows.Forms.MessageBox.Show methods:

MessageBox.Show("My Message", "The dialog title caption", MessageBoxButtons.Ok, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);

By specifying the MessageBoxOptions.ServiceNotification, your messagebox will display on the desktop of the interactive user, or if noone is logged in, it will display on the logon screen.





Re: Windows Forms General Trying to get GUI to appear for logged in user, from a service running as System.....

EricBrown

 

 

using System.Windows.Forms;

...

MessageBox.Show( "System needs to reboot", "Reboot Warning", MessageBoxButtons.Ok, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification );

...

Compile,

Result

Error 2 The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference ) D:\Project Files\SECWizard\Source Code\SECWizardAgent\CommandManager.cs 10 14 SECWizardAgent

This means I can't use forms from a service, correct Or am I missing something obvious

 

Thanks,

 

Eric-

P.S.

 <searching..>

http://msdn2.microsoft.com/en-us/library/system.windows.forms.aspx

<excerpt from web page>

Caution:

Classes within the Windows Forms namespace are not supported for use within a Windows service. Trying to use these classes from within a service may produce unexpected problems, such as diminished service performance and run-time exceptions.

 





Re: Windows Forms General Trying to get GUI to appear for logged in user, from a service running as System.....

EricBrown

Okay I added a Reference to System.Windows.Forms(on the .net tab of add reference), and am calling it... lets hope it's stable.

Eric-





Re: Windows Forms General Trying to get GUI to appear for logged in user, from a service running as System.....

OmegaMan

What if the service is running but the user is logged out Doing a Net send should be non-impactive for either scenario.





Re: Windows Forms General Trying to get GUI to appear for logged in user, from a service running as System.....

Jeff Wharton

You also need to ensure that the service has the property "Allow service to interact with desktop" set in the Logon properties otherwise no matter what you do, and despite the fact that your code is trying to display one,no message box will appear and your service will hang as it is waiting for a response from the dialog box.




Re: Windows Forms General Trying to get GUI to appear for logged in user, from a service running as System.....

EricBrown

Is there a way to set that setting when installing programtically I'm installing the service from another application, thusly.

SvcInstaller c2 = new SvcInstaller();

c2.InstallService( svcPath, svcName, svcDispName );

Thanks!

Eric





Re: Windows Forms General Trying to get GUI to appear for logged in user, from a service running as System.....

EricBrown

static public void SetServiceDesktopPermission( string serviceName )

{

try

{

// This was added to circumvent the problem of ODBC drivers or other applications

// displaying a password dialog or some dialog that is displayed but doesn't show

// up due to that fact that "Allow service to interact with desktop" is not checked

// and therefore stopping the service from functioning as it is waiting for the dialog

// to be dismissed.

ConnectionOptions coOptions = new ConnectionOptions();

coOptions.Impersonation = ImpersonationLevel.Impersonate;

// CIMV2 is a namespace that contains all of the core OS and hardware classes.

// CIM (Common Information Model) which is an industry standard for describing

// data about applications and devices so that administrators and software

// management programs can control applications and devices on different

// platforms in the same way, ensuring interoperability across a network.

ManagementScope mgmtScope = new System.Management.ManagementScope( @"root\CIMV2", coOptions );

mgmtScope.Connect();

ManagementObject wmiService;

wmiService = new ManagementObject( "Win32_Service.Name='" + serviceName + "'" );

ManagementBaseObject InParam = wmiService.GetMethodParameters( "Change" );

InParam[ "DesktopInteract" ] = true;

wmiService.InvokeMethod( "Change", InParam, null );

}

catch

{

//TODO: Log this error

}

}





Re: Windows Forms General Trying to get GUI to appear for logged in user, from a service running as System.....

EricBrown

Update on this, the "interact with desktop" Setting, causes a secuity risk, if you run a batch file from the service it

shows up too, and if they click on it and hit <Ctrl><c> it give them access to a system permissioned command prompt...

(according to msdn)

So we're probably going to have to go away from that soloution.

Probably going with psshutdown, from Sysinternals.

Eric-





Re: Windows Forms General Trying to get GUI to appear for logged in user, from a service running as System.....

Dan Dittenhafer

Thanks for the update Eric. That's good information to conclude this thread.

Thanks again,

Dan