c语⾔创建windows进程,C语⾔实现在windows服务中新建进程
的⽅法
本⽂实例讲述了C语⾔实现在windows服务中新建进程的⽅法。分享给⼤家供⼤家参考。具体如下:
createprocessa运⾏环境:visual stdio 2008
⽂件名:testService.c
#include
#include
#include
#include
HANDLE hMutex;
SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE ServiceStatusHandle;
PROCESS_INFORMATION pi;
//Service Control Handler Function
void WINAPI CmdControl(DWORD dwCode)
{undefined
switch(dwCode)
{undefined
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); //由于我⾃⼰在服务⾥建了⼀个进程,所以当服务停⽌时需要将建的进程也停掉
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
if(SetServiceStatus(ServiceStatusHandle,&ServiceStatus)==0)
{undefined
printf("SetServiceStatus in CmdControl in Switch Error !\n");
}
return ;
case SERVICE_CONTROL_INTERROGATE:
break;
default:
break;
}
if(SetServiceStatus(ServiceStatusHandle,&ServiceStatus)==0) {undefined
printf("SetServiceStatus in CmdControl out Switch Error !\n"); }
return ;
}
int WriteToLog(char* str)
{undefined
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) {undefined
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); //注册控制响应程序if (ServiceStatusHandle == 0)
{undefined
printf("RegisterServiceCtrlHandler Error !\n");
return ;
}
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
if(SetServiceStatus(ServiceStatusHandle,&ServiceStatus)==0)
{undefined
printf("SetServiceStatus in CmdStart Error !\n");
return ;
}
// 接下来可以做你要做的事了,我这⾥新建了⼀个进程
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\\
C:\\DXMonitorSystem\\DXHttpServer.py");
TCHAR cwd[] = TEXT("C:\\DXMonitorSystem");
if (!CreateProcess(NULL, //在服务运⾏后新建了⼀个进程,实际的⼯作都由新建的进程完成szCommandLine,
NULL,
NULL,
FALSE,
0,
NULL,
cwd, //这个参数必不可少,⽽且要是绝对路径,不然服务会不到创建新进程所需⽂件所在⽬录
&si,
&pi))
{undefined
sprintf(buf, "CreateProcess failed (%d).\n", GetLastError());
WriteToLog(buf);
}
else
{undefined
// 不使⽤的句柄最好关掉
//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()
{undefined
SERVICE_TABLE_ENTRY DispatchTable[] =
{undefined
{TEXT("ntkrnl"),CmdStart},
{NULL ,NULL }
};
StartServiceCtrlDispatcher(DispatchTable);
//注意:CmdStart函数
return 0;
}
使⽤⽅法:
安装服务:sc create testService binpath= c:\
删除服务: sc delete testService
希望本⽂所述对⼤家的C语⾔程序设计有所帮助。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论