当前位置:   article > 正文

Windows 服务资料不错_cserctrl.writetolog vc

cserctrl.writetolog vc

//转自天漏客,个人主页:http://www.lilu.name

//主函数

//--------------------------------------------------------------------
#include <windows.h>
#include <Shlwapi.h>
#include <stdio.h>
#include "ServiceControl.h"
#pragma comment(lib,"shlwapi")
//--------------------------------------------------------------------
/*
本服务启动后,随机启动。
每次启动时,在程序目录下生成StartTime.txt文件,记录系统的启动时间
可以每天查看日志,就知道自己的电脑是否被开机过。
*/
TCHAR tzServiceName[] = TEXT("StartTimeService");
TCHAR tzServiceDescription[] = TEXT("服务测试程序。随机启动,启动时会将当前时间写入StartTime.txt文件,并将运行日志写入log文件夹。");
SERVICE_STATUS ServiceStatus = {0};
SERVICE_STATUS_HANDLE hStatus = NULL;
HANDLE hEventEnd = NULL;  //服务是否结束事件
//--------------------------------------------------------------------
//声明服务函数
void WINAPI ServiceMain();
void WINAPI ServiceCtrlHandle(DWORD dwOpcode);

DWORD WINAPI ThreadMain(LPVOID pParam);   //线程函数

CServiceControl CSerCtrl(tzServiceName,tzServiceDescription);

