W
William McIlroy
I have used WinSigGen, the P/Invoke Interop Assistant, to rewrite some gnarly
Win32 code in C#. It is astonishing the decorations it devises. Anyway, the
class, below, will not load when it is called and I don't know why. The
message I get is BEGIN Could not load type 'SystemBackup.ERPU1' from assembly
'SystemBackup, Version=1.0.0.0, Culter=neutral, PublicKeyToken=null' END
That's what the exception handler says the platform is complaining about.
The "call" to the class, which is completely static, is ERPU1.mein(true)
ALSO, while you're puzzling over that, I need help assigning a pointer to
NewState so that it can be passed to AdjustTokenPrivileges(). There is
supposed to be a method in the System.IntPtr class called ToPointer(), but I
cannot get the compiler to accept my lame attempts to use it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SystemBackup
{
class ERPU1
{
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct LUID_AND_ATTRIBUTES
{
/// LUID->_LUID
public LUID Luid;
/// DWORD->unsigned int
public uint Attributes;
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct LUID
{
/// DWORD->unsigned int
public uint LowPart;
/// LONG->int
public int HighPart;
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct TOKEN_PRIVILEGES
{
/// DWORD->unsigned int
public uint PrivilegeCount;
/// LUID_AND_ATTRIBUTES[1]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst = 1, ArraySubType =
System.Runtime.InteropServices.UnmanagedType.Struct)]
public LUID_AND_ATTRIBUTES[] Privileges;
}
static LUID luid;
unsafe static TOKEN_PRIVILEGES NewState;
static System.IntPtr hToken;
/// TOKEN_ADJUST_PRIVILEGES -> (0x0020)
public const int TOKEN_ADJUST_PRIVILEGES = 32;
/// TOKEN_QUERY -> (0x0008)
public const int TOKEN_QUERY = 8;
/// SE_PRIVILEGE_ENABLED -> (0x00000002L)
public const int SE_PRIVILEGE_ENABLED = 2;
public const int FALSE = 0;
public const int TRUE = 1;
/// Return Type: HANDLE->void*
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll",
EntryPoint = "GetCurrentProcess")]
public static extern System.IntPtr GetCurrentProcess();
/// Return Type: BOOL->int
///ProcessHandle: HANDLE->void*
///DesiredAccess: DWORD->unsigned int
///TokenHandle: PHANDLE->HANDLE*
[System.Runtime.InteropServices.DllImportAttribute("advapi32.dll",
EntryPoint = "OpenProcessToken")]
[return:
System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool
OpenProcessToken([System.Runtime.InteropServices.InAttribute()] System.IntPtr
ProcessHandle, uint DesiredAccess, out System.IntPtr TokenHandle);
public static bool GetTokenForTheCurrentProcess()
{
System.IntPtr hCurrentProcessHandle = GetCurrentProcess();
return OpenProcessToken(hCurrentProcessHandle,
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out hToken);
}
/// Return Type: BOOL->int
///lpSystemName: LPCSTR->CHAR*
///lpName: LPCSTR->CHAR*
///lpLuid: PLUID->_LUID*
[System.Runtime.InteropServices.DllImportAttribute("advapi32.dll",
EntryPoint = "LookupPrivilegeValueA")]
[return:
System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool
LookupPrivilegeValueA([System.Runtime.InteropServices.InAttribute()]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
string lpSystemName, [System.Runtime.InteropServices.InAttribute()]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
string lpName, [System.Runtime.InteropServices.OutAttribute()] out LUID
lpLuid);
/// Return Type: BOOL->int
///hObject: HANDLE->void*
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll",
EntryPoint = "CloseHandle")]
[return:
System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool
CloseHandle([System.Runtime.InteropServices.InAttribute()] System.IntPtr
hObject);
/// Return Type: BOOL->int
///TokenHandle: HANDLE->void*
///DisableAllPrivileges: BOOL->int
///NewState: PTOKEN_PRIVILEGES->_TOKEN_PRIVILEGES*
///BufferLength: DWORD->unsigned int
///PreviousState: PTOKEN_PRIVILEGES->_TOKEN_PRIVILEGES*
///ReturnLength: PDWORD->DWORD*
[System.Runtime.InteropServices.DllImportAttribute("advapi32.dll",
EntryPoint = "AdjustTokenPrivileges")]
[return:
System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool
AdjustTokenPrivileges([System.Runtime.InteropServices.InAttribute()]
System.IntPtr TokenHandle,
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
bool DisableAllPrivileges, [System.Runtime.InteropServices.InAttribute()]
System.IntPtr NewState, uint BufferLength, System.IntPtr PreviousState,
System.IntPtr ReturnLength);
public static void mein(bool fEnable)
{
if (GetTokenForTheCurrentProcess())
{ }
else
{
MessageBox.Show("Unable to obtain token of current process.");
return;
}
if (LookupPrivilegeValueA("", "", out luid)) // first parameter
should be NULL
{ }
else
{
CloseHandle(hToken);
MessageBox.Show("Unable to execute LookupPrivilegeValue.");
return;
}
NewState.PrivilegeCount = 1;
NewState.Privileges[0].Luid = luid;
NewState.Privileges[0].Attributes = (fEnable ?
(uint)SE_PRIVILEGE_ENABLED : (uint)0);
/*
if (!AdjustTokenPrivileges(hToken,
FALSE,
&NewState,
0,
NULL,
NULL))
*/
unsafe
{
System.IntPtr pNewState = System.IntPtr.Zero;
System.IntPtr pNULL = System.IntPtr.Zero;
// pNewState.= (System.IntPtr)&NewState;
if (AdjustTokenPrivileges(hToken, false, pNewState, 0,
pNULL, pNULL))
{ }
else
{ }
}
}
}
}
Win32 code in C#. It is astonishing the decorations it devises. Anyway, the
class, below, will not load when it is called and I don't know why. The
message I get is BEGIN Could not load type 'SystemBackup.ERPU1' from assembly
'SystemBackup, Version=1.0.0.0, Culter=neutral, PublicKeyToken=null' END
That's what the exception handler says the platform is complaining about.
The "call" to the class, which is completely static, is ERPU1.mein(true)
ALSO, while you're puzzling over that, I need help assigning a pointer to
NewState so that it can be passed to AdjustTokenPrivileges(). There is
supposed to be a method in the System.IntPtr class called ToPointer(), but I
cannot get the compiler to accept my lame attempts to use it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SystemBackup
{
class ERPU1
{
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct LUID_AND_ATTRIBUTES
{
/// LUID->_LUID
public LUID Luid;
/// DWORD->unsigned int
public uint Attributes;
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct LUID
{
/// DWORD->unsigned int
public uint LowPart;
/// LONG->int
public int HighPart;
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct TOKEN_PRIVILEGES
{
/// DWORD->unsigned int
public uint PrivilegeCount;
/// LUID_AND_ATTRIBUTES[1]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst = 1, ArraySubType =
System.Runtime.InteropServices.UnmanagedType.Struct)]
public LUID_AND_ATTRIBUTES[] Privileges;
}
static LUID luid;
unsafe static TOKEN_PRIVILEGES NewState;
static System.IntPtr hToken;
/// TOKEN_ADJUST_PRIVILEGES -> (0x0020)
public const int TOKEN_ADJUST_PRIVILEGES = 32;
/// TOKEN_QUERY -> (0x0008)
public const int TOKEN_QUERY = 8;
/// SE_PRIVILEGE_ENABLED -> (0x00000002L)
public const int SE_PRIVILEGE_ENABLED = 2;
public const int FALSE = 0;
public const int TRUE = 1;
/// Return Type: HANDLE->void*
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll",
EntryPoint = "GetCurrentProcess")]
public static extern System.IntPtr GetCurrentProcess();
/// Return Type: BOOL->int
///ProcessHandle: HANDLE->void*
///DesiredAccess: DWORD->unsigned int
///TokenHandle: PHANDLE->HANDLE*
[System.Runtime.InteropServices.DllImportAttribute("advapi32.dll",
EntryPoint = "OpenProcessToken")]
[return:
System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool
OpenProcessToken([System.Runtime.InteropServices.InAttribute()] System.IntPtr
ProcessHandle, uint DesiredAccess, out System.IntPtr TokenHandle);
public static bool GetTokenForTheCurrentProcess()
{
System.IntPtr hCurrentProcessHandle = GetCurrentProcess();
return OpenProcessToken(hCurrentProcessHandle,
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out hToken);
}
/// Return Type: BOOL->int
///lpSystemName: LPCSTR->CHAR*
///lpName: LPCSTR->CHAR*
///lpLuid: PLUID->_LUID*
[System.Runtime.InteropServices.DllImportAttribute("advapi32.dll",
EntryPoint = "LookupPrivilegeValueA")]
[return:
System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool
LookupPrivilegeValueA([System.Runtime.InteropServices.InAttribute()]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
string lpSystemName, [System.Runtime.InteropServices.InAttribute()]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
string lpName, [System.Runtime.InteropServices.OutAttribute()] out LUID
lpLuid);
/// Return Type: BOOL->int
///hObject: HANDLE->void*
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll",
EntryPoint = "CloseHandle")]
[return:
System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool
CloseHandle([System.Runtime.InteropServices.InAttribute()] System.IntPtr
hObject);
/// Return Type: BOOL->int
///TokenHandle: HANDLE->void*
///DisableAllPrivileges: BOOL->int
///NewState: PTOKEN_PRIVILEGES->_TOKEN_PRIVILEGES*
///BufferLength: DWORD->unsigned int
///PreviousState: PTOKEN_PRIVILEGES->_TOKEN_PRIVILEGES*
///ReturnLength: PDWORD->DWORD*
[System.Runtime.InteropServices.DllImportAttribute("advapi32.dll",
EntryPoint = "AdjustTokenPrivileges")]
[return:
System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool
AdjustTokenPrivileges([System.Runtime.InteropServices.InAttribute()]
System.IntPtr TokenHandle,
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
bool DisableAllPrivileges, [System.Runtime.InteropServices.InAttribute()]
System.IntPtr NewState, uint BufferLength, System.IntPtr PreviousState,
System.IntPtr ReturnLength);
public static void mein(bool fEnable)
{
if (GetTokenForTheCurrentProcess())
{ }
else
{
MessageBox.Show("Unable to obtain token of current process.");
return;
}
if (LookupPrivilegeValueA("", "", out luid)) // first parameter
should be NULL
{ }
else
{
CloseHandle(hToken);
MessageBox.Show("Unable to execute LookupPrivilegeValue.");
return;
}
NewState.PrivilegeCount = 1;
NewState.Privileges[0].Luid = luid;
NewState.Privileges[0].Attributes = (fEnable ?
(uint)SE_PRIVILEGE_ENABLED : (uint)0);
/*
if (!AdjustTokenPrivileges(hToken,
FALSE,
&NewState,
0,
NULL,
NULL))
*/
unsafe
{
System.IntPtr pNewState = System.IntPtr.Zero;
System.IntPtr pNULL = System.IntPtr.Zero;
// pNewState.= (System.IntPtr)&NewState;
if (AdjustTokenPrivileges(hToken, false, pNewState, 0,
pNULL, pNULL))
{ }
else
{ }
}
}
}
}