Windows Service Manipulation Tips

Source: Internet
Author: User
Tags printf win32 win32 window

Along with the study of Windows services, gradually mastered some tips, now share with you.

Turning a Windows service into a console program

Because of the default Windows service program, the Win32 window program is compiled. When we start or run the program, if we want to see some debugging information, then we can only pass the DebugView or output to the log way. Because if we output debug information through printf or std::cout, the Win32 window program cannot be displayed.

At this time, how we miss our Classic console program Ah, it can be very convenient to our debugging information output, is simply too convenient. That being the case, let's just let it change in a second, well, it should be a second variable console.

Share my implementation code below

Collapse#ifdef _debug//debug version, output directly to the console #define out (s) printf_s (s); 
#define OUT_LN (s) printf_s (s## "\ r \ n");
#else//non-debug version, output to the debugger, typically using DebugView #define out (s) outputdebugstring (s);
#define OUT_LN (s) outputdebugstring (s);
    #endif class Cservicesmodule:public atl::catlservicemodulet< cservicesmodule, Ids_servicename > {public: Declare_libid (libid_serviceslib) Declare_registry_appid_resourceid (Idr_services, "{
    0794CF96-5CC5-432E-8C1D-52B980ACBE0F} ") HRESULT initializesecurity () throw () {return S_OK;
    }//Service initiation HRESULT Load ();
    
    Service Stop HRESULT UnLoad ();
        HRESULT Run (_in_ int nshowcmd = sw_hide) throw () {HRESULT hr = S_OK;
        Out_ln ("Ready to start service");
        hr = Load ();
            if (HR) {out_ln ("Start service failed");
        return HR;
    
        } out_ln ("services started");
    
        hr = atl::catlservicemodulet< Cservicesmodule, ids_servicename >::run (nShowCmd); hr = UNLOad ();
        OUT_LN ("Services normal exit");
    return HR;
    
}
};
    
Cservicesmodule _atlmodule; #ifndef _DEBUG//non-DEBUG version, compiled to Win32 program extern "C" int winapi _tWinMain (hinstance/*hinstance*/, hinstance/*hprevinstanc e*/, LPTSTR/*lpcmdline*/, int nshowcmd) {return _atlmodule.winmain (nshowcmd);} #el  Se//debug version, compiled to console program int _tmain (int argc, _tchar* argv[]) {return _atlmodule.winmain (sw_show);} #endif HRESULT
    Cservicesmodule::load () {OUT_LN ("service is starting");
return 0;
    HRESULT cservicesmodule::unload () {OUT_LN ("service is stopping");
return 0; }

The _DEBUG macro is used to differentiate whether the compiler is compiled into a console program.

When you specify that the debug version is compiled, you can compile the program into a console program, register the service through RegServer, and then run the service EXE directly, so that the information that is output through printf can be displayed on the console, as shown below.

When a release version is specified, the program is compiled into a WIN32 program, service is registered through the service, and the service is run and stopped through the Services manager.

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.