c 如何开发看门狗软件

c 如何开发看门狗软件

首页维修大全综合更新时间:2023-06-23 02:25:43

c 如何开发看门狗软件

1、注册服务

2、检测指定进程是否运行,如果没有运行,则运行进程

// WatchDogSvr.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include "Windows.h"

#include "tlhelp32.h"

SERVICE_STATUS m_ServiceStatus;

SERVICE_STATUS_HANDLE m_ServiceStatusHandle;

BOOL bRunning=true;

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);

void WINAPI ServiceCtrlHandler(DWORD Opcode);

BOOL InstallService();

BOOL DeleteService();

const int nBufSize=512;

void WriteLog(const char* strLog)

{

FILE * fp;

fp=fopen("C:\WatchDog.log","a");

if( fp==NULL ) return;

//fputs(strLog,fp);

fprintf(fp,"%s ",strLog);

fclose(fp);

}

int GetProcessCount(const TCHAR* szExeName)

{

TCHAR sztarget[MAX_PATH];

lstrcpy(sztarget, szExeName);

CharLowerBuff(sztarget, MAX_PATH);

int count = 0;

PROCESSENTRY32 my;

HANDLE l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (((int)l) != -1)

{

my.dwSize = sizeof(my);

if (Process32First(l, &my))

{

do

{

CharLowerBuff(my.szExeFile, MAX_PATH);

if (lstrcmp(sztarget, my.szExeFile) == 0)

{

count++;

}

}while (Process32Next(l, &my));

}

CloseHandle(l);

}

return count;

}

void RunServiceProcess(const TCHAR *strServiceName)

{

//清空结构

STARTUPINFO sInfo;

PROCESS_INFORMATION pInfo;

ZeroMemory(&sInfo,sizeof(sInfo));

sInfo.cb=sizeof(sInfo);

sInfo.dwFlags=STARTF_USESHOWWINDOW;

sInfo.wShowWindow=SW_SHOWNORMAL;

ZeroMemory(&pInfo,sizeof(pInfo));

//输出出错信息

char chBuf[nBufSize];

ZeroMemory(chBuf,nBufSize);

//创建一个进程

if(!::CreateProcess(strServiceName,NULL,

NULL,

NULL,

FALSE,

0,

NULL,

NULL,

&sInfo,

&pInfo))

{

sprintf(chBuf,"CreateProcess failed(%d). ",GetLastError());

WriteLog(chBuf);

return;

}

// Wait until child process exits.

WaitForSingleObject(pInfo.hProcess,INFINITE);

//关闭进程和线程的句柄

CloseHandle(pInfo.hProcess);

CloseHandle(pInfo.hThread);

}

void DoTask()

{

if( GetProcessCount(_T("SjkDataCollect.exe"))==0)

{

RunServiceProcess(_T("C:\xxx.exe"));

WriteLog("can not find process [SjkDataCollect.exe] and run process!");

}

}

int _tmain(int argc, _TCHAR* argv[])

{

if(argc>1)

{

if( wcscmp(argv[1],L"-i")==0)

{

if(InstallService())

printf(" Service Installed Sucessfully ");

else

printf(" Service has been installed ");

}

else if(wcscmp(argv[1],L"-d")==0)

{

if(DeleteService())

printf(" Service UnInstalled Sucessfully ");

else

printf(" Installed Easin Central Service Not Found ");

}

else

{

printf(" Unknown Switch Usage For Install use Servicetest -i For UnInstall use Servicetest -d ");

}

}

else

{

SERVICE_TABLE_ENTRY DispatchTable[]={{L"WatchDogSvr",ServiceMain},{NULL,NULL}};

StartServiceCtrlDispatcher(DispatchTable);

}

return 0;

}

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)

{

// DWORD status;

// DWORD specificError;

m_ServiceStatus.dwServiceType = SERVICE_WIN32;

m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;

m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;

m_ServiceStatus.dwWin32ExitCode = 0;

m_ServiceStatus.dwServiceSpecificExitCode = 0;

m_ServiceStatus.dwCheckPoint = 0;

m_ServiceStatus.dwWaitHint = 0;

m_ServiceStatusHandle = RegisterServiceCtrlHandler(L"WatchDogSvr",ServiceCtrlHandler);

if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)

{

return;

}

m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;

m_ServiceStatus.dwCheckPoint = 0;

m_ServiceStatus.dwWaitHint = 0;

if (!SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus))

{

}

bRunning=true;

while(bRunning)

{

Sleep(3000);

//Place Your Code for processing here....

DoTask();

}

return;

}

void WINAPI ServiceCtrlHandler(DWORD Opcode)

{

switch(Opcode)

{

case SERVICE_CONTROL_PAUSE:

m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;

break;

case SERVICE_CONTROL_CONTINUE:

m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;

break;

case SERVICE_CONTROL_STOP:

m_ServiceStatus.dwWin32ExitCode = 0;

m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;

m_ServiceStatus.dwCheckPoint = 0;

m_ServiceStatus.dwWaitHint = 0;

SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);

bRunning=false;

break;

case SERVICE_CONTROL_INTERROGATE:

break;

}

return;

}

BOOL InstallService()