//--------------------------------------------------------------------
int main(int argc, TCHAR* argv[])
{
 //设置日志路径,为空或者不调用的话,自动按照日期生成日志名
 //CSerCtrl.SetLogFileName(NULL);
 //CSerCtrl.SetLogFileName(TEXT("log.txt"));

 if (argc == 2)
  {
  //安装
  if (_stricmp(argv[1],TEXT("install")) == 0
   || _stricmp(argv[1],TEXT("/install")) == 0
   || _strcmpi(argv[1],TEXT("-installl")) == 0)
   {
    if (CSerCtrl.Install())
     {
     printf("服务安装成功!");
     }
    else
     {
     printf("服务安装失败!");
     }
   }
  //卸载
  if (_stricmp(argv[1],TEXT("uninstall")) == 0
    || _stricmp(argv[1],TEXT("/uninstall")) == 0
    || _stricmp(argv[1], TEXT("-uninstall")) ==0)
   {
   if (CSerCtrl.UnInstall())
    {
    printf("服务卸载成功!");
    }
     else
    {
    printf("服务卸载失败!");

    }
   }
  //启动
  if (_stricmp(argv[1],TEXT("start")) == 0
    || _stricmp(argv[1],TEXT("/start")) == 0
    || _stricmp(argv[1], TEXT("start")) == 0)
   {
   if (CSerCtrl.Start())
    {
    printf("服务启动成功!");
    }
     else
    {
    printf("服务启动失败!");
    }
   }

  //停止
  if (_stricmp(argv[1],TEXT("stop")) == 0
    || _stricmp(argv[1],TEXT("/stop")) == 0
    || _stricmp(argv[1], TEXT("stop")) == 0)
   {
   if (CSerCtrl.Stop())
    {
    printf("服务停止成功!");
    }
   else
    {
    printf("服务停止失败!");
    }
   }
  }
 else
 {
 //启动服务
 SERVICE_TABLE_ENTRY ServiceTable[] =
  {
   {tzServiceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain},
   {NULL,NULL}
  };

 if (StartServiceCtrlDispatcher(ServiceTable) == false)
  {
  CSerCtrl.WriteToLog("Register Service Main Function Error!",sizeof("Register Service Main Function Error!"));
  CSerCtrl.WriteToLog("StartServiceCtrlDispatcherError!",sizeof("StartServiceCtrlDispatcherError!"));

  LPVOID lpMsgBuf;
        FormatMessage(
                  FORMAT_MESSAGE_ALLOCATE_BUFFER |
                  FORMAT_MESSAGE_FROM_SYSTEM,
                  NULL,
                  GetLastError() ,
                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                  (LPTSTR) &lpMsgBuf,
                  0, NULL );
  CSerCtrl.WriteToLog((char*)lpMsgBuf, strlen((char*)lpMsgBuf)+1);
  LocalFree(lpMsgBuf);
  }
 else
  {
  CSerCtrl.WriteToLog("StartServiceCtrlDispatcher成功!",sizeof("StartServiceCtrlDispatcher成功!"));
  }

 }
 CSerCtrl.WriteToLog("main结束!",sizeof("main结束!"));

 return 0;
}
//--------------------------------------------------------------------
void WINAPI ServiceMain()
{
 HANDLE hThread = NULL;
 DWORD dwThreadID = 0;
 CSerCtrl.WriteToLog("ServiceMain开始!",sizeof("ServiceMain开始!"));
 //DebugBreak();
 //初始化
 ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN;
 ServiceStatus.dwCurrentState  = SERVICE_START_PENDING;
 ServiceStatus.dwServiceType   = SERVICE_WIN32;

 hStatus = RegisterServiceCtrlHandler(tzServiceName, (LPHANDLER_FUNCTION)ServiceCtrlHandle);
 if (hStatus == (SERVICE_STATUS_HANDLE)0)
  {
  CSerCtrl.WriteToLog("RegisterServiceCtrlHandler启动失败!", sizeof("RegisterServiceCtrlHandler启动失败!"));
  return;
  }
 CSerCtrl.WriteToLog("RegisterServiceCtrlHandler成功!", sizeof("RegisterServiceCtrlHandler成功!"));
 //报告正在启动
 SetServiceStatus(hStatus, &ServiceStatus);
 CSerCtrl.WriteToLog("SetServiceStatus成功!", sizeof("SetServiceStatus成功!"));
 //创建一个事件进行同步
 hEventEnd = CreateEvent(NULL,TRUE,FALSE,NULL);
 if (hEventEnd == NULL)
  {
  CSerCtrl.WriteToLog("CreateEvent失败!", sizeof("CreateEvent失败!"));
  return;
  }
 ResetEvent(hEventEnd);
 CSerCtrl.WriteToLog("CreateEvent成功!", sizeof("CreateEvent成功!"));
 //报告启动完毕
 ServiceStatus.dwCurrentState = SERVICE_RUNNING;
 SetServiceStatus(hStatus,&ServiceStatus);

 //暂停30秒,在这一句代码的后面下断点。
 //在这段时间中,使用VC或者VS打开“附加到进程”,找到这个程序“StartTimeService.exe”,确定附加。
 //时间到了,就中断了。
 //如果不调试,请注释掉暂停代码。
 //Sleep(30000);
 //创建运行线程
 hThread = CreateThread(NULL, 0, ThreadMain, NULL, NULL, &dwThreadID);
 if (!hThread)
  {
  SetEvent(hEventEnd);
  }
 CloseHandle(hThread);
 CSerCtrl.WriteToLog("CreateThread成功!", sizeof("CreateThread成功!"));
 ServiceStatus.dwCheckPoint = 0;
 ServiceStatus.dwWaitHint = 0;
 SetServiceStatus(hStatus,&ServiceStatus);

 //等待事件退出
 CSerCtrl.WriteToLog("WaitForSingleObject开始!", sizeof("WaitForSingleObject开始!"));
 WaitForSingleObject(hEventEnd,INFINITE);
 CSerCtrl.WriteToLog("WaitForSingleObject结束!", sizeof("WaitForSingleObject结束!"));
 CSerCtrl.WriteToLog("ExitThread结束!",sizeof("ExitThread结束!"));
 CloseHandle(hEventEnd);
 //报告停止状态
 ServiceStatus.dwCurrentState = SERVICE_STOPPED;
 ServiceStatus.dwCheckPoint  = 0;
 ServiceStatus.dwWaitHint  = 0;
 SetServiceStatus(hStatus,&ServiceStatus);
 CSerCtrl.WriteToLog("ServiceMain结束!",sizeof("ServiceMain结束!"));

}
//--------------------------------------------------------------------
void WINAPI ServiceCtrlHandle(DWORD dwOpcode)
{
 switch (dwOpcode)
  {
  case SERVICE_CONTROL_STOP:
   //开始停止
   ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
   SetServiceStatus(hStatus,&ServiceStatus);
   CSerCtrl.WriteToLog("SERVICE_STOP_PENDING结束!",sizeof("SERVICE_STOP_PENDING结束!"));
   //设置信号
   SetEvent(hEventEnd);
   CSerCtrl.WriteToLog("SetEvent结束!",sizeof("SetEvent结束!"));
   //停止
   ServiceStatus.dwCurrentState = SERVICE_STOPPED;
   SetServiceStatus(hStatus,&ServiceStatus);
   CSerCtrl.WriteToLog("SERVICE_STOPPED结束!",sizeof("SERVICE_STOPPED结束!"));
   break;
  case SERVICE_CONTROL_PAUSE:
   ServiceStatus.dwCurrentState = SERVICE_PAUSE_PENDING;
   SetServiceStatus(hStatus,&ServiceStatus);
   ServiceStatus.dwCurrentState = SERVICE_PAUSED;
   SetServiceStatus(hStatus,&ServiceStatus);
   break;
  case SERVICE_CONTROL_CONTINUE:
   ServiceStatus.dwCurrentState = SERVICE_CONTINUE_PENDING;
   SetServiceStatus(hStatus,&ServiceStatus);
   ServiceStatus.dwCurrentState = SERVICE_RUNNING;
   SetServiceStatus(hStatus,&ServiceStatus);
   break;
  case SERVICE_CONTROL_INTERROGATE: //检索更新状态的时
   break;
  case SERVICE_CONTROL_SHUTDOWN:
   ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
   SetServiceStatus(hStatus,&ServiceStatus);
   //设置信号
   SetEvent(hEventEnd);
   CSerCtrl.WriteToLog("SERVICE_CONTROL_SHUTDOWN结束!",sizeof("SERVICE_CONTROL_SHUTDOWN结束!"));
   break;
  default:
   ServiceStatus.dwCurrentState = dwOpcode;
   SetServiceStatus(hStatus,&ServiceStatus);
   break;
  }
}
//--------------------------------------------------------------------
DWORD WINAPI ThreadMain(LPVOID pParam)
{
 HANDLE hFile = INVALID_HANDLE_VALUE;
 DWORD dwWrittenLen = 0;
 TCHAR tzPathName[MAX_PATH] = {0};
 SYSTEMTIME SystemTime = {0};
 char szTime[50] = {0};

 GetModuleFileName(NULL,tzPathName,MAX_PATH);
 PathRemoveFileSpec(tzPathName);
 PathCombine(tzPathName,tzPathName,TEXT("StartTime.txt"));

 hFile = CreateFile(tzPathName,GENERIC_ALL,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
 if (hFile == INVALID_HANDLE_VALUE)
  {
  CSerCtrl.WriteToLog("CreateFile失败!",sizeof("CreateFile失败!"));
  goto EXIT0;
  }
 CSerCtrl.WriteToLog("CreateFile结束!",sizeof("CreateFile结束!"));
 SetFilePointer(hFile,0,NULL,FILE_END);
 GetSystemTime(&SystemTime);
 sprintf_s(szTime,50,"启动时间: %d-%d-%d %d:%d:%d----",SystemTime.wYear,SystemTime.wMonth,SystemTime.wDay,
 SystemTime.wHour + 8,SystemTime.wMinute,SystemTime.wSecond);
 WriteFile(hFile,szTime,(DWORD)strlen(szTime),&dwWrittenLen,NULL);
 WriteFile(hFile,"/r/n",2,&dwWrittenLen,NULL);

EXIT0:
 CloseHandle(hFile);
 //自己在测试命令行的“start”和“stop”命令时,可以注释下面的一行,这样主线程就不会退出了。
 SetEvent(hEventEnd);
 CSerCtrl.WriteToLog("ThreadMain结束!",sizeof("ThreadMain结束!"));

 Sleep(5000);
 return 0;
}
//--------------------------------------------------------------------

、、、、、

//ControlService.h

//--------------------------------------------------------------------
#ifndef MC_ServiceControlH
#define MC_ServiceControlH
#include <WTypes.h>
//--------------------------------------------------------------------
class CServiceControl
{
public:
 CServiceControl(void);
 CServiceControl(LPCTSTR lpServiceName,LPCTSTR lpServieDescription);
 virtual ~CServiceControl(void);

public:
 
 bool Install   (void);      // 安装服务
 bool Start   (void);      // 启动服务
 bool Stop   (void);      // 停止服务
 bool UnInstall  (void);      // 卸载服务
 DWORD QueryStatus  (void);      // 查询服务状态

 void SetLogFileName (LPCTSTR lpLogFileName); // 设置日志文件名。参数为NULL,则根据日期生成文件名。
 bool WriteToLog  (char* pLogContent,DWORD dwContentSize);  // 写入日志

private:
 TCHAR m_tzServiceName[MAX_PATH];
 TCHAR m_tzLogPathName[MAX_PATH];


 bool IsInstall  (void);      // 判断是否安装了服务

private:
 TCHAR m_tzServieDescription[MAX_PATH];
};
#endif
//-------------------------------------------------------------------

、、、

//ServiceControl.cpp

//--------------------------------------------------------------------
#include <Windows.h>
#include "ServiceControl.h"
#include <stdio.h>
#include <Shlwapi.h>
#pragma comment(lib,"shlwapi")
//--------------------------------------------------------------------
CServiceControl::CServiceControl(void)
{
 memset(m_tzServiceName,0,sizeof(m_tzServiceName));
 memset(m_tzLogPathName,0,sizeof(m_tzLogPathName));
 memset(m_tzServieDescription,0,sizeof(m_tzServieDescription));

 //设置日志路径
 SetLogFileName(NULL);

}
//--------------------------------------------------------------------
CServiceControl::CServiceControl(LPCTSTR lpServiceName,LPCTSTR lpServieDescription)
{
 strcpy_s(m_tzServiceName,MAX_PATH,lpServiceName);
 strcpy_s(m_tzServieDescription,MAX_PATH,lpServieDescription);
 memset(m_tzLogPathName,0,sizeof(m_tzLogPathName));
 //设置日志路径
 SetLogFileName(NULL);

}
//--------------------------------------------------------------------
CServiceControl::~CServiceControl(void)
{
}
//--------------------------------------------------------------------
// 判断是否安装了服务
bool CServiceControl::IsInstall(void)
{
 SC_HANDLE hSCM  = NULL;
 SC_HANDLE hService = NULL;

 hSCM = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
 if (hSCM == NULL)
  {
  WriteToLog("IsInstall--打开服务管理器失败,无法查询服务安装状态!",sizeof("IsInstall--打开服务管理器失败,无法查询服务安装状态!"));
  return false;
  }

 hService = OpenService(hSCM,m_tzServiceName,SERVICE_QUERY_CONFIG);
 if (hService == NULL)
  {
  WriteToLog("IsInstall--查询成功:服务未安装!",sizeof("IsInstall--查询成功:服务未安装!"));
  CloseServiceHandle(hSCM);
  return false;
  }
 else
  {
  WriteToLog("IsInstall--查询成功:服务已安装!",sizeof("IsInstall--查询成功:服务已安装!"));
  }

 CloseServiceHandle(hService);
 CloseServiceHandle(hSCM);
 return true;
}
//--------------------------------------------------------------------
// 安装服务
bool CServiceControl::Install(void)
{
 SC_HANDLE hSCM = NULL;
 SC_HANDLE hService = NULL;
 TCHAR  tzFilePathName[MAX_PATH] = {0};

 if (IsInstall())
  {
  WriteToLog("Install--服务已安装过!",sizeof("Install--服务已安装过!"));
  return true;
  }

 hSCM = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
 if (hSCM == NULL)
  {
  WriteToLog("Install--无法打开服务管理器!",sizeof("Install--无法打开服务管理器!"));
  return false;
  }

 SC_LOCK scLock = LockServiceDatabase(hSCM);

 GetModuleFileName(NULL,tzFilePathName,MAX_PATH);
 hService = CreateService(hSCM,m_tzServiceName,m_tzServiceName,SERVICE_ALL_ACCESS,
 SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,
 tzFilePathName,NULL,NULL,NULL,NULL,NULL);
 if (hService == NULL)
  {
  WriteToLog("Install--无法创建服务!",sizeof("Install--无法创建服务!"));
  CloseServiceHandle(hSCM);
  return false;
  }
 else
  {
  WriteToLog("Install--服务创建成功!",sizeof("Install--服务创建成功!"));
  }

 //修改服务描述
 //hService = OpenService(hSCM,m_tzServiceName,SERVICE_ALL_ACCESS);
 SERVICE_DESCRIPTION ServiceDescription = {0};
 ServiceDescription.lpDescription = m_tzServieDescription;

 if (0 == ChangeServiceConfig2(hService,SERVICE_CONFIG_DESCRIPTION,&ServiceDescription))
  {
  WriteToLog("ChangeServiceConfig2--更改服务描述失败!",sizeof("ChangeServiceConfig2--更改服务描述失败!"));
  }

 CloseServiceHandle(hService);

 UnlockServiceDatabase(scLock);
 CloseServiceHandle(hSCM);
 return true;
}
//--------------------------------------------------------------------
// 启动服务
bool CServiceControl::Start(void)
{
 SC_HANDLE hSCM = NULL;
 SC_HANDLE hService = NULL;
 SERVICE_STATUS ServiceStatus = {0};
 DWORD  dwWaitTime = 0;


 //打开服务
 hSCM = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
 if (hSCM == NULL)
  {
  WriteToLog("Start--无法打开服务管理器!",sizeof("Start--无法打开服务管理器!"));
  return false;
  }
 hService = OpenService(hSCM,m_tzServiceName,SERVICE_QUERY_STATUS|SERVICE_START);
 if (hService == NULL)
  {
  WriteToLog("Start--打开服务失败!",sizeof("Start--打开服务失败!"));
  CloseServiceHandle(hSCM);
  return false;
  }

 //查询是否在运行
 QueryServiceStatus(hService,&ServiceStatus);
 if (ServiceStatus.dwCurrentState == SERVICE_RUNNING
  || ServiceStatus.dwCurrentState == SERVICE_START_PENDING)
  {
  WriteToLog("Start--服务已经在运行,无需启动!",sizeof("Start--服务已经在运行,无需启动!"));
  CloseServiceHandle(hService);
  CloseServiceHandle(hSCM);
  return true;
  }

 //启动服务
 if (!StartService(hService,0,NULL))
  {
  WriteToLog("Start--启动服务失败!(StartService)",sizeof("Start--启动服务失败!(StartService)"));
  CloseServiceHandle(hService);
  CloseServiceHandle(hSCM);
  return false;
  }

 //查询状态,是否已经启动完毕
 QueryServiceStatus(hService,&ServiceStatus);
 dwWaitTime = ServiceStatus.dwWaitHint / 10;
 if (dwWaitTime < 1000)
  {
  dwWaitTime = 1000;
  }
 else if(dwWaitTime > 10000)
  {
  dwWaitTime = 10000;
  }

 while(ServiceStatus.dwCurrentState == SERVICE_START_PENDING)
  {
  Sleep(dwWaitTime);

  QueryServiceStatus(hService,&ServiceStatus);
  }

 if (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
  {
  WriteToLog("Start--服务正在运行!",sizeof("Start--服务正在运行!"));
  }
 else
  {
  WriteToLog("Start--启动服务失败!",sizeof("Start--启动服务失败!"));
  CloseServiceHandle(hService);
  CloseServiceHandle(hSCM);
  return false;
  }
 CloseServiceHandle(hService);
 CloseServiceHandle(hSCM);
 return true;
}
//--------------------------------------------------------------------
// 停止服务
bool CServiceControl::Stop(void)
{
 SC_HANDLE hSCM = NULL;
 SC_HANDLE hService = NULL;
 SERVICE_STATUS ServiceStatus = {0};
 DWORD  dwWaitTime = 0;

 hSCM = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
 if (hSCM == NULL)
  {
  WriteToLog("Stop--无法打开服务管理器!",sizeof("Stop--无法打开服务管理器!"));
  return false;
  }
 hService = OpenService(hSCM,m_tzServiceName,SERVICE_QUERY_STATUS|SERVICE_STOP);
 if (hService == NULL)
  {
  WriteToLog("Stop--打开服务失败!",sizeof("Stop--打开服务失败!"));
  CloseServiceHandle(hSCM);
  return false;
  }

 //查询是否在运行
 QueryServiceStatus(hService,&ServiceStatus);
 if (ServiceStatus.dwCurrentState == SERVICE_STOPPED
  || ServiceStatus.dwCurrentState == SERVICE_STOP_PENDING)
  {
  WriteToLog("Stop--服务已经停止,无需停止!",sizeof("Stop--服务已经停止,无需停止!"));
  CloseServiceHandle(hService);
  CloseServiceHandle(hSCM);
  return true;
  }

 //现在开始停止

 if (!ControlService(hService,SERVICE_CONTROL_STOP,&ServiceStatus))
  {
  WriteToLog("Stop--停止服务失败!",sizeof("Stop--停止服务失败!"));
  CloseServiceHandle(hService);
  CloseServiceHandle(hSCM);
  return false;
  }
 //再查询状态
 QueryServiceStatus(hService,&ServiceStatus);
 dwWaitTime = ServiceStatus.dwWaitHint / 10;
 if (dwWaitTime < 1000)
  {
  dwWaitTime = 1000;
  }
 else if (dwWaitTime > 10000)
  {
  dwWaitTime = 10000;
  }
 while (ServiceStatus.dwCurrentState == SERVICE_STOP_PENDING)
  {
  Sleep(dwWaitTime);
  QueryServiceStatus(hService,&ServiceStatus);
  }

 if (ServiceStatus.dwCurrentState == SERVICE_STOPPED)
  {
  WriteToLog("Stop--停止服务成功!",sizeof("Stop--停止服务成功!"));
  }
 else
  {
  WriteToLog("Stop--停止服务失败!",sizeof("Stop--停止服务失败!"));
  }
 CloseServiceHandle(hService);
 CloseServiceHandle(hSCM);
 return true;
}
//--------------------------------------------------------------------
// 卸载服务
bool CServiceControl::UnInstall(void)
{
 SC_HANDLE hSCM = NULL;
 SC_HANDLE hService = NULL;
 SERVICE_STATUS ServiceStatus = {0};

 //检测是否安装
 if (!IsInstall())
  {
  WriteToLog("UnInstall--服务尚未安装,不用卸载!",sizeof("UnInstall--服务尚未安装,不用卸载!"));
  return true;
  }

 //先停止服务
 if (!Stop())
  {
  WriteToLog("UnInstall--无法停止服务!卸载失败!",sizeof("UnInstall--无法停止服务!卸载失败!"));
  return false;
  }
 //打开服务
 hSCM = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
 if (hSCM == NULL)
  {
  WriteToLog("UnInstall--无法打开服务管理器!",sizeof("UnInstall--无法打开服务管理器!"));
  return false;
  }

 hService = OpenService(hSCM,m_tzServiceName,SERVICE_STOP|DELETE);
 if (hService == NULL)
  {
  WriteToLog("UnInstall--无法打开服务!卸载失败!",sizeof("UnInstall--无法打开服务!卸载失败!"));
  CloseServiceHandle(hSCM);
  return false;
  }

 //开始卸载。
 if (!DeleteService(hService))
  {
  WriteToLog("UnInstall--无法卸载服务!",sizeof("UnInstall--无法卸载服务!"));
  CloseServiceHandle(hService);
  CloseServiceHandle(hSCM);
  return false;
  }
 else
  {
  WriteToLog("UnInstall--卸载服务成功!",sizeof("UnInstall--卸载服务成功!"));
  }

 CloseServiceHandle(hService);
 CloseServiceHandle(hSCM);
 return true;

// 设置日志文件名。参数为NULL,则根据日期生成文件名。
void CServiceControl::SetLogFileName(LPCTSTR lpLogFileName)

 //获取当前路径
 //TCHAR tzCurPath[MAX_PATH] = {0};
 TCHAR tzFileName[MAX_PATH] = {0};
 SYSTEMTIME SystemTime = {0};

 GetModuleFileName(NULL,m_tzLogPathName,MAX_PATH);
 PathRemoveFileSpec(m_tzLogPathName);
 PathCombine(m_tzLogPathName,m_tzLogPathName,TEXT("log//"));
 //检测日志目录是否存在,不存在就创建
 if (!PathFileExists(m_tzLogPathName))
  {
  CreateDirectory(m_tzLogPathName,NULL);
  }
 

 if (lpLogFileName == NULL)
  {
   //以日期为路径
  GetSystemTime(&SystemTime);
  sprintf_s(tzFileName,MAX_PATH,TEXT("%d%d%d.txt"),SystemTime.wYear,SystemTime.wMonth,SystemTime.wDay);

  PathCombine(m_tzLogPathName,m_tzLogPathName,tzFileName);
  }
 else
  {
  PathCombine(m_tzLogPathName,m_tzLogPathName,lpLogFileName);
  }
}
//--------------------------------------------------------------------
// 写入日志
bool CServiceControl::WriteToLog(char* pLogContent,DWORD dwContentSize)
{
 HANDLE hFile = INVALID_HANDLE_VALUE;
 DWORD dwWrittenLen = 0;
 SYSTEMTIME SystemTime = {0};
 char szTime[50] = {0};

 hFile = CreateFile(m_tzLogPathName,GENERIC_ALL,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
 if (hFile == INVALID_HANDLE_VALUE)
  {
  return false;
  }
 SetFilePointer(hFile,0,NULL,FILE_END);
 GetSystemTime(&SystemTime);
 sprintf_s(szTime,50,"%d-%d-%d %d:%d:%d----",SystemTime.wYear,SystemTime.wMonth,SystemTime.wDay,
  SystemTime.wHour + 8,SystemTime.wMinute,SystemTime.wSecond);
 if (GetFileSize(hFile,NULL) == 0)
  {
  WriteFile(hFile,"程序编写者:天漏客,个人主页:http://www.lilu.name,QQ:285252760。/r/n/r/n",
  sizeof("程序编写者:天漏客,个人主页:http://www.lilu.name,QQ:285252760。/r/n/r/n"),&dwWrittenLen,NULL);
  }
 WriteFile(hFile,szTime,(DWORD)strlen(szTime),&dwWrittenLen,NULL);
 WriteFile(hFile,pLogContent,dwContentSize,&dwWrittenLen,NULL);
 WriteFile(hFile,"/r/n",2,&dwWrittenLen,NULL);

 CloseHandle(hFile);

 return true;
}
//--------------------------------------------------------------------
// 查询服务状态
DWORD CServiceControl::QueryStatus(void)
{
 SC_HANDLE hSCM = NULL;
 SC_HANDLE hService = NULL;
 SERVICE_STATUS ServiceStatus = {0};

 hSCM = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
 if (hSCM == NULL)
  {
  WriteToLog("QueryStatus--无法打开服务管理器!",sizeof("QueryStatus--无法打开服务管理器!"));
  return false;
  }
 hService = OpenService(hSCM,m_tzServiceName,SERVICE_QUERY_STATUS);
 if (hService == NULL)
  {
  WriteToLog("QueryStatus--打开服务失败!",sizeof("QueryStatus--打开服务失败!"));
  CloseServiceHandle(hSCM);
  return false;
  }

 //查询服务状态
 if (QueryServiceStatus(hService,&ServiceStatus))
  {
  WriteToLog("QueryStatus--查询服务运行状态失败!",sizeof("QueryStatus--查询服务运行状态失败!"));
  CloseServiceHandle(hService);
  CloseServiceHandle(hSCM);
  return false;
  }


 CloseServiceHandle(hService);
 CloseServiceHandle(hSCM);

 return ServiceStatus.dwCurrentState;
}

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/69276
推荐阅读
相关标签
  

闽ICP备14008679号