#include <windows.h>void EnableDebugPriv() //DEBUG权限,这个例程不错!!!{ HANDLE hToken; LUID sedebugnameValue; TOKEN_PRIVILEGES tkp; OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken); LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue); tkp.PrivilegeCount = 1; tkp.Privileges[0].Luid = sedebugnameValue; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(hToken, false, &tkp, sizeof tkp, NULL, NULL); CloseHandle(hToken); } 里面好像有结构体要单独声明 谢谢
public struct LUID
{
public int LowPart;
public int HighPart;
}public const UInt32 SE_PRIVILEGE_ENABLED_BY_DEFAULT = 0x00000001;
public const UInt32 SE_PRIVILEGE_ENABLED = 0x00000002;
public const UInt32 SE_PRIVILEGE_REMOVED = 0x00000004;
public const UInt32 SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000;public struct TOKEN_PRIVILEGES
{
public UInt32 PrivilegeCount;
[MarshalAs(UnmanagedType.ByValArray)]
public LUID_AND_ATTRIBUTES[] Privileges;
}[StructLayout(LayoutKind.Sequential)]
public struct LUID_AND_ATTRIBUTES
{
public LUID pLuid;
public UInt32 Attributes;
}[DllImport("advapi32.dll", EntryPoint = "OpenProcessToken")]
public static extern int OpenProcessToken(
int ProcessHandle,
int DesiredAccess,
ref int TokenHandle
);[DllImport("kernel32.dll", EntryPoint = "GetCurrentProcess")]
public static extern int GetCurrentProcess();[DllImport("kernel32.dll", EntryPoint = "CloseHandle")]
public static extern int CloseHandle(
int hObject
);[DllImport("advapi32.dll", EntryPoint = "AdjustTokenPrivileges")]
public static extern int AdjustTokenPrivileges(
int TokenHandle,
int DisableAllPrivileges,
ref TOKEN_PRIVILEGES NewState,
int BufferLength,
ref TOKEN_PRIVILEGES PreviousState,
ref int ReturnLength
);[DllImport("advapi32.dll", EntryPoint = "LookupPrivilegeValue")]
public static extern int LookupPrivilegeValue(
string lpSystemName,
string lpName,
ref LUID lpLuid
);public const string SE_DEBUG_NAME = "SeDebugPrivilege";
private const int TOKEN_ADJUST_PRIVILEGES = 0x0020;
private const int TOKEN_QUERY = 0x8;void EnableDebugPriv() //DEBUG权限,这个例程不错!!!
{ int hToken = 0; LUID sedebugnameValue = new LUID(); TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES(); OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref hToken); LookupPrivilegeValue(null, SE_DEBUG_NAME, ref sedebugnameValue); tkp.PrivilegeCount = 1; tkp.Privileges[0].pLuid = sedebugnameValue; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
TOKEN_PRIVILEGES NewP = new TOKEN_PRIVILEGES();
int nNew = 0;
AdjustTokenPrivileges(hToken, 0, ref tkp, Marshal.SizeOf(tkp), ref NewP, ref nNew); CloseHandle(hToken);}随手翻译的,编译通过。没试过。