首页
社区
课程
招聘
[求助]自己加载有服务总是卡在“正在启动中”,求破
发表于: 2013-7-30 08:42 4977

[求助]自己加载有服务总是卡在“正在启动中”,求破

2013-7-30 08:42
4977
本来是想搞个图片显示出来的,但是看雪显示图片要链接。。我就用传附件的格式了。。

///////------------------------------下面是安装服务的代码------------------------------------
BOOL InstallService(LPCTSTR strServiceName, 
					LPCTSTR strDisplayName, 
					LPCTSTR strDescription,
					LPCTSTR strPathName)
{
	BOOL bRet = FALSE;
	HKEY key=NULL;
	SC_HANDLE svc=NULL, scm=NULL;
	__try
	{
		scm = OpenSCManager(0, 0,SC_MANAGER_ALL_ACCESS);
		if (!scm)
			__leave;
		svc = CreateService(
			scm, 
			strServiceName, 
			strDisplayName,
			SERVICE_ALL_ACCESS|SERVICE_INTERACTIVE_PROCESS,
			SERVICE_WIN32_OWN_PROCESS,
			SERVICE_AUTO_START,
			SERVICE_ERROR_NORMAL,
			strPathName,
			NULL, NULL, NULL, NULL, NULL);

		if (svc == NULL)
		{
			if (GetLastError() == ERROR_SERVICE_EXISTS)
			{
				
				svc = OpenService(scm,strServiceName,SERVICE_ALL_ACCESS);
				if (svc==NULL)
					__leave;
				else
					StartService(svc,0, 0);
			
				return true;
			}
		}

		TCHAR Desc[MAX_PATH];
		wsprintf(Desc,_T("SYSTEM\\CurrentControlSet\\Services\\%s"), strServiceName);
		RegOpenKey(HKEY_LOCAL_MACHINE,Desc,&key);
		RegSetValueEx(key,_T("Description"),0,REG_SZ,(CONST BYTE*)strDescription,(lstrlen(strDescription)+1)*sizeof(TCHAR));
		RegSetValueEx(key,_T("ServiceMain"),0,REG_SZ,(CONST BYTE *)L"UCC",(wcslen(L"UCC")+1)*sizeof(TCHAR));

		
		if (!StartService(svc,0, 0))
			__leave;
			

		bRet = TRUE;
	}
	__finally
	{
		if (key!=NULL) 
			RegCloseKey(key);
		if (svc!=NULL)
			CloseServiceHandle(svc);
		if (scm!=NULL)
			CloseServiceHandle(scm);
	}

	return bRet;
}


BOOL InstallSvchostService(LPCTSTR strServiceName, 
					       LPCTSTR strDisplayName, 
					       LPCTSTR strDescription,
					       LPCTSTR strDllPath)
{
	
    	int rc = 0;
	HKEY hKey = 0;
	BOOL bRet = FALSE;
	TCHAR szOpenKey[MAX_PATH];
	try
	{
      bRet = InstallService(strServiceName,
                     strDisplayName,
					 strDescription,
                     _T("%SystemRoot%\\System32\\svchost.exe -k rpccss")); //安装服务

		//修改dll指向
		ZeroMemory(szOpenKey,sizeof(szOpenKey));
		wsprintf(szOpenKey, _T("SYSTEM\\CurrentControlSet\\Services\\%s\\Parameters"), strServiceName);
        //rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szOpenKey, 0, KEY_ALL_ACCESS, &hKey);
		rc = RegCreateKey(HKEY_LOCAL_MACHINE, szOpenKey,&hKey); 
        if(ERROR_SUCCESS != rc) throw "";

        rc = RegSetValueEx(hKey, _T("ServiceDll"), 0, REG_EXPAND_SZ, (unsigned char*)strDllPath, (lstrlen(strDllPath)+1)*sizeof(TCHAR));
        SetLastError(rc);
		if(ERROR_SUCCESS != rc) throw "RegSetValueEx(ServiceDll)";
		RegCloseKey(hKey);
		//添加服务名到netsvcs组
		ZeroMemory(szOpenKey,sizeof(szOpenKey));
		lstrcpy(szOpenKey, _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Svchost"));
        rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szOpenKey, 0, KEY_ALL_ACCESS, &hKey);
        if(ERROR_SUCCESS != rc) throw "RegOpenKeyEx(Svchost)";
		rc = RegSetValueEx(hKey, _T("rpccss"), 0, REG_MULTI_SZ, (unsigned char*)strServiceName, (lstrlen(strServiceName)+1)*sizeof(TCHAR));
        SetLastError(rc);
        if(ERROR_SUCCESS != rc) throw "RegSetValueEx(Svchost\\rpccss)";
		RegCloseKey(hKey);

		bRet = StartService(strServiceName);
	}
	catch(char *str)
	{
		if(str && str[0])
        {
            rc = GetLastError();
        }
	}
	
	RegCloseKey(hKey);

	return bRet;
	
}



求来个服务帝指教一下。

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

上传的附件:
收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 110
活跃值: (527)
能力值: ( LV4,RANK:40 )
在线值:
发帖
回帖
粉丝
2
自己顶一个。
2013-7-30 09:11
0
雪    币: 18
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
既然显示正在启动,那应该服务安装成功了,你可以在命令行下试试手动启动,或则贴上 StartService()函数大家看看! 要么是杀软拦截,要么是你服务 Exe 程序写的有问题(这个可能也是有的,exe 替换成空的服务项目试试)
2013-7-30 09:44
0
雪    币: 110
活跃值: (527)
能力值: ( LV4,RANK:40 )
在线值:
发帖
回帖
粉丝
4
BOOL StartService(LPCTSTR lpService)
{
	SC_HANDLE        schSCManager;
	SC_HANDLE        schService;
	SERVICE_STATUS   ServiceStatus;
	DWORD            dwErrorCode;
//	TCHAR   lpBuff[1024];

	schSCManager=::OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE);//打开服务控制管理器数据库
	if (schSCManager!=NULL)
	{
		schService=::OpenService(schSCManager,lpService,SERVICE_QUERY_STATUS|SERVICE_START);
		if (schService!=NULL)
		{
			//StartService(schService,0,NULL);
			//设置服务为自动启动
			ChangeServiceConfig(schService, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
				NULL, NULL, NULL, NULL, NULL, NULL, NULL);		
			while(QueryServiceStatus(schService,&ServiceStatus)!=0)           
			{
				
				if(StartService(schService,0,NULL)==0)//已经存在该服务,就启动服务                        
				{
					dwErrorCode=GetLastError();
					if(dwErrorCode==ERROR_SERVICE_ALREADY_RUNNING)
					{
						CloseServiceHandle(schSCManager);  
						CloseServiceHandle(schService);
						return true;
					}
				}	
				if(ServiceStatus.dwCurrentState==SERVICE_START_PENDING)
				{
					Sleep(100);
				}
				else
				{
					break;
				}
			}
			CloseServiceHandle(schService);
		}
		CloseServiceHandle(schSCManager);
	}
	else
		return FALSE;

	return TRUE;
}


服务不是exe是DLl的。。重启的话是会加载。很奇怪的问题,,
2013-7-30 11:46
0
游客
登录 | 注册 方可回帖
返回
//