Register a program as a system service

Source: Internet
Author: User
// Register and start/stop the service
Bool registerserver (maid, maid)
{
SC _handle hscmanager = openscmanager (0, 0, SC _manager_create_service );
If (hscmanager = NULL)
{
Return false;
}

If (createservice (hscmanager, lpservicename, lpdisplayname,
Service_all_access, service_win32_own_process, service_demand_start,
Service_error_normal, lpbinarypathname, null, null ))
{
Printf ("registerserver.../R/N ");

Closeservicehandle (hscmanager );
Return true;
}
Else
{
Printf ("registerserver error code = % d/R/N", getlasterror ());

Closeservicehandle (hscmanager );
Return false;
}
}

Bool executeservice (lpctstr lpservicename)
{
SC _handle hscmanager, hservice;
Hscmanager = openscmanager (0, 0, SC _manager_all_access );
If (hscmanager = NULL)
{
Return false;
}

Hservice = openservice (hscmanager, lpservicename, service_all_access );
If (hservice = NULL)
{
Closeservicehandle (hscmanager );
Return false;
}

If (startservice (hservice, 0, null ))
{
Printf ("startservice.../R/N ");

Closeservicehandle (hservice );
Closeservicehandle (hscmanager );
Return true;
}
Else
{
Printf ("startservice error code = % d/R/N", getlasterror ());

Closeservicehandle (hservice );
Closeservicehandle (hscmanager );
Return false;
}
}

Bool terminateservice (lpctstr lpservicename)
{
SC _handle hscmanager, hservice;
Hscmanager = openscmanager (0, 0, SC _manager_all_access );
If (hscmanager = NULL)
{
Return false;
}

Hservice = openservice (hscmanager, lpservicename, service_all_access );
If (hservice = NULL)
{
Closeservicehandle (hscmanager );
Return false;
}

If (startservice (hservice, 0, null ))
{
Service_status serverstatus;
Controlservice (hservice, service_control_stop, & serverstatus );

Printf ("terminateservice.../R/N ");

Closeservicehandle (hservice );
Closeservicehandle (hscmanager );
Return true;
}
Else
{
Printf ("terminateservice error code = % d/R/N", getlasterror ());

Closeservicehandle (hservice );
Closeservicehandle (hscmanager );
Return false;
}
}

//////////////////////////////////////// ////////////////////////
// The EXE to be made into a service must be written in this way

# Include <stdio. h>
# Include <windows. h>
# Include <winsvc. h>

# Define servername "enochserver"

Void winapi servicemain (DWORD argc, lptstr * argv );
Void winapi servicecontrol (DWORD ncontrolcode );
Bool executeservice ();
Void terminateservice ();
DWORD winapi serviceproc (lpvoid lpparameter );
Bool updateservicestatus (DWORD dwcurrentstate, DWORD dwwin32exitcode,
DWORD dwservicespecificexitcode, DWORD dwcheckpoint, DWORD dwwaithint );

Service_status_handle hservicestatus;
DWORD servicecurrentstatus;
Handle hservicethread;
Bool bservicerunning;
Handle hserviceevent;

Void main (INT argc, char * argv [])
{
Service_table_entry servicetable [] =
{
{Servername, (lpservice_main_function) servicemain },
{Null, null}
};

If (! Startservicectrldispatcher (servicetable ))
{
Printf ("registerserver first ");
}
}

Void winapi servicemain (DWORD argc, lptstr * argv)
{
Hservicestatus = registerservicectrlhandler (servername, (lphandler_function) servicecontrol );
If (! Hservicestatus |! Updateservicestatus (service_start_pending, no_error, 3000 ))
{
Return;
}

Hserviceevent = createevent (0, true, false, 0 );
If (! Hserviceevent |! Updateservicestatus (service_start_pending, no_error, 1000 ))
{
Return;
}

If (! Executeservice ())
{
Return;
}

Servicecurrentstatus = service_running;

If (! Updateservicestatus (service_running, no_error, 0, 0 ))
{
Return;
}

Waitforsingleobject (hserviceevent, infinite );
Closehandle (hserviceevent );
}

Void winapi servicecontrol (DWORD dwcontrolcode)
{
Switch (dwcontrolcode)
{
Case service_control_shutdown:
Case service_control_stop:
Servicecurrentstatus = service_stop_pending;
Updateservicestatus (service_stop_pending, no_error, 0,1, 3000 );
Terminateservice ();
Return;
Default:
Break;
}

Updateservicestatus (servicecurrentstatus, no_error, 0, 0 );
}

Bool executeservice ()
{
DWORD dwthreadid;
Hservicethread = createthread (0, 0, (lpthread_start_routine) serviceproc, 0, 0, & dwthreadid );

If (hservicethread! = NULL)
{
Bservicerunning = true;
Return true;
}
Else
{
Return false;
}
}

Void terminateservice ()
{
Bservicerunning = false;
Setevent (hserviceevent );
Updateservicestatus (service_stopped, no_error, 0, 0 );
}

DWORD winapi serviceproc (lpvoid lpparameter) // Add the operation you want in this function
{
While (bservicerunning)
{
Beep (450,150 );
Sleep (4000 );
}

Return 0;
}

Bool updateservicestatus (DWORD dwcurrentstate, DWORD dwwin32exitcode,
DWORD dwservicespecificexitcode, DWORD dwcheckpoint, DWORD dwwaithint)
{
Service_status servicestatus;
Servicestatus. dwservicetype = service_win32_own_process;
Servicestatus. dwcurrentstate = dwcurrentstate;

If (dwcurrentstate = service_start_pending)
{
Servicestatus. dwcontrolsaccepted = 0;
}
Else
{
Servicestatus. dwcontrolsaccepted = service_accept_stop | service_accept_shutdown;
}

If (dwservicespecificexitcode = 0)
{
Servicestatus. dwwin32exitcode = dwwin32exitcode;
}
Else
{
Servicestatus. dwwin32exitcode = error_service_specific_error;
}

Servicestatus. dwservicespecificexitcode = dwservicespecificexitcode;
Servicestatus. dwcheckpoint = dwcheckpoint;
Servicestatus. dwwaithint = dwwaithint;

If (! Setservicestatus (hservicestatus, & servicestatus ))
{
Terminateservice ();
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.