描述:
同样的代码,在XP下可以正常运行
在Win2000下,
安装服务后卸载 没有问题
安装服务后运行服务再卸载 没有问题
安装服务后运行服务,停止服务,再卸载则出现服务已禁用,删除操作必须在重新启动后才能生效。
为什么?
解决方案1:
奇怪!
解决方案2: 我用vc 6建的ATL SERVER没出现过这种情况,不知道有没有用,给你贴一点看看
inline HRESULT CServiceModule::UnregisterServer()
{
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
return hr;
// Remove service entries
UpdateRegistryFromResource(IDR_Service, FALSE);
// Remove service
Uninstall();
// Remove object entries
hr = CComModule::UnregisterServer(TRUE);
CoUninitialize();
// return S_OK;
return hr;
}
inline BOOL CServiceModule::Uninstall()
{
if (!IsInstalled())
return TRUE;
SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hSCM == NULL)
{
// MessageBox(NULL, _T("Couldn't open service manager"), m_szServiceName, MB_OK);
return FALSE;
}
SC_HANDLE hService = ::OpenService(hSCM, SRV_SERVICENAME, SERVICE_STOP | DELETE);
if (hService == NULL)
{
::CloseServiceHandle(hSCM);
// MessageBox(NULL, _T("Couldn't open service"), m_szServiceName, MB_OK);
return FALSE;
}
SERVICE_STATUS status;
::ControlService(hService, SERVICE_CONTROL_STOP, &status);
BOOL bDelete = ::DeleteService(hService);
::CloseServiceHandle(hService);
::CloseServiceHandle(hSCM);
if (bDelete)
return TRUE;
// MessageBox(NULL, _T("Service could not be deleted"), m_szServiceName, MB_OK);
return FALSE;
}
我也遇到过这样的问题,MSDN上也没有找到答案,估计是要删除注册表项才能彻底删除.