您好,登录后才能下订单哦!
本篇内容主要讲解“Python服务程序代码怎么写”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python服务程序代码怎么写”吧!
#include "stdafx.h"
#include <windows.h>
using namespace std;
class CDSvr{
public:
static void ShowHelp(void);
CDSvr(){para=NULL;};
CDSvr(_TCHAR* argv[]);
~CDSvr(){};
void Run();
static bool CreateSvr(_TCHAR* svrname,_TCHAR* path);
static bool DeleteSvr(_TCHAR* svrname);
private:
_TCHAR** para;
};
CDSvr::CDSvr(_TCHAR* argv[]){
para=argv;
Run();
}
void CDSvr::Run(){
}
void CDSvr::ShowHelp(){
cout<<" [use] "<<endl
<<" [svrname] [path] "<<" create the service "<<endl
<<" [svrname]"<<" delete service "<<endl;
}
bool CDSvr::CreateSvr(_TCHAR *svrname, _TCHAR *path){
SC_HANDLE sch=::OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE);
if( NULL==sch ){
cout<<"openSCManager failed!"<<endl;
return false;
}
SC_HANDLE sccs=NULL;
if( NULL==(sccs=::CreateService( sch,svrname,svrname,
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS,
SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,path,NULL,NULL,NULL,NULL,NULL)
)
){
cout<<"CreateService Error"<<endl;
::CloseHandle(sch);
::CloseHandle(sccs);
return false;
}
::CloseHandle(sch);
::CloseHandle(sccs);
return true;
}
bool CDSvr::DeleteSvr(_TCHAR *svrname){
SC_HANDLE sch=::OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE);
if( NULL==sch ){
cout<<"openSCManager failed!"<<endl;
return false;
}
SC_HANDLE sc=::OpenService(sch,svrname,SERVICE_ALL_ACCESS);
if( NULL==sc ){
cout<<"open services failed!"<<endl;
::CloseHandle(sch);
::CloseHandle(sc);
return false;
}
if(::DeleteService(sc)){
cout<<"delete "<<sc<<" SUCCESS"<<endl;
}else{
cout<<"delete "<<sc<<" ERROR"<<endl;
}
::CloseHandle(sch);
::CloseHandle(sc);
return true;
};
int _tmain(int argc, _TCHAR* argv[])
{
if( argc<2 ){
CDSvr::ShowHelp();
}else if( argc==2 ){
if(CDSvr::DeleteSvr( argv[argc-1])){
cout<< " DONE !!!"<<endl;
}else{
cout<< " ERROR!!!"<<endl;
}
}else if( argc==3 ){
if(CDSvr::CreateSvr(argv[argc-2],argv[argc-1])){
cout<<" DONE!!!"<<endl;
}else{
cout<<" ERROR!!"<<endl;
}
}
return 0;
}
到此,相信大家对“Python服务程序代码怎么写”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。