NextPVR Forums
  • ______
  • Home
  • New Posts
  • Wiki
  • Members
  • Help
  • Search
  • Register
  • Login
  • Home
  • Wiki
  • Members
  • Help
  • Search
NextPVR Forums Public Add-ons (3rd party plugins, utilities and skins) Old Stuff (Legacy) GB-PVR Support (legacy) v
« Previous 1 … 1153 1154 1155 1156 1157 … 1231 Next »
Shutdown Plugin on MVP

 
  • 0 Vote(s) - 0 Average
Shutdown Plugin on MVP
adi2004
Offline

Junior Member

Posts: 26
Threads: 7
Joined: Oct 2004
#11
2004-10-20, 05:35 PM
[b Wrote:Quote[/b] (Huw @ Oct. 20 2004,11:25)]System.Management v1.1 SP1 fails to enable privileges required to execute methods on WMI classes.

I have a fixed shutdown.dll if you want a copy.
Thanks. I would like to try it. Could you upload it here as an attachment?
Huw
Offline

Member

Posts: 219
Threads: 7
Joined: Oct 2004
#12
2004-10-20, 05:36 PM
The problem is it just doesn't work if you have the .net 1.1 sp1 installed, you need to set the priveleges differently.

The code below is my replacement ShutDown.cs

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">using System;
using System.Management;
using System.Runtime.InteropServices;
using System.Security;
using System.Diagnostics;

namespace GBPVR.Plugins.ShutDownPlugin

{
class ShutMeDown
{

/// <summary>
/// Shutdown or restarts the computer.
/// </summary>
/// <param name="action">"Shutdown" or "Reboot"</param>
public static void Shutdown(string action)

{
ManagementScope sc = null;
ConnectionOptions co = null;
ObjectQuery oq = null;
ManagementObjectSearcher os = null;

try
{
co = new ConnectionOptions();
co.Impersonation = ImpersonationLevel.Impersonate;
co.EnablePrivileges = true;
TokenAdjuster.SetPrivilege("SeShutdownPrivilege", true); //New Line to set privilege
sc = new ManagementScope(@"\ROOT\CIMV2", co);
sc.Connect();
oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
os = new ManagementObjectSearcher(sc, oq);

foreach( ManagementObject operatingSystem in os.Get())
{
ManagementBaseObject outParams = operatingSystem.InvokeMethod (action, null, null);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
os.Dispose();
}
}
}
////////TokenAdjuster Class
public class TokenAdjuster
{
// PInvoke stuff required to set/enable security privileges
[DllImport("advapi32", SetLastError=true),SuppressUnmanagedCodeSecurityAttribute]
static extern int OpenProcessToken(
System.IntPtr ProcessHandle, // handle to process
int DesiredAccess, // desired access to process
ref IntPtr TokenHandle // handle to open access token
);

[DllImport("kernel32", SetLastError=true),SuppressUnmanagedCodeSecurityAttribute]
static extern bool CloseHandle(IntPtr handle);

[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true),SuppressUnmanagedCodeSecurityAttribute]
static extern int AdjustTokenPrivileges(
IntPtr TokenHandle,
int DisableAllPrivileges,
IntPtr NewState,
int BufferLength,
IntPtr PreviousState,
ref int ReturnLength
);

[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true),SuppressUnmanagedCodeSecurityAttribute]
static extern bool LookupPrivilegeValue(
string lpSystemName,
string lpName,
ref LUID lpLuid
);

[StructLayout(LayoutKind.Sequential)]
internal struct LUID {
internal int LowPart;
internal int HighPart;
}

[StructLayout(LayoutKind.Sequential)]
struct LUID_AND_ATTRIBUTES {
LUID Luid;
int Attributes;
}

[StructLayout(LayoutKind.Sequential)]
struct _PRIVILEGE_SET {
int PrivilegeCount;
int Control;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=1)] // ANYSIZE_ARRAY = 1
LUID_AND_ATTRIBUTES [] Privileges;
}

[StructLayout(LayoutKind.Sequential)]
internal struct TOKEN_PRIVILEGES {
internal int PrivilegeCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=3)]
internal int[] Privileges;
}
const int SE_PRIVILEGE_ENABLED = 0x00000002;
const int TOKEN_ADJUST_PRIVILEGES = 0X00000020;
const int TOKEN_QUERY = 0X00000008;
const int TOKEN_ALL_ACCESS = 0X001f01ff;
const int PROCESS_QUERY_INFORMATION = 0X00000400;