{

WCHAR strDir[1024];

SC_HANDLE schSCManager,schService;

GetCurrentDirectory(1024,strDir);

lstrcat(strDi1、注册服务

2、检测指定进程是否运行,如果没有运行,则运行进程

// WatchDogSvr.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include "Windows.h"

#include "tlhelp32.h"

SERVICE_STATUS m_ServiceStatus;

SERVICE_STATUS_HANDLE m_ServiceStatusHandle;

BOOL bRunning=true;

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);

void WINAPI ServiceCtrlHandler(DWORD Opcode);

BOOL InstallService();

BOOL DeleteService();

const int nBufSize=512;

void WriteLog(const char* strLog)

{

FILE * fp;

fp=fopen("C:\WatchDog.log","a");

if( fp==NULL ) return;

//fputs(strLog,fp);

fprintf(fp,"%s ",strLog);

fclose(fp);

}

int GetProcessCount(const TCHAR* szExeName)

{

TCHAR sztarget[MAX_PATH];

lstrcpy(sztarget, szExeName);

CharLowerBuff(sztarget, MAX_PATH);

int count = 0;

PROCESSENTRY32 my;

HANDLE l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (((int)l) != -1)

{

my.dwSize = sizeof(my);

if (Process32First(l, &my))

{

do

{

CharLowerBuff(my.szExeFile, MAX_PATH);

if (lstrcmp(sztarget, my.szExeFile) == 0)

{

count++;

}

}while (Process32Next(l, &my));

}

CloseHandle(l);

}

return count;

}

void RunServiceProcess(const TCHAR *strServiceName)

{

//清空结构

STARTUPINFO sInfo;

PROCESS_INFORMATION pInfo;

ZeroMemory(&sInfo,sizeof(sInfo));

sInfo.cb=sizeof(sInfo);

sInfo.dwFlags=STARTF_USESHOWWINDOW;

sInfo.wShowWindow=SW_SHOWNORMAL;

ZeroMemory(&pInfo,sizeof(pInfo));

//输出出错信息

char chBuf[nBufSize];

ZeroMemory(chBuf,nBufSize);

//创建一个进程

if(!::CreateProcess(strServiceName,NULL,

NULL,

NULL,

FALSE,

0,

NULL,

NULL,

&sInfo,

&pInfo))

{

sprintf(chBuf,"CreateProcess failed(%d). ",GetLastError());

WriteLog(chBuf);

return;

}

// Wait until child process exits.

WaitForSingleObject(pInfo.hProcess,INFINITE);

//关闭进程和线程的句柄

CloseHandle(pInfo.hProcess);

CloseHandle(pInfo.hThread);

}

void DoTask()

{

if( GetProcessCount(_T("SjkDataCollect.exe"))==0)

{

RunServiceProcess(_T("C:\xxx.exe"));

WriteLog("can not find process [SjkDataCollect.exe] and run process!");

}

}

int _tmain(int argc, _TCHAR* argv[])

{

if(argc>1)

{

if( wcscmp(argv[1],L"-i")==0)

{

if(InstallService())

printf(" Service Installed Sucessfully ");

else

printf(" Service has been installed ");

}

else if(wcscmp(argv[1],L"-d")==0)

{

if(DeleteService())

printf(" Service UnInstalled Sucessfully ");

else

printf(" Installed Easin Central Service Not Found ");

}

else

{

printf(" Unknown Switch Usage For Install use Servicetest -i For UnInstall use Servicetest -d ");

}

}

else

{

SERVICE_TABLE_ENTRY DispatchTable[]={{L"WatchDogSvr",ServiceMain},{NULL,NULL}};

StartServiceCtrlDispatcher(DispatchTable);

}

return 0;

}

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)

{

// DWORD status;

// DWORD specificError;

m_ServiceStatus.dwServiceType = SERVICE_WIN32;

m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;

m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;

m_ServiceStatus.dwWin32ExitCode = 0;

m_ServiceStatus.dwServiceSpecificExitCode = 0;

m_ServiceStatus.dwCheckPoint = 0;

m_ServiceStatus.dwWaitHint = 0;

m_ServiceStatusHandle = RegisterServiceCtrlHandler(L"WatchDogSvr",ServiceCtrlHandler);

if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)

{

return;

}

m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;

m_ServiceStatus.dwCheckPoint = 0;

m_ServiceStatus.dwWaitHint = 0;

if (!SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus))

{

}

bRunning=true;

while(bRunning)

{

Sleep(3000);

//Place Your Code for processing here....

DoTask();

}

return;

}

void WINAPI ServiceCtrlHandler(DWORD Opcode)

{

switch(Opcode)

{

case SERVICE_CONTROL_PAUSE:

m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;

break;

case SERVICE_CONTROL_CONTINUE:

m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;

break;

case SERVICE_CONTROL_STOP:

m_ServiceStatus.dwWin32ExitCode = 0;

m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;

m_ServiceStatus.dwCheckPoint = 0;

m_ServiceStatus.dwWaitHint = 0;

SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);

bRunning=false;

break;

case SERVICE_CONTROL_INTERROGATE:

break;

}

return;

}

BOOL InstallService()

{

WCHAR strDir[1024];

SC_HANDLE schSCManager,schService;

GetCurrentDirectory(1024,strDir);

lstrcat(strDir,L"\WatchDogSvr.exe");

schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

if (schSCManager == NULL)

return false;r,L"\WatchDogSvr.exe");

schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

if (schSCManager == NULL)

return false;

大家还看了
也许喜欢
更多栏目

© 2021 3dmxku.com,All Rights Reserved.