8 Replies - 924 Views - Last Post: 05 May 2016 - 06:30 PM Rate Topic: -----

#1 kenw232   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-September 15

How to open windows shutdown dialog box from C#?

Posted 05 May 2016 - 02:13 PM

How do I open this shutdown dialog box in C#?

Posted Image
Is This A Good Question/Topic? 0
  • +

Replies To: How to open windows shutdown dialog box from C#?

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,229
  • Joined: 12-December 12

Re: How to open windows shutdown dialog box from C#?

Posted 05 May 2016 - 02:17 PM

You could send Ctrl Alt Delete..

May I ask why your application would offer to shutdown the user's computer?
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How to open windows shutdown dialog box from C#?

Posted 05 May 2016 - 03:34 PM

Ctrl-Alt-Delete doesn't pop that dialog; it shows a different screen.

I've found a way to do what you ask, since just showing the shutdown dialog doesn't seem to be malicious.

This involves calling into one of the Win32 API Dlls, (shell32.dll).

public class Shell32
{
    [DllImport("shell32", EntryPoint = "#60")]
    private static extern int SHShutDownDialog(long p);

    public static void ShutDownDialog()
    {
        int x = SHShutDownDialog(0);
    }
}



You can call the code as Shell32.ShutDownDialog();

I have only tested this in a WinForms project.
Was This Post Helpful? 0
  • +
  • -

#4 kenw232   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-September 15

Re: How to open windows shutdown dialog box from C#?

Posted 05 May 2016 - 04:42 PM

This does not appear to work for me. I have a Console Application and when I try to run this SHShutDownDialog(0) it does nothing. No errors or anything. I guess from http://codegur.com/2...-from-a-thread:

"Your background threads [Console App?] need to post a message to your main thread to ask the main thread to open up the dialog.". Because my console app can't access GUI stuff apparently..

Does anyone know an easy way to have my Console App open a SHShutDownDialog()?
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,989
  • Joined: 05-May 12

Re: How to open windows shutdown dialog box from C#?

Posted 05 May 2016 - 05:44 PM

Even a console application can start a message pump. You don't have to mark your console application as a Windows UI application.

Out of curiosity, why would you want to bring up that dialog from a console application? What is the purpose of your console application?
Was This Post Helpful? 0
  • +
  • -

#6 kenw232   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-September 15

Re: How to open windows shutdown dialog box from C#?

Posted 05 May 2016 - 05:52 PM

Its a wrapper for something else. Difficult to explain Do you have an example of a console app making a message pump? I noticed I can't do something as simple as BeginInvoke(SHShutDownDialog(0));
Was This Post Helpful? 0
  • +
  • -

#7 kenw232   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-September 15

Re: How to open windows shutdown dialog box from C#?

Posted 05 May 2016 - 06:19 PM

I guess if you throw a [STAThread] above your Main() function it makes it a GUI app too. Works anyway.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,989
  • Joined: 05-May 12

Re: How to open windows shutdown dialog box from C#?

Posted 05 May 2016 - 06:22 PM

Please explain. There maybe a simpler solution depending on what you are doing.

A message pump is inherent in any classic Win32 API program. You see it in every Windows programmming tutorial.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,989
  • Joined: 05-May 12

Re: How to open windows shutdown dialog box from C#?

Posted 05 May 2016 - 06:30 PM

View Postkenw232, on 05 May 2016 - 09:19 PM, said:

I guess if you throw a [STAThread] above your Main() function it makes it a GUI app too. Works anyway.


No,it does not make it a GUI app. It essentially tells the .net framework to start your thread in single threaded apartment mode while initializing COM, instead of the default multiple threaded apartment mode. A lot of the Win32 API UI helper objects work only in STA mode.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1