Creating a Windows service program with QT

Source: Internet
Author: User

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, &AMP;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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.