The example in this article describes how the C language implementation creates a new process in a Windows service. Share to everyone for your reference. Specifically as follows:
Operating environment: Visual Stdio 2008
FileName: testservice.c
#include <windows.h> #include <stdio.h> #include <time.h> #include <tchar.h> HANDLE Hmutex;
Service_status ServiceStatus;
Service_status_handle Servicestatushandle;
Process_information Pi; Service control Handler Function void WINAPI Cmdcontrol (DWORD dwcode) {switch (dwcode) {case Service_control
_pause:servicestatus.dwcurrentstate = service_paused;
Break
Case SERVICE_CONTROL_CONTINUE:ServiceStatus.dwCurrentState = service_running;
Break
Case Service_control_stop:waitforsingleobject (Hmutex,infinite); if (WaitForSingleObject (pi.hprocess, 5000)!= wait_object_0) terminateprocess (pi.hprocess,0);
Because I have built a process in the service, it is necessary to stop the process when the service is stopped servicestatus.dwcurrentstate = service_stopped;
Servicestatus.dwwin32exitcode = 0;
Servicestatus.dwcheckpoint = 0;
Servicestatus.dwwaithint = 0; if (SetServiceStatus (servicestatushandle,&servicestatus) ==0) {printf ("setservicesTatus in Cmdcontrol in Switch Error!\n ");
} return;
Case Service_control_interrogate:break;
Default:break; } if (SetServiceStatus (servicestatushandle,&servicestatus) ==0) {printf ("SetServiceStatus in Cmdcontrol out
Switch Error!\n ");
} return;
int WriteToLog (char* str) {file* log;
Log = fopen ("Dma_ws.log", "A +");
if (log = = NULL) return-1;
fprintf (log, "%s\n", str);
fclose (log);
return 0; //service servicemain Function void WINAPI Cmdstart (DWORD dwargc,lptstr *lpargv) {Servicestatus.dwservicetype
= Service_win32;
Servicestatus.dwcurrentstate = service_start_pending; servicestatus.dwcontrolsaccepted = Service_accept_stop |
Service_accept_pause_continue;
Servicestatus.dwservicespecificexitcode = 0;
Servicestatus.dwwin32exitcode = 0;
Servicestatus.dwcheckpoint = 0;
Servicestatus.dwwaithint = 0; Servicestatushandle=registerservicectrlhandler (TEXT ("Ntkrnl"), Cmdcontrol);
Registration Control Responder if (Servicestatushandle = 0) {printf ("RegisterServiceCtrlHandler Error!\n");
return;
} servicestatus.dwcurrentstate = service_running;
Servicestatus.dwcheckpoint = 0;
Servicestatus.dwwaithint = 0; if (SetServiceStatus (servicestatushandle,&servicestatus) ==0) {printf ("SetServiceStatus in Cmdstart Error!\n")
;
return;
//Next you can do what you want to do, I have a new process here startupinfo si;
ZeroMemory (&si, sizeof (SI));
SI.CB = sizeof (SI);
Si.wshowwindow = true;
ZeroMemory (&pi, sizeof (PI));
Char buf[100] ={0}; TCHAR szcommandline[] = TEXT ("C:\\Program Files (x86) \\IronPython 2.7.1\\ipy.exe-c:\\dxmonitorsystem\\dxhttpserver.py
");
TCHAR cwd[] = TEXT ("C:\\dxmonitorsystem"); if (!
CreateProcess (NULL,//Create a new process after the service runs, the actual work is done by the newly created process szcommandline, NULL, NULL, FALSE, 0, NULL, CWD,//This parameter is necessary, and if the absolute path, otherwise the service will not find the file that creates the new process the directory &si, &pi)) {sprintf (buf, CreAteprocess failed (%d). \ n ", GetLastError ());
WriteToLog (BUF);
The else {//unused handle is best to turn off//closehandle (Pi.hthread);
CloseHandle (pi.hprocess);
sprintf (BUF, "New process id:%d\n", pi.dwprocessid);
sprintf (Buf+strlen (BUF), "New process Master Thread id:%d\n", pi.dwthreadid);
WriteToLog (BUF);
} writetolog ("hello,world\n");
return;
int main () {service_table_entry dispatchtable[] = {{TEXT ("NTKRNL"), Cmdstart}, {null, NULL}};
StartServiceCtrlDispatcher (dispatchtable);
Note: Cmdstart function return 0;
}
How to use:
Installation Services: SC create testservice binpath= c:\testService.exe
Delete Service: SC delete testservice
I hope this article will help you with the C language program.