本文整理汇总了C++中OpenServiceW函数的典型用法代码示例。如果您正苦于以下问题:C++ OpenServiceW函数的具体用法?C++ OpenServiceW怎么用?C++ OpenServiceW使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了OpenServiceW函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: UnregisterDriver
BOOL
UnregisterDriver(LPCWSTR lpDriverName)
{
SC_HANDLE hService;
SC_HANDLE hSCManager;
BOOL bRet;
hSCManager = OpenSCManagerW(NULL,
NULL,
SC_MANAGER_ALL_ACCESS);
if (!hSCManager)
return FALSE;
hService = OpenServiceW(hSCManager,
lpDriverName,
SERVICE_ALL_ACCESS);
if (!hService)
{
CloseServiceHandle(hSCManager);
return FALSE;
}
bRet = DeleteService(hService);
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return bRet;
}
示例2: SvcUninstall
VOID SvcUninstall() {
SC_HANDLE schSCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (NULL == schSCManager) {
printf(">> OpenSCManager failed (LastError=0x%x)\n", GetLastError());
goto _ret;
}
SC_HANDLE schService = OpenServiceW(schSCManager, SERVICE_NAME, SERVICE_ALL_ACCESS);
if (schService == NULL) {
DWORD LastError = GetLastError();
if (LastError == ERROR_SERVICE_DOES_NOT_EXIST) printf(">> Service does not exist!\n");
else printf(">> OpenServiceW failed (LastError=0x%x)\n", GetLastError());
goto _close_sc;
}
SERVICE_STATUS ServiceStatus;
if (!ControlService(schService, SERVICE_CONTROL_STOP, &ServiceStatus)) {
DWORD LastError = GetLastError();
if (LastError != ERROR_SERVICE_NOT_ACTIVE) {
printf(">> ControlService(..., SERVICE_STOP, ...) failed (LastError=0x%x)\n", GetLastError());
goto _close_service;
}
}
if (!DeleteService(schService)) {
printf(">> DeleteService failed (LastError=0x%x)\n", GetLastError());
goto _close_service;
}
printf(">> Service uninstalled successfully!\n");
_close_service:
CloseServiceHandle(schService);
_close_sc:
CloseServiceHandle(schSCManager);
_ret:
return;
}
示例3: hSCManager
// Determine if the autodial service is running on this PC.
bool nsAutodial::IsAutodialServiceRunning()
{
nsAutoServiceHandle hSCManager(OpenSCManager(nullptr,
SERVICES_ACTIVE_DATABASE,
SERVICE_QUERY_STATUS));
if (hSCManager == nullptr)
{
LOGE(("Autodial: failed to open service control manager. Error %d.",
::GetLastError()));
return false;
}
nsAutoServiceHandle hService(OpenServiceW(hSCManager,
L"RasAuto",
SERVICE_QUERY_STATUS));
if (hSCManager == nullptr)
{
LOGE(("Autodial: failed to open RasAuto service."));
return false;
}
SERVICE_STATUS status;
if (!QueryServiceStatus(hService, &status))
{
LOGE(("Autodial: ::QueryServiceStatus() failed. Error: %d",
::GetLastError()));
return false;
}
return (status.dwCurrentState == SERVICE_RUNNING);
}
示例4: UpdateServiceStatus
BOOL
UpdateServiceStatus(ENUM_SERVICE_STATUS_PROCESS* pService)
{
SC_HANDLE hScm;
BOOL bRet = FALSE;
hScm = OpenSCManagerW(NULL,
NULL,
SC_MANAGER_ENUMERATE_SERVICE);
if (hScm != NULL)
{
SC_HANDLE hService;
hService = OpenServiceW(hScm,
pService->lpServiceName,
SERVICE_QUERY_STATUS);
if (hService)
{
DWORD size;
QueryServiceStatusEx(hService,
SC_STATUS_PROCESS_INFO,
(LPBYTE)&pService->ServiceStatusProcess,
sizeof(SERVICE_STATUS_PROCESS),
&size);
CloseServiceHandle(hService);
bRet = TRUE;
}
CloseServiceHandle(hScm);
}
return bRet;
}
示例5: register_service
static HRESULT register_service(BOOL do_register)
{
static const WCHAR name[] = { 'B','I','T','S', 0 };
static const WCHAR path[] = { 's','v','c','h','o','s','t','.','e','x','e',
' ','-','k',' ','n','e','t','s','v','c','s', 0 };
SC_HANDLE scm, service;
scm = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!scm)
return SELFREG_E_CLASS;
if (do_register)
service = CreateServiceW(scm, name, name, SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
path, NULL, NULL, NULL, NULL, NULL);
else
service = OpenServiceW(scm, name, DELETE);
CloseServiceHandle(scm);
if (service)
{
if (!do_register) DeleteService(service);
CloseServiceHandle(service);
}
return S_OK;
}
示例6: DoUnregServer
static DWORD DoUnregServer(void)
{
static const WCHAR msiserverW[] = {'M','S','I','S','e','r','v','e','r',0};
SC_HANDLE scm, service;
DWORD ret = 0;
if (!(scm = OpenSCManagerW(NULL, SERVICES_ACTIVE_DATABASEW, SC_MANAGER_CONNECT)))
{
fprintf(stderr, "Failed to open service control manager\n");
return 1;
}
if ((service = OpenServiceW(scm, msiserverW, DELETE)))
{
if (!DeleteService(service))
{
fprintf(stderr, "Failed to delete MSI service\n");
ret = 1;
}
CloseServiceHandle(service);
}
else if (GetLastError() != ERROR_SERVICE_DOES_NOT_EXIST)
{
fprintf(stderr, "Failed to open MSI service\n");
ret = 1;
}
CloseServiceHandle(scm);
return ret;
}
示例7: StopService
static BOOL StopService(SC_HANDLE SCManager, SC_HANDLE serviceHandle)
{
LPENUM_SERVICE_STATUSW dependencies = NULL;
DWORD buffer_size = 0;
DWORD count = 0, counter;
BOOL result;
SC_HANDLE dependent_serviceHandle;
SERVICE_STATUS_PROCESS ssp;
result = EnumDependentServicesW(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count);
if(!result && (GetLastError() == ERROR_MORE_DATA))
{
dependencies = HeapAlloc(GetProcessHeap(), 0, buffer_size);
if(EnumDependentServicesW(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count))
{
for(counter = 0; counter < count; counter++)
{
output_string(STRING_STOP_DEP, dependencies[counter].lpDisplayName);
dependent_serviceHandle = OpenServiceW(SCManager, dependencies[counter].lpServiceName, SC_MANAGER_ALL_ACCESS);
if(dependent_serviceHandle) result = StopService(SCManager, dependent_serviceHandle);
CloseServiceHandle(dependent_serviceHandle);
if(!result) output_string(STRING_CANT_STOP, dependencies[counter].lpDisplayName);
}
}
}
if(result) result = ControlService(serviceHandle, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp);
HeapFree(GetProcessHeap(), 0, dependencies);
return result;
}
示例8: cs
BOOL DrvCtrl::LoadWdmInf(WCHAR *inf, WCHAR* szDrvSvcName)
{
WCHAR exe[] = L"c:\\windows\\system32\\InfDefaultInstall.exe ";
STARTUPINFOW si = { 0 };
PROCESS_INFORMATION pi = { 0 };
WCHAR *cmd = cs(exe, inf);
if (!CreateProcessW(NULL, cmd, NULL, NULL, 0, 0, NULL, NULL, &si, &pi))
return FALSE;
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
SC_HANDLE hSCM = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
SC_HANDLE hSvcHandle = OpenServiceW(hSCM, szDrvSvcName, SERVICE_ALL_ACCESS);
if (hSvcHandle)
{
CloseServiceHandle(hSCM);
CloseServiceHandle(hSvcHandle);
return TRUE;
}
else
{
CloseServiceHandle(hSCM);
return FALSE;
}
}
示例9: OpenSCManagerW
void Service::uninstall()
{
// Open service manager
SC_HANDLE hSCM = OpenSCManagerW( nullptr, nullptr,
SC_MANAGER_CREATE_SERVICE );
if ( !hSCM )
EXCEPT_WINAPI( L"Couldn't open service manager" );
// Open service for query + deletion
SC_HANDLE hService = OpenServiceW( hSCM, mName.c_str(),
SERVICE_QUERY_STATUS | DELETE );
if ( !hService )
EXCEPT_WINAPI( L"Couldn't open service" );
// Make sure it's stopped
SERVICE_STATUS stStatus;
if ( !QueryServiceStatus( hService, &stStatus ) )
EXCEPT_WINAPI( L"Couldn't query service status" );
if ( stStatus.dwCurrentState != SERVICE_STOPPED )
EXCEPT( L"Cannot uninstall service while it's running" );
// Delete service
if ( !DeleteService( hService ) )
EXCEPT_WINAPI( L"Couldn't delete service" );
// Done
CloseServiceHandle( hService );
CloseServiceHandle( hSCM );
}
示例10: IsServiceInstalled
/**
* Determines if the specified service exists or not
*
* @param serviceName The name of the service to check
* @param exists Whether or not the service exists
* @return TRUE if there were no errors
*/
static BOOL
IsServiceInstalled(LPCWSTR serviceName, BOOL &exists)
{
exists = FALSE;
// Get a handle to the local computer SCM database with full access rights.
SC_HANDLE serviceManager = OpenSCManager(NULL, NULL,
SC_MANAGER_ENUMERATE_SERVICE);
if (!serviceManager) {
return FALSE;
}
SC_HANDLE serviceHandle = OpenServiceW(serviceManager,
serviceName,
SERVICE_QUERY_CONFIG);
if (!serviceHandle && GetLastError() != ERROR_SERVICE_DOES_NOT_EXIST) {
CloseServiceHandle(serviceManager);
return FALSE;
}
if (serviceHandle) {
CloseServiceHandle(serviceHandle);
exists = TRUE;
}
CloseServiceHandle(serviceManager);
return TRUE;
}
示例11: IsStopService
BOOL IsStopService(LPCWSTR lpcwszName)
{
BOOL bRet = FALSE;
SC_HANDLE hScm = NULL;
SC_HANDLE hSrv = NULL;
hScm = OpenSCManagerW(0, 0, SC_MANAGER_CONNECT);
if(hScm == NULL){
OutputDebugString(_T("OpenSCManager failed"));
return FALSE;
}
hSrv = OpenServiceW(hScm, lpcwszName, SERVICE_QUERY_STATUS);
if(hSrv == NULL){
OutputDebugString(_T("OpenService failed"));
CloseServiceHandle(hScm);
return FALSE;
}
SERVICE_STATUS stStatus;
if( QueryServiceStatus(hSrv, &stStatus) != FALSE ){
if(stStatus.dwCurrentState == SERVICE_STOPPED){
bRet = TRUE;
}
}else{
OutputDebugString(_T("QueryServiceStatus failed"));
}
CloseServiceHandle(hSrv);
CloseServiceHandle(hScm);
return bRet;
}
示例12: GetServiceStatus
DWORD GetServiceStatus(LPCWSTR lpcwszName)
{
SC_HANDLE hScm = NULL;
SC_HANDLE hSrv = NULL;
SERVICE_STATUS stStatus;
hScm = OpenSCManagerW(0, 0, SC_MANAGER_CONNECT);
if(hScm == NULL){
OutputDebugString(_T("OpenSCManager failed"));
return FALSE;
}
hSrv = OpenServiceW(hScm, lpcwszName, SERVICE_QUERY_STATUS);
if(hSrv == NULL){
OutputDebugString(_T("OpenService failed"));
CloseServiceHandle(hScm);
return FALSE;
}
if(QueryServiceStatus(hSrv, &stStatus) == FALSE ){
OutputDebugString(_T("QueryServiceStatus failed"));
CloseServiceHandle(hSrv);
CloseServiceHandle(hScm);
return FALSE;
}
DWORD dwStatus = stStatus.dwCurrentState;
return dwStatus;
}
示例13: SvcInstall
//
// Purpose:
// Installs a service in the SCM database
//
// Parameters:
// None
//
// Return value:
// None
//
BOOL SvcInstall()
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
WCHAR szPath[MAX_PATH];
if( !GetModuleFileNameW( NULL, szPath, MAX_PATH ) )
{
return FALSE;
}
// Get a handle to the SCM database.
schSCManager = OpenSCManager(
NULL, // local computer
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if (NULL == schSCManager)
{
return FALSE;
}
// Create the service
schService = CreateServiceW(
schSCManager, // SCM database
SVCNAME, // name of service
SVCNAME, // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_DEMAND_START, // start type
SERVICE_ERROR_NORMAL, // error control type
szPath, // path to service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password
if (schService == NULL) {
schService = OpenServiceW(schSCManager,SVCNAME,SERVICE_ALL_ACCESS);
if(schService == NULL){
CloseServiceHandle(schSCManager);
return FALSE;
}
}
if(!Install::Install()){
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return FALSE;
}
if(!StartService(schService,0,NULL))
return FALSE;
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return TRUE;
}
示例14: install
//---------------------------------------------------------------------------
Service & Service::install(SC_HANDLE hSCManager)
{
install();
utf8::WideString serviceNameW(serviceName_.getUNICODEString());
utf8::WideString displayNameW(displayName_.getUNICODEString());
utf8::WideString binaryPathNameW((!args_.isNull() ? binaryPathName_ + args_ : binaryPathName_).getUNICODEString());
utf8::WideString loadOrderGroupW(loadOrderGroup_.getUNICODEString());
utf8::WideString dependenciesW(dependencies_.getUNICODEString());
utf8::WideString serviceStartNameW(serviceStartName_.getUNICODEString());
utf8::WideString passwordW(password_.getUNICODEString());
SC_HANDLE handle = CreateServiceW(
hSCManager,
serviceNameW,
displayNameW,
SERVICE_ALL_ACCESS,
serviceType_,
startType_,
errorControl_,
binaryPathNameW,
!loadOrderGroup_.isNull() ? (wchar_t *) loadOrderGroupW : NULL,
(serviceType_ == SERVICE_KERNEL_DRIVER || serviceType_ == SERVICE_FILE_SYSTEM_DRIVER) &&
(startType_ == SERVICE_BOOT_START || startType_ == SERVICE_SYSTEM_START) ?
&tagId_ : NULL,
!dependencies_.isNull() ? (wchar_t *) dependenciesW : NULL,
!serviceStartName_.isNull() ? (wchar_t *) serviceStartNameW : NULL,
!password_.isNull() ? (wchar_t *) passwordW : NULL
);
if( handle == NULL ){
int32_t err = GetLastError();
if( err == ERROR_SERVICE_EXISTS ){
SetLastError(err = ERROR_SUCCESS);
handle = OpenServiceW(hSCManager,serviceNameW,SERVICE_ALL_ACCESS);
if( handle == NULL ||
ChangeServiceConfigW(handle,
serviceType_,
startType_,
errorControl_,
binaryPathNameW,
!loadOrderGroup_.isNull() ? (wchar_t *) loadOrderGroupW : NULL,
(serviceType_ == SERVICE_KERNEL_DRIVER || serviceType_ == SERVICE_FILE_SYSTEM_DRIVER) &&
(startType_ == SERVICE_BOOT_START || startType_ == SERVICE_SYSTEM_START) ?
&tagId_ : NULL,
!dependencies_.isNull() ? (wchar_t *) dependenciesW : NULL,
!serviceStartName_.isNull() ? (wchar_t *) serviceStartNameW : NULL,
!password_.isNull() ? (wchar_t *) passwordW : NULL,
displayNameW
) == 0 ) err = GetLastError();
}
if( err != ERROR_SUCCESS ){
if( handle != NULL ) CloseServiceHandle(handle);
newObjectV1C2<Exception>(err + errorOffset,__PRETTY_FUNCTION__)->throwSP();
}
}
CloseServiceHandle(handle);
return *this;
}
示例15: test_service
static void test_service(void)
{
static const WCHAR spooler[] = {'S','p','o','o','l','e','r',0};
static const WCHAR dummyW[] = {'d','u','m','m','y',0};
SERVICE_STATUS_PROCESS status;
SC_HANDLE scm, service;
IShellDispatch2 *sd;
DWORD dummy;
HRESULT hr;
BSTR name;
VARIANT v;
hr = CoCreateInstance(&CLSID_Shell, NULL, CLSCTX_INPROC_SERVER,
&IID_IShellDispatch2, (void**)&sd);
if (hr != S_OK)
{
win_skip("IShellDispatch2 not supported\n");
return;
}
V_VT(&v) = VT_I2;
V_I2(&v) = 10;
hr = IShellDispatch2_IsServiceRunning(sd, NULL, &v);
ok(V_VT(&v) == VT_BOOL, "got %d\n", V_VT(&v));
ok(V_BOOL(&v) == VARIANT_FALSE, "got %d\n", V_BOOL(&v));
EXPECT_HR(hr, S_OK);
scm = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT);
service = OpenServiceW(scm, spooler, SERVICE_QUERY_STATUS);
QueryServiceStatusEx(service, SC_STATUS_PROCESS_INFO, (BYTE *)&status, sizeof(SERVICE_STATUS_PROCESS), &dummy);
CloseServiceHandle(service);
CloseServiceHandle(scm);
/* service should exist */
name = SysAllocString(spooler);
V_VT(&v) = VT_I2;
hr = IShellDispatch2_IsServiceRunning(sd, name, &v);
EXPECT_HR(hr, S_OK);
ok(V_VT(&v) == VT_BOOL, "got %d\n", V_VT(&v));
if (status.dwCurrentState == SERVICE_RUNNING)
ok(V_BOOL(&v) == VARIANT_TRUE, "got %d\n", V_BOOL(&v));
else
ok(V_BOOL(&v) == VARIANT_FALSE, "got %d\n", V_BOOL(&v));
SysFreeString(name);
/* service doesn't exist */
name = SysAllocString(dummyW);
V_VT(&v) = VT_I2;
hr = IShellDispatch2_IsServiceRunning(sd, name, &v);
EXPECT_HR(hr, S_OK);
ok(V_VT(&v) == VT_BOOL, "got %d\n", V_VT(&v));
ok(V_BOOL(&v) == VARIANT_FALSE, "got %d\n", V_BOOL(&v));
SysFreeString(name);
IShellDispatch2_Release(sd);
}