c++ windows服务的编写的问题 高手麻烦看一下

huguanhuaeric 2009-03-21 07:59:01
书上给了一个编写服务的框架 v6下调试 可是运行后没有在服务里出现新的服务 高手帮忙看下
#include <stdio.h>
#include <windows.h>
SERVICE_STATUS m_ServiceStatus;
SERVICE_STATUS_HANDLE m_ServiceStatusHandle;
BOOL bRunning=true;
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);//服务主函数
void WINAPI ServiceCtrlHandler(DWORD Opcode);//服务控制函数
void WINAPI CmdStart(void);//要启动的程序函数
BOOL InstallService(); //安装服务的函数
BOOL DeleteService(); //删除服务的函数

int main(int argc, char* argv[])
{
printf("\twindows based service demo\n");
printf("\tgxisone@hotmail.com\n");
/* if(argc!=3)
{
printf("usage: %s -install[remove]",argv[0]);
return 0;
}
if(strcmp(argv[1],"-install")==0) /// install
{
if(InstallService())
printf("\n\nService Installed Sucessfully\n");
else
printf("\n\nError Installing Service\n");
}
else if(strcmp(argv[1],"-remove")==0) // remove
{
if(DeleteService())
printf("\n\nService remove sucessfully\n");
else
printf("\n\nError removing Service\n");
}
else
{
printf("\nusage: %s -install[remove]\n",argv[0]);
return 0;
}*/
//在进入点函数里面要完成ServiceMain的初始化,
//准确点说是初始化一个SERVICE_TABLE_ENTRY结构数组,
//这个结构记录了这个服务程序里面所包含的所有服务的名称
//和服务的进入点函数
SERVICE_TABLE_ENTRY DispatchTable[]={{"WindowsMgr",ServiceMain},{NULL,NULL}};
//最后的NULL指明数组的结束

StartServiceCtrlDispatcher(DispatchTable);




return 0;
}

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
m_ServiceStatus.dwServiceType = SERVICE_WIN32;
m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwServiceSpecificExitCode = 0;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
m_ServiceStatusHandle = RegisterServiceCtrlHandler("WindowsMgr",ServiceCtrlHandler);
if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)return;
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING; //设置服务状态
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
//SERVICE_STATUS结构含有七个成员,它们反映服务的现行状态。
//所有这些成员必须在这个结构被传递到SetServiceStatus之前正确的设置
SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus);
bRunning=true;
//*
CmdStart(); //启动我们的服务程序
//*
return;
}
void WINAPI ServiceCtrlHandler(DWORD Opcode)//服务控制函数
{
switch(Opcode)
{
case SERVICE_CONTROL_PAUSE: // we accept the command to pause it
m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
break;
case SERVICE_CONTROL_CONTINUE: // we got the command to continue
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
break;
case SERVICE_CONTROL_STOP: // we got the command to stop this service
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
bRunning=false;
break;
case SERVICE_CONTROL_INTERROGATE: //
break;
}
return;
}
BOOL InstallService() //安装服务函数
{
char strDir[1024];
SC_HANDLE schSCManager,schService;
GetCurrentDirectory(1024,strDir);
GetModuleFileName(NULL,strDir,sizeof(strDir));

char chSysPath[1024];
GetSystemDirectory(chSysPath,sizeof(chSysPath));

strcat(chSysPath,"\\WindowsMgr.exe");
if(!CopyFile(strDir,chSysPath,FALSE))printf("Copy file OK\n"); // 把我们的服务程序复制到系统根目录

strcpy(strDir,chSysPath);
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
{
printf("open scmanger failed,maybe you do not have the privilage to do this\n");
return false;
}

LPCTSTR lpszBinaryPathName=strDir;

schService = CreateService(schSCManager,"WindowsMgr","Windows Manger Control", //将服务的信息添加到SCM的数据库
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
lpszBinaryPathName, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password

if (schService == NULL)
{
printf("faint,we failed just because we invoke createservices failed\n");
return false;
}
CloseServiceHandle(schService);
return true;
}
BOOL DeleteService()
{
SC_HANDLE schSCManager;
SC_HANDLE hService;
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

char chSysPath[1024];
GetSystemDirectory(chSysPath,sizeof(chSysPath));
strcat(chSysPath,"\\WindowsMgr.exe");

if (schSCManager == NULL)
{
printf("faint,open scmanger failed\n");
return false;
}
hService=OpenService(schSCManager,"WindowsMgr",SERVICE_ALL_ACCESS);
if (hService == NULL)
{
printf("faint,open services failt\n");
return false;
}
if(DeleteFile(chSysPath)==0)
{
printf("Dell file Failure !\n");
return false;
}
else printf("Delete file OK!\n");
if(DeleteService(hService)==0)
return false;

if(CloseServiceHandle(hService)==0)
return false;
else
return true;
}

void WINAPI CmdStart(void)
{
LoadLibrary("msvcrt.dll");
system("command.com");
//--------------------------------
//把你的要做成服务启动的程序代码添加到这里
//那么你的代码就可以作为NT服务启动了
//--------------------------------
}
...全文
288 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
huguanhuaeric 2009-03-22
  • 打赏
  • 举报
回复
看来大学四年还有很长一段路要走 继续努力吧
huguanhuaeric 2009-03-22
  • 打赏
  • 举报
回复
看来大学四年还有很长一段路要走 继续努力吧
huguanhuaeric 2009-03-21
  • 打赏
  • 举报
回复
创建成功 可是无法开启 错误1053:服务没有及时响应启动或控制请求
qingkongyihe2008 2009-03-21
  • 打赏
  • 举报
回复
都是高手啊
cnStreamlet 2009-03-21
  • 打赏
  • 举报
回复
用 sc 命令试试看吧
打开命令提示符,输入
sc create ServiceName BinPath= "ExePath" DisplayName= "DisplayName" Start= auto
sc start ServiceName

ServiceName、DisplayName 随意取,ExePath 写对就好了

用完了
sc stop ServiceName
sc delete ServiceName

就可以了
huguanhuaeric 2009-03-21
  • 打赏
  • 举报
回复
恩 这样做了可是还是显示faint,we failed just because we invoke createservices failed
Error Installing Service
kunjyo 2009-03-21
  • 打赏
  • 举报
回复
比如你编译出的程序叫servicetest.exe. 在dos窗口下,该程序的目录里面,执行"servicetest.exe -install"
huguanhuaeric 2009-03-21
  • 打赏
  • 举报
回复
请问一下什么叫“编译好的exe文件执行的时候带上"-install"参数” 还有我注释掉的那段话是什么意思 不好意思 菜鸟的问题多一点
kunjyo 2009-03-21
  • 打赏
  • 举报
回复
[Quote=引用楼主 huguanhuaeric 的帖子:]
/* if(argc!=3)
{
printf("usage: %s -install[remove]",argv[0]);
return 0;
}
if(strcmp(argv[1],"-install")==0) /// install
{
if(InstallService())
printf("\n\nService Installed Sucessfully\n");
else
printf("\n\nError Installing Service\n");
}
else if(strcmp(argv[1],"-remove")==0) // remove
{
if(DeleteService())
printf("\n\nService remove sucessfully\n");
else
printf("\n\nError removing Service\n");
}
else
{
printf("\nusage: %s -install[remove]\n",argv[0]);
return 0;
}*/
[/Quote]

1.注释去掉,并把if(argc!=3)改成if(argc!=2)

2.编译好的exe文件执行的时候带上"-install"参数,完成后在services里面可以看到“Windows Manger Control”服务

Good luck!
oyljerry 2009-03-21
  • 打赏
  • 举报
回复
可以先但不挑食一下,看你的代码注册服务的时候,是否失败了...打印一下错误信息等
huguanhuaeric 2009-03-21
  • 打赏
  • 举报
回复
自己顶一下

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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