first, attach the Source Code of the Windows creation service. This is very useful and is usually used by everyone.
# Include "XXXX. H "// included header file // defines the global function Variable Void Init (); bool isinstalled (); bool install (); bool uninstall (); void logevent (lpctstr pszformat ,...); void winapi servicemain (); void winapi servicestrl (DWORD dwopcode); tchar szservicename [] = _ T ("Watchdog"); bool binstall; service_status_handle hservicestatus; service_status status; DWORD dwthreadid; int apientry winmain (hinstance, hinstance hprevinstance, LPS Tr lpcmdline, int ncmdshow) {Init (); dwthreadid =: getcurrentthreadid (); service_table_entry st [] ={{ szservicename, (lpservice_main_function) servicemain}, {null, null }}; if (stricmp (lpcmdline, "/install") = 0) {install ();} else if (stricmp (lpcmdline, "/uninstall ") = 0) {uninstall ();} else {If (!: Startservicectrldispatcher (ST) {logevent (_ T ("Register Service Main Function error! ") ;}} Return 0 ;} //************************************** * ****************** // functiopn: init // Description: initialization // CILS: main // <author> niying <time> 2006-8-10 <version> <DESC> //********************* *********************************** void Init () {hservicestatus = NULL; status. dwservicetype = service_win32_own_process | service_interactive_process; status. dwcurrentstate = service_start_pending; status. dwcont Rolsaccepted = service_accept_stop; status. dwwin32exitcode = 0; status. dwservicespecificexitcode = 0; status. dwcheckpoint = 0; status. dwwaithint = 0 ;} //************************************** * ****************** // functiopn: servicemain // Description: Service main function, control the registration of Service Control in/<author> niying <time> 2006-8-10 <version> <DESC> //************ **************************************** ***** void winapi servicemain () {// Register the control request handler status. dwcurrentstate = service_start_pending; status. dwcontrolsaccepted = service_accept_stop; // Register Service Control hservicestatus = registerservicectrlhandler (szservicename, servicestrl); If (hservicestatus = NULL) {logevent (_ T ("handler not installed ")); return;} setservicestatus (hservicestatus, & status); status. dwwin32exitcode = s_ OK; status. dwcheckpoint = 0; Status. Dwwaithint = 0; status. dwcurrentstate = service_running; setservicestatus (hservicestatus, & status); // simulate the running of the service. Put the main task here for the application. // you can enter the task to be executed by the Service. Code Generally, it is an endless loop while (1) {// loop} status. dwcurrentstate = service_stopped; setservicestatus (hservicestatus, & status); outputdebugstring (_ T ("Service stopped "));} //************************************** * ****************** // functiopn: servicestrl // Description: The main function of Service Control, which controls the service. // when the service manager stops or performs other operations, the code here is run. // cballs: // called by: // table accessed: // table updated: // input: dwopcode: Control Service Status // output: // return: // others: // History: // <author> niying <time> 2006-8-10 <version> <DESC> //********************** ********************************** void winapi servicestrl (DWORD dwopcode) {Switch (dwopcode) {Case service_control_stop: status. dwcurrentstate = service_stop_pending; setservicestatus (hservicestatus, & status); postthreadmessage (dwthreadid, wm_close, 0, 0); break; Case service_control_pause: break; Case service_contro L_continue: break; Case service_control_interrogate: break; Case service_control_shutdown: break; default: logevent (_ T ("bad service request ")); outputdebugstring (_ T ("bad service request "));}} //************************************** * ****************** // functiopn: isinstalled // description: determine whether the service has been installed. // <author> niying <time> 2006-8-10 <version> <DESC> //**************** ************************************* * *** Bool isinstalled () {bool bresult = false; // open the Service Control Manager SC _handle hscm =:: openscmanager (null, null, SC _manager_all_access); If (hscm! = NULL) {// open the service SC _handle hservice =: openservice (hscm, szservicename, service_query_config); If (hservice! = NULL) {bresult = true;: closeservicehandle (hservice); }:: closeservicehandle (hscm);} return bresult ;} //************************************** * ****************** // functiopn: install // description: install the service function // <author> niying <time> 2006-8-10 <version> <DESC> //******************* * ************************************ bool install () {If (isinstalled () return true; // open the Service Control Manager SC _handle hscm =: openscmanager (Null, null, SC _manager_all_access); If (hscm = NULL) {MessageBox (null, _ T ("couldn't open service manager"), szservicename, mb_ OK ); return false;} // get the executable file path tchar szfilepath [max_path];: getmodulefilename (null, szfilepath, max_path); // create the service SC _handle hservice = :: createservice (hscm, szservicename, szservicename, service_all_access, service_win32_own_process | service_interactiv E_process, service_auto_start, service_error_normal, szfilepath, null, null, _ T (""), null, null); If (hservice = NULL) {: closeservicehandle (hscm ); messageBox (null, _ T ("couldn't create service"), szservicename, mb_ OK); Return false ;}:: closeservicehandle (hservice);: closeservicehandle (hscm ); return true ;} //************************************** * ****************** // functiopn: uninstall // descrip Tion: delete A service function // <author> niying <time> 2006-8-10 <version> <DESC> //******************* * ************************************ bool uninstall () {If (! Isinstalled () return true; SC _handle hscm =: openscmanager (null, null, SC _manager_all_access); If (hscm = NULL) {MessageBox (null, _ T ("couldn't open service manager"), szservicename, mb_ OK); Return false;} SC _handle hservice =: openservice (hscm, szservicename, service_stop | delete ); if (hservice = NULL) {: closeservicehandle (hscm); MessageBox (null, _ T ("couldn't open service"), szservicename, Mb_ OK); Return false;} service_status status;: controlservice (hservice, service_control_stop, & status); // Delete the service bool bdelete =: deleteservice (hservice );:: closeservicehandle (hservice);: closeservicehandle (hscm); If (bdelete) return true; logevent (_ T ("service cocould not be deleted"); Return false ;} //************************************** * ****************** // functiopn: logevent // Description: Record Service events <Author> niying <time> 2006-8-10 <version> <DESC> //********************* *********************************** void logevent (lpctstr pformat ,...) {tchar chmsg [256]; handle heventsource; lptstr lpszstrings [1]; va_list parg; va_start (parg, pformat); _ vstprintf (chmsg, pformat, parg ); va_end (parg); lpszstrings [0] = chmsg; heventsource = registereventsource (null, szservicename); If (heventsource! = NULL) {reportevent (heventsource, eventlog_information_type, 0, 0, null, 1, 0, (lpctstr *) & lpszstrings [0], null); deregistereventsource (heventsource );}}
I used the functions in the DLL during development and used the static import method at the beginning. However, I found that the service will die after it is started. This is not a dll path problem.
No way, you have to use the dynamic import method. Fortunately, the service is automatically started and running, and it is still normal.
TIPS: If you need to bring up the MessageBox message, for example, to allow the Service to interact with the desktop. This can be set in the service.
Of course, I have set the initialization parameters here. After the installation is completed in the DOS command, you will find that the box has been selected for you.
// Important Note: it is best not to compile and execute the service directly. Although it is exe, the best way is to install it,
Install servicename.exe/install
Uninstall servicename.exe in DOS/Uninstall
Running EXE directly will not only report an error, but also cause the service to fail to run automatically when it is started.
Most of the time, if you run the EXE directly, the error 1063 is reported because you run the service in the console.