如何把控制台exe启动为windows服务

I_believe328 2011-03-21 09:29:46
用安装程序去调用控制台程序, 希望把它启动为windows服务,应该怎么做。
...全文
164 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eleven 2011-03-21
  • 打赏
  • 举报
回复

2000下可以安裝服務來實現.
BOOL InstallService(DWORD dwArgc,LPTSTR *lpszArgv)
{
BOOL bRet=FALSE;
__try
{
//Open Service Control Manager on Local or Remote machine
hSCManager=OpenSCManager(szTarget,NULL,SC_MANAGER_ALL_ACCESS);//szTarget可以是遠程機器,為空則指本地機.
if(hSCManager==NULL)
{
printf( "\nOpen Service Control Manage failed:%d ",GetLastError());
__leave;
}
//printf( "\nOpen Service Control Manage ok! ");
//Create Service
hSCService=CreateService(hSCManager,// handle to SCM database
ServiceName,// name of service to start
ServiceName,// display name
SERVICE_ALL_ACCESS,// type of access to service
SERVICE_WIN32_OWN_PROCESS,// type of service
SERVICE_AUTO_START,// when to start service
SERVICE_ERROR_IGNORE,// severity of service
failure
EXE,// 你的exe文件
NULL,// name of load ordering group
NULL,// tag identifier
NULL,// array of dependency names
NULL,// account name
NULL);// account password
//create service failed
if(hSCService==NULL)
{
//如果服务已经存在,那么则打开
if(GetLastError()==ERROR_SERVICE_EXISTS)
{
//printf( "\nService %s Already exists ",ServiceName);
//open service
hSCService = OpenService(hSCManager, ServiceName,
SERVICE_ALL_ACCESS);
if(hSCService==NULL)
{
printf( "\nOpen Service failed:%d ",GetLastError());
__leave;
}
//printf( "\nOpen Service %s ok! ",ServiceName);
}
else
{
printf( "\nCreateService failed:%d ",GetLastError());
__leave;
}
}
//create service ok
else
{
//printf( "\nCreate Service %s ok! ",ServiceName);
}

// 起动服务
if ( StartService(hSCService,dwArgc,lpszArgv))
{
//printf( "\nStarting %s. ", ServiceName);
Sleep(20);//时间最好不要超过100ms
while( QueryServiceStatus(hSCService, &ssStatus ) )
{
if ( ssStatus.dwCurrentState == SERVICE_START_PENDING)
{
printf( ". ");
Sleep(20);
}
else
break;
}
if ( ssStatus.dwCurrentState != SERVICE_RUNNING )
printf( "\n%s failed to run:%d ",ServiceName,GetLastError());
}
else if(GetLastError()==ERROR_SERVICE_ALREADY_RUNNING)
{
//printf( "\nService %s already running. ",ServiceName);
}
else
{
printf( "\nStart Service %s failed:%d ",ServiceName,GetLastError());
__leave;
}
bRet=TRUE;
}//enf of try
__finally
{
return bRet;
}
return bRet;
}

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