Was ist falsch? Const Problem



  • Ich hab folgenden Code in meinem Programm:

    private const object TOKEN_ADJUST_PRIVILEGES = 32;
    	private const object TOKEN_QUERY = 8;
    	private const object SE_PRIVILEGE_ENABLED = 2;
    	private const object FORMAT_MESSAGE_FROM_SYSTEM = 4096;
    	private const object EWX_FORCE = 4;
    

    Jede dieser Zeilen erzeugt den Fehler:

    Der Ausdruck, der 'WindowsController.TOKEN_ADJUST_PRIVILEGES' zugewiesen wird, muss konstant sein

    u.s.w.

    Was ist da Falsch? Das sind doch Konstane Werte oder?



  • Ändere mal Deinen Datentyp in Integer.



  • Ne klappt auch nicht - hier der ganze code.

    using System;
    using System.Text;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    public class WindowsController
    {
    	public enum RestartOptions
    	{
    		LogOff = 0,
    		PowerOff = 8,
    		Reboot = 2,
    		ShutDown = 1,
    		Suspend = -1,
    		Hibernate = -2
    	}
    	public struct LUID
    	{
    		int LowPart;
    		int HighPart;
    	}
    	public struct LUID_AND_ATTRIBUTES
    	{
    		LUID pLuid;
    		int Attributes;
    	}
    	public struct TOKEN_PRIVILEGES
    	{
    		int PrivilegeCount;
    		LUID_AND_ATTRIBUTES Privileges;
    	}
    	private const object  TOKEN_ADJUST_PRIVILEGES = 32;
    	private const object TOKEN_QUERY = 8;
    	private const object SE_PRIVILEGE_ENABLED = 2;
    	private const object FORMAT_MESSAGE_FROM_SYSTEM = 4096;
    	private const object EWX_FORCE = 4;
    	[System.Runtime.InteropServices.DllImport("kernel32", EntryPoint="LoadLibraryA")]
    	static extern IntPtr LoadLibrary(string lpLibFileName);
    	[System.Runtime.InteropServices.DllImport("kernel32")]
    	static extern int FreeLibrary(IntPtr hLibModule);
    	[System.Runtime.InteropServices.DllImport("kernel32")]
    	static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
    	[System.Runtime.InteropServices.DllImport("Powrprof")]
    	static extern int SetSuspendState(int Hibernate, int ForceCritical, int DisableWakeEvent);
    	[System.Runtime.InteropServices.DllImport("advapi32.dll")]
    	static extern int OpenProcessToken(IntPtr ProcessHandle, int DesiredAccess, ref IntPtr TokenHandle);
    	[System.Runtime.InteropServices.DllImport("advapi32.dll", EntryPoint="LookupPrivilegeValueA")]
    	static extern int LookupPrivilegeValue(string lpSystemName, string lpName, ref LUID lpLuid);
    	[System.Runtime.InteropServices.DllImport("advapi32.dll")]
    	static extern int AdjustTokenPrivileges(IntPtr TokenHandle, int DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, int BufferLength, ref TOKEN_PRIVILEGES PreviousState, ref int ReturnLength);
    	[System.Runtime.InteropServices.DllImport("user32")]
    	static extern int ExitWindowsEx(int uFlags, int dwReserved);
    	[System.Runtime.InteropServices.DllImport("kernel32", EntryPoint="FormatMessageA")]
    	static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int nSize, int Arguments);
    
    	public void ExitWindows(RestartOptions how, bool force)
    	{
    		if (how == RestartOptions.Suspend) {
    			SuspendSystem(false, force);
    		} else if (how == RestartOptions.Hibernate) {
    			SuspendSystem(true, force);
    		} else {
    			ExitWindows(Convert.ToInt32(how), force);
    		}
    	}
    
    	protected void ExitWindows(int how, bool force)
    	{
    		EnableToken("SeShutdownPrivilege");
    		if (force) {
    			how = how | EWX_FORCE;
    		}
    		if ((ExitWindowsEx(how, 0) == 0)) {
    			throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
    		}
    	}
    
    	protected void EnableToken(string privilege)
    	{
    		if (!(CheckEntryPoint("advapi32.dll", "AdjustTokenPrivileges"))) {
    			return;
    		}
    		IntPtr tokenHandle = IntPtr.Zero;
    		object privilegeLUID = new LUID();
    		object newPrivileges = new TOKEN_PRIVILEGES();
    		TOKEN_PRIVILEGES tokenPrivileges;
    		if ((OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, tokenHandle)) == 0) {
    			throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
    		}
    		if ((LookupPrivilegeValue("", privilege, privilegeLUID)) == 0) {
    			throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
    		}
    		tokenPrivileges.PrivilegeCount = 1;
    		tokenPrivileges.Privileges.Attributes = SE_PRIVILEGE_ENABLED;
    		tokenPrivileges.Privileges.pLuid = privilegeLUID;
    		int Size = 4;
    		if ((AdjustTokenPrivileges(tokenHandle, 0, tokenPrivileges, 4 + (12 * tokenPrivileges.PrivilegeCount), newPrivileges, Size)) == 0) {
    			throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
    		}
    	}
    
    	protected void SuspendSystem(bool hibernate, bool force)
    	{
    		if (!(CheckEntryPoint("powrprof.dll", "SetSuspendState"))) {
    			throw new PlatformNotSupportedException("The SetSuspendState method is not supported on this system!");
    		}
    		SetSuspendState(Convert.ToInt32(IIf(hibernate, 1, 0)), Convert.ToInt32(IIf(force, 1, 0)), 0);
    	}
    
    	protected bool CheckEntryPoint(string library, string method)
    	{
    		IntPtr libPtr = LoadLibrary(library);
    		if (!(libPtr.Equals(IntPtr.Zero))) {
    			if (!(GetProcAddress(libPtr, method).Equals(IntPtr.Zero))) {
    				FreeLibrary(libPtr);
    				return true;
    			}
    			FreeLibrary(libPtr);
    		}
    		return false;
    	}
    
    	protected string FormatError(int number)
    	{
    		object Buffer = new StringBuilder(255);
    		FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, IntPtr.Zero, number, 0, Buffer, Buffer.Capacity, 0);
    		return Buffer.ToString();
    	}
    }
    public class PrivilegeException : Exception
    {
    
    	public PrivilegeException()
    	{
    		base.New();
    	}
    
    	public PrivilegeException(string message)
    	{
    		base.New(message);
    	}
    }
    struct Module1
    {
    
    	void Main()
    	{
    
    		WindowsController shutdown = new WindowsController();
    		shutdown.ExitWindows(shutdown.RestartOptions.Hibernate, true);
    	}
    }
    

    Ich will einfach eine kleine Konsolenanwendung die den PC in den Ruhezustand schickt schreiben.....



  • private const int TOKEN_ADJUST_PRIVILEGES = 32;
    private const int TOKEN_QUERY = 8;
    private const int SE_PRIVILEGE_ENABLED = 2;
    private const int FORMAT_MESSAGE_FROM_SYSTEM = 4096;
    private const int EWX_FORCE = 4;
    

    Das funktioniert, ich habe es getestet, dann musst Du noch andere Fehler im Code haben.



  • Kann sein das der Code fehlerhaft ist - ich habe in von vb.net Code hergeleitet*.....naja ... ich habe den Code ja gepostet 😃

    * d.h. SharpDevelop hat vb.net Code in C# Code umgewandelt 😉


Anmelden zum Antworten