VC ++ ends the process, killing antivirus software or other unpleasant processes

Source: Internet
Author: User

Void pslist (void)
{
Handle hprocesssnap = NULL;
Processentry32 pe32 = {0 };
Hprocesssnap = createconlhelp32snapshot (th32cs_snapprocess, 0 );
If (hprocesssnap = (handle)-1)
{
Printf ("/ncreatemedilhelp32snapshot () failed: % d", getlasterror ());
Return;
}
Pe32.dwsize = sizeof (processentry32 );
Printf ("/nprocessname processid ");
If (process32first (hprocesssnap, & pe32 ))
{
Char A [5];
Do
{
ITOA (pe32.th32processid, A, 10 );
Printf ("/n %-20 S % d", pe32.szexefile, pe32.th32processid );
}
While (process32next (hprocesssnap, & pe32 ));
}
Else
{
Printf ("/nprocess32firstt () failed: % d", getlasterror ());
}
Closehandle (hprocesssnap );
Return;
}

Bool killps (dword id) // kill a process function
{
Handle hprocess = NULL, hprocesstoken = NULL;
Bool iskilled = false, Bret = false;
_ Try
{

If (! Openprocesstoken (getcurrentprocess (), token_all_access, & hprocesstoken ))
{
Printf ("/nopen current process token failed: % d", getlasterror ());
_ Leave;
}
// Printf ("/nopen current process token OK! ");
If (! Setprivilege (hprocesstoken, se_debug_name, true ))
{
_ Leave;
}
Printf ("/nsetprivilege OK! ");

If (hprocess = OpenProcess (process_all_access, false, ID) = NULL)
{
Printf ("/nopen process % d failed: % d", ID, getlasterror ());
_ Leave;
}
// Printf ("/nopen process % d OK! ", ID );
If (! Terminateprocess (hprocess, 1 ))
{
Printf ("/nterminateprocess failed: % d", getlasterror ());
_ Leave;
}
Iskilled = true;
}
_ Finally
{
If (hprocesstoken! = NULL) closehandle (hprocesstoken );
If (hprocess! = NULL) closehandle (hprocess );
}
Return (iskilled );
}
 
Bool setprivilege (handle htoken, lpctstr lpszprivilege, bool benableprivilege) // escalate Permissions
{
Token_privileges TP;
Luid;

If (! Lookupprivilegevalue (null, lpszprivilege, & luid ))
{
Printf ("/nlookupprivilegevalue error: % d", getlasterror ());
Return false;
}
TP. privilegecount = 1;
TP. Privileges [0]. luid = luid;
If (benableprivilege)
TP. Privileges [0]. Attributes = se_privilege_enabled;
Else
TP. Privileges [0]. Attributes = 0;
Adjusttokenprivileges (
Htoken,
False,
& TP,
Sizeof (token_privileges ),
(Ptoken_privileges) null,
(Pdword) null );
If (getlasterror ()! = Error_success)
{
Printf ("adjusttokenprivileges failed: % u/N", getlasterror ());
Return false;
}
Return true;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.