谢谢
解决方案 »
- 图像问题
- 求教,这个序列化为什么错误?
- 为什么我的代码运行那么慢,请看 ~_~||
- 在vc控制台下的问题!
- 如何修改256色位图的调色板?
- 怎样用PB程序代码在SQL_server中建立数据库?急!!!
- 如何生成有窗口,菜单,工具栏的自定义控件?
- 如何让 EDIT BOX UPDATEDATE时 自动滚屏到最下方(用CEDIT::VSCROLL(LINE)不行)
- MFC CDHtmlDialog OnDocumentComplete如何判断网页正确加载完毕? 断网的情况
- 这是如何实现的?CRichEditCtrl::GetLineCount() const
- 哪位大哥知道做矢量字体需要什么软件呀??
- 问一个简单的问题,见笑了!关于字符串的问题?
2。启动一个Service,使用StarService
{
SERVICE_STATUS ssStatus;
DWORD dwOldCheckPoint;
DWORD dwStartTickCount;
DWORD dwWaitTime; if (!ControlService(
schService, // handle to service
SERVICE_CONTROL_STOP, // control value to send
&ssStatus) ) // address of status info
{
AfxMessageBox("停止服务 MSSQLSERVER 时出现错误!");
return FALSE;
}
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of status information structure
{
AfxMessageBox("查询服务状态的时候出现错误!");
return FALSE;
}
// Save the tick count and initial checkpoint. dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint; while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
{
// Do not wait longer than the wait hint. A good interval is
// one tenth the wait hint, but no less than 1 second and no
// more than 10 seconds.
dwWaitTime = ssStatus.dwWaitHint / 10; if( dwWaitTime < 1000 )
dwWaitTime = 1000;
else if ( dwWaitTime > 10000 )
dwWaitTime = 10000; Sleep( dwWaitTime ); // Check the status again.
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of structure
break;
if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
{
// The service is making progress. dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
else
{
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
{
// No progress made within the wait hint
break;
}
}
} if (ssStatus.dwCurrentState != SERVICE_STOPPED)
{
AfxMessageBox("停止服务 MSSQLSERVER 时出现错误!");
return FALSE;
}
return TRUE;
}BOOL StartService(SC_HANDLE schService)
{
SERVICE_STATUS ssStatus;
DWORD dwOldCheckPoint;
DWORD dwStartTickCount;
DWORD dwWaitTime;
if (!StartService(
schService, // handle to service
0, // number of arguments
NULL) ) // no arguments
{
AfxMessageBox("启动服务 MSSQLSERVER 时出现错误!");
return FALSE;
}
// Check the status until the service is no longer start pending.
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of status information structure
{
AfxMessageBox("查询服务状态的时候出现错误!");
return FALSE;
}
// Save the tick count and initial checkpoint. dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint; while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
{
// Do not wait longer than the wait hint. A good interval is
// one tenth the wait hint, but no less than 1 second and no
// more than 10 seconds.
dwWaitTime = ssStatus.dwWaitHint / 10; if( dwWaitTime < 1000 )
dwWaitTime = 1000;
else if ( dwWaitTime > 10000 )
dwWaitTime = 10000; Sleep( dwWaitTime ); // Check the status again.
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of structure
break;
if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
{
// The service is making progress. dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
else
{
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
{
// No progress made within the wait hint
break;
}
}
} if (ssStatus.dwCurrentState != SERVICE_RUNNING)
{
AfxMessageBox("启动服务 MSSQLSERVER 时出现错误!");
return FALSE;
}
return TRUE;
} // CattachdbApp 初始化BOOL CattachdbApp::InitInstance()
{
CWinApp::InitInstance(); //打开服务管理器
SC_HANDLE schSCManager = OpenSCManager(
NULL, // local machine
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if (schSCManager == NULL)
{
AfxMessageBox("打开服务管理器的时候出现错误!");
return FALSE;
}
SC_HANDLE schService = OpenService(
schSCManager, // SCM database
SERVICENAME, // service name
SERVICE_ALL_ACCESS);
if (schService == NULL)
{
CloseServiceHandle(schSCManager);
AfxMessageBox("打开服务 MSSQLSERVER 时出现错误");
return FALSE;
}
//先停止服务
if(!StopService(schService))
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return FALSE;
}
//启动服务
if(!StartService(schService))
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return FALSE;
} CloseServiceHandle(schService);
CloseServiceHandle(schSCManager); return FALSE;
}
BOOL QueryServiceStatus(CString strServiceName)
{
BOOL bCurrentStatus = FALSE; SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SERVICE_STATE_ALL);
if (hSCM == NULL)
{
return bCurrentStatus;
} SC_HANDLE hService =NULL;
hService=::OpenService(hSCM, strServiceName, GENERIC_READ);
if (hService == NULL)
{
::CloseServiceHandle(hSCM);
return bCurrentStatus;
} SERVICE_STATUS status;
if(::QueryServiceStatus(hService,&status) == 0)
{
bCurrentStatus = FALSE;
}
else if(!(status.dwCurrentState & SERVICE_RUNNING))
{
bCurrentStatus = TRUE;
}
::CloseServiceHandle(hService);
::CloseServiceHandle(hSCM); return bCurrentStatus;
}
与上述函数一样,StartService过程利用以下语句得到hService调用StartService即可;
BOOL StartService(CString strServiceName)
{
//...
hService=::OpenService(hSCM, strServiceName, GENERIC_READ);
//...
StartService(hService,0,NULL));
//...
}