public static bool SetPrivilege (string lpszPrivilege, bool bEnablePrivilege )
{
bool retval = false;
int ltkpOld = 0;
IntPtr hToken = IntPtr.Zero;
TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES();
tkp.Privileges = new int[3];
TOKEN_PRIVILEGES tkpOld = new TOKEN_PRIVILEGES();
tkpOld.Privileges = new int[3];
LUID tLUID = new LUID();
tkp.PrivilegeCount = 1;
if (bEnablePrivilege)
tkp.Privileges[2] = SE_PRIVILEGE_ENABLED;
else
tkp.Privileges[2] = 0;
if(LookupPrivilegeValue(null , lpszPrivilege , ref tLUID))
{
Process proc = Process.GetCurrentProcess();
if(proc.Handle != IntPtr.Zero) {
if (OpenProcessToken(proc.Handle, TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, ref hToken) != 0)
{
tkp.PrivilegeCount = 1;
tkp.Privileges[2] = SE_PRIVILEGE_ENABLED;
tkp.Privileges[1] = tLUID.HighPart;
tkp.Privileges[0] = tLUID.LowPart;
const int bufLength = 256;
IntPtr tu = Marshal.AllocHGlobal( bufLength );
Marshal.StructureToPtr(tkp, tu, true);
if(AdjustTokenPrivileges(hToken, 0, tu, bufLength, IntPtr.Zero, ref ltkpOld) != 0)
{
// successful AdjustTokenPrivileges doesn't mean privilege could be changed
if (Marshal.GetLastWin32Error() == 0)
{
retval = true; // Token changed
}
}
TOKEN_PRIVILEGES tokp = (TOKEN_PRIVILEGES) Marshal.PtrToStructure(tu,
typeof(TOKEN_PRIVILEGES) );
Marshal.FreeHGlobal( tu );
}
}
}
if (hToken != IntPtr.Zero)
{
CloseHandle(hToken);
}
return retval;
}
} // End class TokenAdjuster
}[/QUOTE]

It has an additional class TokenAdjuster, just prior to creating your management object a new line is added to set the privilege correctly

TokenAdjuster.SetPrivilege(&quot;SeShutdownPrivilege&quot;, true);
Huw
Offline

Member

Posts: 219
Threads: 7
Joined: Oct 2004
#13
2004-10-20, 05:46 PM
[b Wrote:Quote[/b] (adi2004 @ Oct. 20 2004,13:35)]
[b Wrote:Quote[/b] (Huw @ Oct. 20 2004,11:25)]System.Management v1.1 SP1 fails to enable privileges required to execute methods on WMI classes.

I have a fixed shutdown.dll if you want a copy.
Thanks.  I would like to try it.  Could you upload it here as an attachment?
How do I do an attachment ?
colin
Offline

Senior Member

Posts: 683
Threads: 39
Joined: Nov 2003
#14
2004-10-20, 06:38 PM
Thanks Huw,

you cannot do attachments in this forum but feel free to email it to me (save me doing indentation!Wink. I know i had to change the privileges, but i was not sure what I was doing here, now i doSmile

Cheers,
Colin.
adi2004
Offline

Junior Member

Posts: 26
Threads: 7
Joined: Oct 2004
#15
2004-10-20, 09:15 PM
If I'm following this, it seems that the shutdown plugin can never work if .Net Framework 1.1 SP1 is installed, so I think I'll just dip out of this one and use the on/off button on the front of the case. Wink It's not clear to me why Microsoft have made it so hard to call such a simple function. Didn't you used to be able to this with a single Win32 call?

Thanks, anyway.
Huw
Offline

Member

Posts: 219
Threads: 7
Joined: Oct 2004
#16
2004-10-20, 11:34 PM
Here is a link to the fixed dll for adi2004, and the replacement .cs file for colin.

In answer to your question adi, with the current code it will not work with .NET 1.1 sp1, however the new code will work.

Shutdown code
adi2004
Offline

Junior Member

Posts: 26
Threads: 7
Joined: Oct 2004
#17
2004-10-21, 01:33 PM
[b Wrote:Quote[/b] (Huw @ Oct. 20 2004,19:34)]Here is a link to the fixed dll for adi2004, and the replacement .cs file for colin.

In answer to your question adi, with the current code it will not work with .NET 1.1 sp1, however the new code will work.

Shutdown code
Thanks, Huw. Just tested the replacement DLL and the Shutdown and Restart options work perfectly.

Good job.
Huw
Offline

Member

Posts: 219
Threads: 7
Joined: Oct 2004
#18
2004-10-21, 04:20 PM
[b Wrote:Quote[/b] (adi2004 @ Oct. 21 2004,14:33)]Thanks, Huw. Just tested the replacement DLL and the Shutdown and Restart options work perfectly.

Good job.

Great [Image: smile.gif]
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)

Pages (2): « Previous 1 2


Possibly Related Threads…
Thread Author Replies Views Last Post
  Inhibit Computer Shutdown While Recording rwnz 52 31,053 2012-04-03, 11:40 AM
Last Post: pBS
  What LCD Plugin or Utility to use these days? Snooze 66 23,365 2011-04-06, 02:51 AM
Last Post: Snooze
  Analog HD Recording Plugin To Record 16:9 Widescreen BTJustice 18 6,477 2010-09-12, 05:14 AM
Last Post: johnsonx42
  Any provision of auto shutdown after recording? seymoria 15 4,755 2010-05-10, 06:17 PM
Last Post: morser
  Shutdown when recording finishes kojak 5 2,943 2010-01-29, 03:33 PM
Last Post: martint123
  Where is "enable multidec plugin support" tcmy95 2 1,660 2009-12-06, 10:58 PM
Last Post: tcmy95
  Shutdown/Restart Exits to Desktop smajor 1 1,673 2009-09-06, 06:59 PM
Last Post: sub
  1.3.11 Slick Plugin Specific Backgrounds don't load crlorentzen 1 1,366 2009-08-29, 03:23 AM
Last Post: crlorentzen
  How to add a plugin to the selction list DRL 2 1,709 2009-08-27, 05:19 AM
Last Post: DRL
  1.3.7 - Crash in Settings plugin JavaWiz 3 2,017 2009-08-24, 06:06 PM
Last Post: gEd

  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Designed by D&D, modified by NextPVR - Powered by MyBB

Linear Mode
Threaded Mode