C說話完成在windows辦事中新建過程的辦法。本站提示廣大學習愛好者:(C說話完成在windows辦事中新建過程的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C說話完成在windows辦事中新建過程的辦法正文
本文實例講述了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說話法式設計有所贊助。