欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

C语言实现在windows服务中新建进程的方法

时间:2021-08-08 08:26:02|栏目:C代码|点击:

本文实例讲述了C语言实现在windows服务中新建进程的方法。分享给大家供大家参考。具体如下:

运行环境:visual stdio 2008

文件名: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); //由于我自己在服务里建了一个进程,所以当服务停止时需要将建的进程也停掉 
    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); //注册控制响应程序 
  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 ; 
  } 
  // 接下来可以做你要做的事了,我这里新建了一个进程 
  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, //在服务运行后新建了一个进程,实际的工作都由新建的进程完成 
   szCommandLine, 
   NULL, 
   NULL, 
   FALSE, 
   0, 
   NULL, 
   cwd, //这个参数必不可少,而且要是绝对路径,不然服务会找不到创建新进程所需文件所在目录 
   &si, 
   &pi)) 
  { 
   sprintf(buf, "CreateProcess failed (%d).\n", GetLastError()); 
   WriteToLog(buf); 
  } 
  else 
  { 
   // 不使用的句柄最好关掉 
   //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);
  //注意:CmdStart函数
  return 0; 
} 

使用方法:

安装服务:sc create testService binpath= c:\testService.exe

删除服务: sc delete testService

希望本文所述对大家的C语言程序设计有所帮助。

上一篇:如何利用C语言实现最简单的HTTP服务器详解

栏    目:C代码

下一篇:通过先序遍历和中序遍历后的序列还原二叉树(实现方法)

本文标题:C语言实现在windows服务中新建进程的方法

本文地址:http://www.codeinn.net/misctech/165205.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有