我写一段,你将东西套进去就可以了,这里原来写的是定时服务,,用的是boost::asio库,当然最好看一看boost::asio相关介绍。
#include <iostream>
#include <fstream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#ifdef _WIN32
#include <windows.h>
#endif
class printer
{
public:
printer(boost::asio::io_service &io)
{
........
}
~printer()
{
}
......
};
#ifdef _WIN32
boost::function0<void> console_ctrl_function;
SERVICE_STATUS_HANDLE g_ssh = NULL;
DWORD g_dwStatus = SERVICE_STOPPED;
int g_exitcode;
BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
{
switch (ctrl_type)
{
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_SHUTDOWN_EVENT:
console_ctrl_function();
return TRUE;
default:
return FALSE;
}
}
void SetStatus(DWORD dwStatus)
{
SERVICE_STATUS ss = {
SERVICE_WIN32_OWN_PROCESS,
SERVICE_STOPPED,
SERVICE_ACCEPT_STOP,
NO_ERROR,
0,
1,
5000};
ss.dwCurrentState = dwStatus;
SetServiceStatus(g_ssh, &ss);
g_dwStatus = dwStatus;
}
VOID WINAPI Handler(DWORD dwCtl)
{
switch (dwCtl)
{
case SERVICE_CONTROL_STOP:
console_ctrl_function();
break;
default:
SetStatus(g_dwStatus);
}
}
VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
{
g_ssh = RegisterServiceCtrlHandler(lpszArgv[0], Handler);
SetStatus(SERVICE_START_PENDING);
try
{
boost::asio::io_service io;
printer p(io);
console_ctrl_function = boost::bind(&boost::asio::io_service::stop, &io);
SetStatus(SERVICE_RUNNING);
io.run();
g_exitcode = EXIT_SUCCESS;
}
catch (std::exception &e)
{
std::cerr << "[EXCEPTION]\t" << e.what() << std::endl;
g_exitcode = EXIT_FAILURE;
}
catch (const char *e)
{
std::cerr << "[EXCEPTION]\t" << e << std::endl;
g_exitcode = EXIT_FAILURE;
}
SetStatus(SERVICE_STOPPED);
}
int main(int argc, char **argv)
{
SERVICE_TABLE_ENTRY arSvc[] =
{
{L"ChinuxTeam TestServ v1.0", ServiceMain},
{NULL, NULL}
};
if (!StartServiceCtrlDispatcher(arSvc))
{
std::cerr << "Start Service Error" << std::endl;
return EXIT_FAILURE;
}
return g_exitcode;
}
#endif
相关文章
- PyQt(Python+Qt)学习随笔:windows下使用pyinstaller将PyQt文件打包成exe可执行文件
- Windows平台下解决Oracle12c使用PDB数据库创建SDE的问题
- 【Windows编程】系列第七篇:Menubar的创建和使用
- 【翻译】利用Qt设计师窗体在运行时创建用户界面(Creating a user interface from a Qt Designer form at run-time)
- Qt应用程序:无法加载平台插件“windows”。可用的平台是:
- 创建新网格会导致Windows手机应用崩溃
- 使用Windows Service Wrapper快速创建一个Windows Service 如nginx
- 使用QT创建系统托盘
- 部署一个Windows窗体应用程序,该应用程序链接到另一个系统上在visual studio 2010上创建的两个excel文件
- Windows XP 下如何使用Qt Creator中的Git版本控制功能