cout << "Hello World!" << endl; // 张鲁夺 :: 个人博客,幸福着你的幸福!


tstring

作者:鲁夺,2018年10月27日,原创文章,转载请注明出处!

原文:http://zhangluduo.com/article/aea68ed3/


一个简单的代码片断,不多做解释。



//   #define SE_CREATE_TOKEN_NAME              TEXT("SeCreateTokenPrivilege")
//   #define SE_ASSIGNPRIMARYTOKEN_NAME        TEXT("SeAssignPrimaryTokenPrivilege")
//   #define SE_LOCK_MEMORY_NAME               TEXT("SeLockMemoryPrivilege")
//   #define SE_INCREASE_QUOTA_NAME            TEXT("SeIncreaseQuotaPrivilege")
//   #define SE_UNSOLICITED_INPUT_NAME         TEXT("SeUnsolicitedInputPrivilege")
//   #define SE_MACHINE_ACCOUNT_NAME           TEXT("SeMachineAccountPrivilege")
//   #define SE_TCB_NAME                       TEXT("SeTcbPrivilege")
//   #define SE_SECURITY_NAME                  TEXT("SeSecurityPrivilege")
//   #define SE_TAKE_OWNERSHIP_NAME            TEXT("SeTakeOwnershipPrivilege")
//   #define SE_LOAD_DRIVER_NAME               TEXT("SeLoadDriverPrivilege")
//   #define SE_SYSTEM_PROFILE_NAME            TEXT("SeSystemProfilePrivilege")
//   #define SE_SYSTEMTIME_NAME                TEXT("SeSystemtimePrivilege")
//   #define SE_PROF_SINGLE_PROCESS_NAME       TEXT("SeProfileSingleProcessPrivilege")
//   #define SE_INC_BASE_PRIORITY_NAME         TEXT("SeIncreaseBasePriorityPrivilege")
//   #define SE_CREATE_PAGEFILE_NAME           TEXT("SeCreatePagefilePrivilege")
//   #define SE_CREATE_PERMANENT_NAME          TEXT("SeCreatePermanentPrivilege")
//   #define SE_BACKUP_NAME                    TEXT("SeBackupPrivilege")
//   #define SE_RESTORE_NAME                   TEXT("SeRestorePrivilege")
//   #define SE_SHUTDOWN_NAME                  TEXT("SeShutdownPrivilege")
//   #define SE_DEBUG_NAME                     TEXT("SeDebugPrivilege")
//   #define SE_AUDIT_NAME                     TEXT("SeAuditPrivilege")
//   #define SE_SYSTEM_ENVIRONMENT_NAME        TEXT("SeSystemEnvironmentPrivilege")
//   #define SE_CHANGE_NOTIFY_NAME             TEXT("SeChangeNotifyPrivilege")
//   #define SE_REMOTE_SHUTDOWN_NAME           TEXT("SeRemoteShutdownPrivilege")

BOOL SetPrivilege(LPCTSTR privilege_name)
{
   HANDLE hToken = NULL;

   if (!OpenProcessToken(GetCurrentProcess(), 
      TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
   {
      return FALSE;
   }

   LUID luid;

   if (!LookupPrivilegeValue(NULL, privilege_name, &luid))
   {
      CloseHandle(hToken);
      return FALSE;
   }

   TOKEN_PRIVILEGES tkp;
   tkp.PrivilegeCount           = 1;   
   tkp.Privileges[0].Luid       = luid;   
   tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
   f
   if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof tkp, NULL, NULL))
   {
      CloseHandle(hToken);
      return FALSE;
   }

   CloseHandle(hToken);
   return TRUE;
}

BOOL EnumProcess(vector<PROCESS_INFO>* Processes)
{
   
   PROCESSENTRY32 pe32;;
   // Set the size of the structure before using it.
   pe32.dwSize = sizeof( PROCESSENTRY32 );

   // Take a snapshot of all processes in the system.
   HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
   if (hProcessSnap == INVALID_HANDLE_VALUE)
   {
      return FALSE;
   }

   // Retrieve information about the first process,
   // and exit if unsuccessful
   if( !Process32First( hProcessSnap, &pe32 ) )
   {
      CloseHandle (hProcessSnap);
      return FALSE;
   }

   // Now walk the snapshot of processes, and
   // display information about each process in turn
   do
   {
      PROCESS_INFO t;
      t.ProcessId = pe32.th32ProcessID;
      t.ProcessName = pe32.szExeFile;

      Processes->push_back(t);
   } while (Process32Next (hProcessSnap, &pe32));

   // Don't forget to clean up the snapshot object!
   CloseHandle (hProcessSnap);
   return TRUE;
}

BOOL KillProcess(DWORD ProcessId)
{
   Setzrivilege(SE_DEBUG_NAME);
   HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessId);
   if (hProcess)
   {
      BOOL Result = TerminateProcess(hProcess, 0);
      CloseHandle(hProcess);
      hProcess = NULL;
      return Result;
   }
   return FALSE;
}

感谢向原创作者打赏!


Copyright © 2015 Zhang Luduo.

All rights reserved.