Hallo,
ich finde meinen Fehler nicht. Beim API-Aufruf von AdjustTokenPrivileges bekomme ich die Fehlermeldung "Feld Privileges des Typs TOKEN_PRIVILEGES kann nicht gemarshallt werden: Dieser Typ kann nicht als Strukturfeld gemarshallt werden." Anbei mein kompletter Code mit Deklaration usw.
Danke für die Hilfe. Gruß Börge Schmüser
public const int SE_PRIVILEGE_ENABLED = 0x00000002;
public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
public const int ANYSIZE_ARRAY = 1;
public const int EWX_LOGOFF = 0;
public const int EWX_SHUTDOWN = 1;
public const int EWX_REBOOT = 2;
public const int EWX_FORCE = 4;
public const int EWX_POWEROFF = 8;
public const int EWX_FORCEIFHUNG = 16;
public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
public const int TOKEN_QUERY = 0x00000008;
[DllImport("user32.dll")]
public static extern System.Boolean ExitWindowsEx(int uFlags, int dwReserved);
[StructLayout(LayoutKind.Sequential)]
public struct LUID
{
public int LowPart;
public int HighPart;
}
[StructLayout(LayoutKind.Sequential)]
public struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public int Attributes;
}
[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_PRIVILEGES
{
public int PrivilegeCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=ANYSIZE_ARRAY)]
public LUID_AND_ATTRIBUTES[] Privileges;
}
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern System.Boolean OpenProcessToken(int ProcessHandle, int DesiredAccess, ref int TokenHandle);
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern int GetCurrentProcess();
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern System.Boolean LookupPrivilegeValue(string lpSystemName, string lpName, [MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern System.Boolean AdjustTokenPrivileges(int TokenHandle, int DisableAllPrivileges, [MarshalAs(UnmanagedType.Struct)] ref TOKEN_PRIVILEGES NewState, int BufferLength, [MarshalAs(UnmanagedType.Struct)] ref TOKEN_PRIVILEGES PreviousState, ref int ReturnLength);
// ------------------------------------------------------------------------------
// API Aufrufe
// ------------------------------------------------------------------------------
public Win32() // Konstruktorlogik
{
}
/// <summary>
///
/// </summary>
public void ShutDown()
{
int hToken = 0;
int int_Size_Of_Old_TOKEN_PRIVILEGES = 0;
int int_Size_Of_TOKEN_PRIVILEGES = 0;
TOKEN_PRIVILEGES myTOKEN_PRIVILEGES = new TOKEN_PRIVILEGES();
myTOKEN_PRIVILEGES.Privileges = new LUID_AND_ATTRIBUTES[ANYSIZE_ARRAY];
TOKEN_PRIVILEGES Old_TOKEN_PRIVILEGES = new TOKEN_PRIVILEGES();
Old_TOKEN_PRIVILEGES.Privileges = new LUID_AND_ATTRIBUTES[ANYSIZE_ARRAY];
LUID tLUID = new LUID();
if(LookupPrivilegeValue(null , SE_SHUTDOWN_NAME, ref tLUID))
{
int intProcessNumber = GetCurrentProcess();
if(intProcessNumber != 0)
{
if ( OpenProcessToken(intProcessNumber, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref hToken) )
{
myTOKEN_PRIVILEGES.PrivilegeCount = 1;
myTOKEN_PRIVILEGES.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
myTOKEN_PRIVILEGES.Privileges[0].Luid.HighPart = tLUID.HighPart;
myTOKEN_PRIVILEGES.Privileges[0].Luid.LowPart = tLUID.LowPart;
//int_Size_Of_TOKEN_PRIVILEGES = Marshal.SizeOf(typeof(TOKEN_PRIVILEGES));
int_Size_Of_TOKEN_PRIVILEGES = 0;
if( AdjustTokenPrivileges(hToken, 0, ref myTOKEN_PRIVILEGES, int_Size_Of_TOKEN_PRIVILEGES, ref Old_TOKEN_PRIVILEGES, ref int_Size_Of_Old_TOKEN_PRIVILEGES) )
// Fehler:
// Feld Privileges des Typs TOKEN_PRIVILEGES kann nicht gemarshallt werden:
// Dieser Typ kann nicht als Strukturfeld gemarshallt werden
ich finde meinen Fehler nicht. Beim API-Aufruf von AdjustTokenPrivileges bekomme ich die Fehlermeldung "Feld Privileges des Typs TOKEN_PRIVILEGES kann nicht gemarshallt werden: Dieser Typ kann nicht als Strukturfeld gemarshallt werden." Anbei mein kompletter Code mit Deklaration usw.
Danke für die Hilfe. Gruß Börge Schmüser
public const int SE_PRIVILEGE_ENABLED = 0x00000002;
public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
public const int ANYSIZE_ARRAY = 1;
public const int EWX_LOGOFF = 0;
public const int EWX_SHUTDOWN = 1;
public const int EWX_REBOOT = 2;
public const int EWX_FORCE = 4;
public const int EWX_POWEROFF = 8;
public const int EWX_FORCEIFHUNG = 16;
public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
public const int TOKEN_QUERY = 0x00000008;
[DllImport("user32.dll")]
public static extern System.Boolean ExitWindowsEx(int uFlags, int dwReserved);
[StructLayout(LayoutKind.Sequential)]
public struct LUID
{
public int LowPart;
public int HighPart;
}
[StructLayout(LayoutKind.Sequential)]
public struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public int Attributes;
}
[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_PRIVILEGES
{
public int PrivilegeCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=ANYSIZE_ARRAY)]
public LUID_AND_ATTRIBUTES[] Privileges;
}
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern System.Boolean OpenProcessToken(int ProcessHandle, int DesiredAccess, ref int TokenHandle);
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern int GetCurrentProcess();
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern System.Boolean LookupPrivilegeValue(string lpSystemName, string lpName, [MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern System.Boolean AdjustTokenPrivileges(int TokenHandle, int DisableAllPrivileges, [MarshalAs(UnmanagedType.Struct)] ref TOKEN_PRIVILEGES NewState, int BufferLength, [MarshalAs(UnmanagedType.Struct)] ref TOKEN_PRIVILEGES PreviousState, ref int ReturnLength);
// ------------------------------------------------------------------------------
// API Aufrufe
// ------------------------------------------------------------------------------
public Win32() // Konstruktorlogik
{
}
/// <summary>
///
/// </summary>
public void ShutDown()
{
int hToken = 0;
int int_Size_Of_Old_TOKEN_PRIVILEGES = 0;
int int_Size_Of_TOKEN_PRIVILEGES = 0;
TOKEN_PRIVILEGES myTOKEN_PRIVILEGES = new TOKEN_PRIVILEGES();
myTOKEN_PRIVILEGES.Privileges = new LUID_AND_ATTRIBUTES[ANYSIZE_ARRAY];
TOKEN_PRIVILEGES Old_TOKEN_PRIVILEGES = new TOKEN_PRIVILEGES();
Old_TOKEN_PRIVILEGES.Privileges = new LUID_AND_ATTRIBUTES[ANYSIZE_ARRAY];
LUID tLUID = new LUID();
if(LookupPrivilegeValue(null , SE_SHUTDOWN_NAME, ref tLUID))
{
int intProcessNumber = GetCurrentProcess();
if(intProcessNumber != 0)
{
if ( OpenProcessToken(intProcessNumber, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref hToken) )
{
myTOKEN_PRIVILEGES.PrivilegeCount = 1;
myTOKEN_PRIVILEGES.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
myTOKEN_PRIVILEGES.Privileges[0].Luid.HighPart = tLUID.HighPart;
myTOKEN_PRIVILEGES.Privileges[0].Luid.LowPart = tLUID.LowPart;
//int_Size_Of_TOKEN_PRIVILEGES = Marshal.SizeOf(typeof(TOKEN_PRIVILEGES));
int_Size_Of_TOKEN_PRIVILEGES = 0;
if( AdjustTokenPrivileges(hToken, 0, ref myTOKEN_PRIVILEGES, int_Size_Of_TOKEN_PRIVILEGES, ref Old_TOKEN_PRIVILEGES, ref int_Size_Of_Old_TOKEN_PRIVILEGES) )
// Fehler:
// Feld Privileges des Typs TOKEN_PRIVILEGES kann nicht gemarshallt werden:
// Dieser Typ kann nicht als Strukturfeld gemarshallt werden