I write a paragraph, you will put things into it, here originally written is the timing service, with the Boost::asio library, of course, it is best to take a look at Boost::asio related introduction.
#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
QT Solution about the service, with QT integration a little better, but if just want to realize the service, not to do what cross-platform Ah, library ah and so on, in win directly refer to MSDN, search can also give a lot of code
https://code.msdn.microsoft.com/CppWindowsService-cacf4948
No thanks, no thanks.
Create a Windows service program with QT