Click here to Skip to main content
16,017,261 members
Home / Discussions / C#
   

C#

 
QuestionCapture and streaming Pin
aspiringCodeMonkey29-Aug-05 21:09
aspiringCodeMonkey29-Aug-05 21:09 
AnswerRe: Capture and streaming Pin
Dario Solera29-Aug-05 21:22
Dario Solera29-Aug-05 21:22 
QuestionSuperscript Pin
Patrik Ruzic29-Aug-05 19:49
Patrik Ruzic29-Aug-05 19:49 
AnswerRe: Superscript Pin
Christian Graus29-Aug-05 20:03
protectorChristian Graus29-Aug-05 20:03 
Questionhelp me to correct my code Pin
sajikp29-Aug-05 19:39
sajikp29-Aug-05 19:39 
AnswerRe: help me to correct my code Pin
Christian Graus29-Aug-05 19:42
protectorChristian Graus29-Aug-05 19:42 
GeneralRe: help me to correct my code Pin
sajikp29-Aug-05 19:58
sajikp29-Aug-05 19:58 
GeneralRe: help me to correct my code Pin
Christian Graus29-Aug-05 20:01
protectorChristian Graus29-Aug-05 20:01 
OK, that's a lot more helpful Smile | :)

So long as we're copying and pasting code, here's the code I found online to do this when I had the same problem as you. I think in a nutshell, some sort of privilege thing needs to happen first, which gives your app the *right* to shut down the PC.

public class Shutdown
{
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}

[DllImport("kernel32.dll", ExactSpelling=true) ]
internal static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok );

[DllImport("advapi32.dll", SetLastError=true) ]
internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid );

[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );

[DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool ExitWindowsEx( int flg, int rea );

internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;

private static void DoExitWin( int flg )
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero );
ok = ExitWindowsEx( flg, 0 );
}

public static void ShutDown()
{
DoExitWin( EWX_POWEROFF | EWX_FORCE );
}
}
}


Christian Graus - Microsoft MVP - C++

-- modified at 2:02 Tuesday 30th August, 2005
GeneralRe: help me to correct my code Pin
sajikp29-Aug-05 20:54
sajikp29-Aug-05 20:54 
QuestionProblems with FileStream Pin
Tehnoon Raza29-Aug-05 19:13
sussTehnoon Raza29-Aug-05 19:13 
AnswerRe: Problems with FileStream Pin
Christian Graus29-Aug-05 19:24
protectorChristian Graus29-Aug-05 19:24 
GeneralRe: Problems with FileStream Pin
Member 171916929-Aug-05 19:34
Member 171916929-Aug-05 19:34 
GeneralRe: Problems with FileStream Pin
Christian Graus29-Aug-05 19:40
protectorChristian Graus29-Aug-05 19:40 
GeneralRe: Problems with FileStream Pin
Matt Gerrans29-Aug-05 20:09
Matt Gerrans29-Aug-05 20:09 
GeneralRe: Problems with FileStream Pin
Dave Kreskowiak30-Aug-05 7:41
mveDave Kreskowiak30-Aug-05 7:41 
QuestionComparing string with string array Pin
binglin29-Aug-05 18:58
binglin29-Aug-05 18:58 
AnswerRe: Comparing string with string array Pin
Guffa29-Aug-05 19:04
Guffa29-Aug-05 19:04 
AnswerRe: Comparing string with string array Pin
mkani30-Aug-05 21:19
mkani30-Aug-05 21:19 
QuestionTimer Pin
Taurian11029-Aug-05 18:28
Taurian11029-Aug-05 18:28 
AnswerRe: Timer Pin
Andy Brummer29-Aug-05 18:41
sitebuilderAndy Brummer29-Aug-05 18:41 
AnswerRe: Timer Pin
Alomgir Miah29-Aug-05 18:47
Alomgir Miah29-Aug-05 18:47 
AnswerRe: Timer Pin
mav.northwind29-Aug-05 20:35
mav.northwind29-Aug-05 20:35 
Questionhow to add web refrence through coding in c# Pin
mkani29-Aug-05 18:15
mkani29-Aug-05 18:15 
AnswerRe: how to add web refrence through coding in c# Pin
Andy Brummer29-Aug-05 18:48
sitebuilderAndy Brummer29-Aug-05 18:48 
QuestionHow to call to MAIN Pin
levi's29-Aug-05 18:08
levi's29-Aug-05 18:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.