系列文章目录
文章目录
- 系列文章目录
- 前言
- 一、分析出错原因
- 二、解决方法
- 三、完整代码
- 1.头文件
- 2.实现文件:
- 3.测试文件
- 四、源码下载
前言
在windows下编程需要获取管理员权限或者启动、停止某个服务,比如Delivery Optimization服务等,在调试时经常异常提示:
OpenService failed. Error code: 1060. 指定的服务未安装。
一、分析出错原因
可能的原因包括:
使用 OpenService 函数打开服务时,hService 的值为 nullptr,并且抛出了异常,异常信息为:“OpenService failed. Error code: 1060. 指定的服务未安装。”
错误代码 1060 表示 Windows 无法找到指定的服务。这意味着在服务控制管理器中没有注册您试图启动的服务。这通常意味着以下几种情况之一:
服务名称拼写错误:确保您提供的服务名称 _serviceName 是正确的,并且与在服务控制管理器中注册的服务名称完全匹配(包括大小写)。
服务尚未安装:确认服务已经正确安装在目标计算机上。您可以通过服务管理工具 (services.msc) 或者命令行工具 (sc query <service_name>) 来检查服务是否已安装。
服务已被卸载或删除:如果服务曾经安装过但现在找不到,可能是因为它已被卸载或从系统中删除。
服务配置问题:有时候服务可能由于配置问题而不可见。例如,服务可能被配置为隐藏或其他原因。
权限问题:确保您的应用程序有足够的权限来访问服务。如果应用没有以管理员身份运行,可能会遇到权限问题。
为了进一步诊断问题,您可以按照以下步骤操作:
步骤 1: 验证服务名称
确保您使用的服务名称是正确的。您可以手动检查服务名称是否存在于服务列表中。例如,在命令提示符下运行 sc query <service_name> 命令来验证服务是否存在。
步骤 2: 检查服务状态
使用 services.msc 或者 sc query <service_name> 命令来查看服务的状态。
步骤 3: 确认权限
确保您的应用程序具有足够的权限来访问服务。如果需要,以管理员身份运行您的应用程序。
步骤 4: 日志记录
在您的代码中增加更多的日志记录,以便更好地了解问题所在。例如,在 startService 和 stopService 方法中加入更多的日志记录点,以便跟踪每个步骤的状态。
步骤 5: 检查服务安装
确保服务已经被正确安装。您可以尝试重新安装服务,或者查看服务的安装文档以确保遵循了所有必要的步骤。
1.首先:Delivery Optimization服务已经存在,而且我在代码中已经提升了访问权限
2.我在visual studio 2022中也提升了权限
鼠标右键工程,选择”属性“
属性页中选择"连接器"->”清单文件“->启动用户账户控制(UAC),UAC执行级别选择
”requireAdministrator (/level=‘requireAdministrator’)“
二、解决方法
1.在cmd命令行中输入: Get-Service -DisplayName “Delivery Optimization”
发现无法运行这个命令 Get-Service -DisplayName Delivery Optimization
2.在任意文件夹下,按住shift键盘,同时鼠标右键选择:”在此处打开Powershell窗口(S)“
3.输入:Get-Service -DisplayName “Delivery Optimization”
4.发现Delivery Optimization服务的别名是:DoSvc,所以启动服务名不能是”Delivery Optimization“,需要改成
DoSvc
三、完整代码
1.头文件
#pragma once
#include <windows.h>
#include <iostream>
#include <string>
namespace WindowsServiceControl {
class IServicesControl
{
public:
virtual bool isAdmin() = 0;
virtual bool elevatePermissionsAndRun() = 0;
virtual bool startService(const std::wstring& serviceName) = 0;
virtual bool stopService(const std::wstring& serviceName) = 0;
};
}
#pragma once
#include "IServicesControl.h"
using namespace WindowsServiceControl;
namespace WindowsServiceControl {
class microsoftStoreService : public IServicesControl {
public:
explicit microsoftStoreService(const std::wstring& serviceName);
bool isAdmin() override;
bool elevatePermissionsAndRun() override;
bool startService(const std::wstring& serviceName) override;
bool stopService(const std::wstring& serviceName) override;
//getting,setting
std::wstring getServiceName() { return _serviceName; }
void setServiceName(std::wstring& serviceName) { _serviceName = serviceName; }
private:
std::wstring _serviceName;
std::wstring _errorMessage;
private:
std::wstring formatErrorMessage();
std::string wstringToUtf8String(const std::wstring& wstr);
//void AttemptElevatedPrivileges(HANDLE hToken);
};
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
2.实现文件:
#include "microsoftStoreService.h"
#include <tchar.h>
#include <vector>
#include <stdexcept>
#include <sstream>
#include <stdexcept>
#include <shellapi.h>
microsoftStoreService::microsoftStoreService(const std::wstring& serviceName) : _serviceName(serviceName)
{
}
bool microsoftStoreService::isAdmin()
{
HANDLE hToken = nullptr;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
DWORD lastError = GetLastError();
formatErrorMessage();
throw std::runtime_error("Failed to open process token. Error code: " + std::to_string(lastError));
}
TOKEN_ELEVATION elevation;
DWORD cbSize = sizeof(TOKEN_ELEVATION);
if (!GetTokenInformation(hToken, TokenElevation, &elevation, sizeof(elevation), &cbSize)) {
CloseHandle(hToken);
DWORD lastError = GetLastError();
formatErrorMessage();
throw std::runtime_error("Failed to get token information. Error code: " + std::to_string(lastError));
}
CloseHandle(hToken);
if (elevation.TokenIsElevated == 0) {
formatErrorMessage();
throw std::runtime_error("The process is not running with administrator permissions.");
}
return true;
}
bool microsoftStoreService::elevatePermissionsAndRun()
{
#if 0
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
TCHAR szCmdLine[MAX_PATH];
TCHAR szAppPath[MAX_PATH];
// 获取可执行文件的路径
GetModuleFileName(NULL, szAppPath, MAX_PATH);
_sntprintf_s(szCmdLine, MAX_PATH, _T("\"%s\" %s"), szAppPath, GetCommandLine());
// 获取当前用户的句柄
HANDLE hToken = nullptr;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
DWORD lastError = GetLastError();
formatErrorMessage();
throw std::runtime_error("Failed to open process token. Error code: " + std::to_string(lastError));
}
lstrcpy(szAppPath, L"C:/Users/aoxue/AppData/Local/Temp/getadmin.vbs"); //getadmin.vbs regedit.exe
// Attempt to launch the application with elevated privileges using the user token
if (!CreateProcessAsUser(hToken, szAppPath, GetCommandLine(), nullptr, nullptr, FALSE,
CREATE_NEW_CONSOLE, // 注意这里只保留了一个标志
nullptr, nullptr, &si, &pi)) {
DWORD lastError = GetLastError();
CloseHandle(hToken);
formatErrorMessage();
throw std::runtime_error("Failed to create process with elevated privileges. Error code: " + std::to_string(lastError));
}
// Wait for the process to finish
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
CloseHandle(hToken);
return true;
#endif 0
#if 1
TCHAR szCmdLine[MAX_PATH];
TCHAR szAppPath[MAX_PATH];
// 获取可执行文件的路径
GetModuleFileName(NULL, szAppPath, MAX_PATH);
_sntprintf_s(szCmdLine, MAX_PATH, _T("\"%s\" %s"), szAppPath, GetCommandLine());
// 使用 ShellExecute 启动新进程
HWND hwnd = GetConsoleWindow(); // 如果程序是在控制台环境下运行,则获取控制台窗口句柄
if (hwnd == NULL) {
hwnd = FindWindow(NULL, _T("Your Application Title")); // 如果程序没有控制台窗口,则获取程序的主窗口句柄
}
// 添加调试测试输出
std::wcout << L"Attempting to elevate with command line: " << szCmdLine << std::endl;
lstrcpy(szAppPath, L"C:/Users/aoxue/AppData/Local/Temp/getadmin.vbs"); //getadmin.vbs regedit.exe
try {
HINSTANCE hInstance = ShellExecute(hwnd, _T("runas"), szAppPath, nullptr, nullptr, SW_SHOW);
//HINSTANCE hInstance = ShellExecute(NULL, _T("open"), _T("calc.exe"), NULL, NULL, SW_SHOWNORMAL);
if (hInstance == NULL) {
DWORD lastError = GetLastError();
formatErrorMessage();
throw std::runtime_error("Failed to elevate privileges and run the application. Error code: " + std::to_string(lastError));
}
// 退出当前进程
//ExitProcess(0);
}
catch (const std::runtime_error& e) {
std::cerr << "An error occurred:" << e.what() << std::endl;
}
#endif 1
return true;
}
bool microsoftStoreService::startService(const std::wstring& serviceName)
{
SC_HANDLE hSCManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE);
if (hSCManager == nullptr) {
DWORD lastError = GetLastError();
formatErrorMessage();
throw std::runtime_error("OpenSCManager failed. Error code: " + std::to_string(lastError));
}
SC_HANDLE hService = OpenService(hSCManager, serviceName.c_str(), SERVICE_START | SERVICE_QUERY_STATUS);
if (hService == nullptr) {
CloseServiceHandle(hSCManager);
DWORD lastError = GetLastError();
formatErrorMessage();
throw std::runtime_error("OpenService failed. Error code: " + std::to_string(lastError));
}
// Check if the service is already running
SERVICE_STATUS_PROCESS ssp;
DWORD bytesNeeded;
if (!QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, reinterpret_cast<LPBYTE>(&ssp), sizeof(ssp), &bytesNeeded)) {
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
DWORD lastError = GetLastError();
formatErrorMessage();
throw std::runtime_error("QueryServiceStatusEx failed. Error code: " + std::to_string(lastError));
}
// If the service is already running, do nothing
if (ssp.dwCurrentState == SERVICE_RUNNING) {
std::wcout << L"The service is already running." << std::endl;
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return true;
}
// Send start command
if (!StartService(hService, 0, nullptr)) {
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
DWORD lastError = GetLastError();
formatErrorMessage();
throw std::runtime_error("StartService failed. Error code: " + std::to_string(lastError));
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
std::wcout << L"Service started successfully." << std::endl;
return true;
}
bool microsoftStoreService::stopService(const std::wstring& serviceName)
{
SC_HANDLE hSCManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE);
if (hSCManager == nullptr) {
DWORD lastError = GetLastError();
throw std::runtime_error("OpenSCManager failed. Error code: " + std::to_string(lastError));
}
SC_HANDLE hService = OpenService(hSCManager, serviceName.c_str(), SERVICE_STOP | SERVICE_QUERY_STATUS);
if (hService == nullptr) {
CloseServiceHandle(hSCManager);
DWORD lastError = GetLastError();
formatErrorMessage();
throw std::runtime_error("OpenService failed. Error code: " + std::to_string(lastError));
}
SERVICE_STATUS_PROCESS ssStatus;
DWORD dwBytesNeeded;
// Check if the service is already stopped
if (!QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssStatus, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded)) {
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
DWORD lastError = GetLastError();
formatErrorMessage();
throw std::runtime_error("QueryServiceStatusEx failed. Error code: " + std::to_string(lastError));
}
// If the service is already stopped, do nothing
if (ssStatus.dwCurrentState == SERVICE_STOPPED) {
std::wcout << L"The service is already stopped." << std::endl;
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return true;
}
// Send stop command
if (!ControlService(hService, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssStatus)) {
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
DWORD lastError = GetLastError();
formatErrorMessage();
throw std::runtime_error("ControlService failed. Error code: " + std::to_string(lastError));
}
// Wait for the service to stop
while (QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssStatus, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded)) {
if (ssStatus.dwCurrentState == SERVICE_STOPPED)
break;
Sleep(ssStatus.dwWaitHint);
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
std::wcout << L"Service stopped successfully." << std::endl;
return true;
}
// Helper function to convert wide string to UTF-8 string
std::string microsoftStoreService::wstringToUtf8String(const std::wstring& wstr)
{
if (wstr.empty()) return std::string();
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo(size_needed, '\0');
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}
std::wstring microsoftStoreService::formatErrorMessage()
{
std::wstring errorMessage;
wchar_t buffer[1024] = L"";
DWORD lastError = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, lastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buffer, 1024, nullptr);
errorMessage = L"OpenService failed. Error code: " + std::to_wstring(lastError) + L". " + buffer;
//std::string utf8Str = wideToUtf8(errorMessage);
std::wcout << errorMessage << std::endl;
throw std::runtime_error(wstringToUtf8String(errorMessage));
}
//void ServicesControl::AttemptElevatedPrivileges(HANDLE hToken)
//{
// if (hToken == nullptr || hToken == INVALID_HANDLE_VALUE) {
// // 处理无效句柄
// return;
// }
//
// TOKEN_PRIVILEGES tokenPrivileges;
// tokenPrivileges.PrivilegeCount = 2;
//
// // 获取特权标识符
// if (!LookupPrivilegeValue(NULL, SE_ASSIGNPRIMARYTOKEN_NAME, &tokenPrivileges.Privileges[0].Luid) ||
// !LookupPrivilegeValue(NULL, SE_INCREASE_QUOTA_NAME, &tokenPrivileges.Privileges[1].Luid)) {
// // 处理错误
// return;
// }
//
// // 设置特权
// tokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// tokenPrivileges.Privileges[1].Attributes = SE_PRIVILEGE_ENABLED;
//
// // 尝试启用特权
// if (!AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges, 0, NULL, NULL)) {
// // 处理错误
// return;
// }
//
// // 注意:通常不需要在这里关闭 hToken,因为它可能是由调用者管理的
// // 如果确实需要关闭它(例如,如果它在这里被打开),则应该添加 CloseHandle 调用
// // 但在这个上下文中,我们假设它是外部管理的
//}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.
- 235.
- 236.
- 237.
- 238.
- 239.
- 240.
- 241.
- 242.
- 243.
- 244.
- 245.
- 246.
- 247.
- 248.
- 249.
- 250.
- 251.
- 252.
- 253.
- 254.
- 255.
- 256.
- 257.
- 258.
- 259.
- 260.
- 261.
- 262.
- 263.
- 264.
- 265.
- 266.
- 267.
- 268.
- 269.
- 270.
- 271.
- 272.
- 273.
- 274.
- 275.
- 276.
- 277.
- 278.
- 279.
- 280.
- 281.
- 282.
- 283.
- 284.
- 285.
- 286.
- 287.
- 288.
- 289.
- 290.
- 291.
- 292.
- 293.
- 294.
- 295.
- 296.
- 297.
- 298.
- 299.
- 300.
3.测试文件
#include "microsoftStoreService.h" // 包含 ServicesControl 类的定义
#include <memory>
#include <io.h>
#include <fcntl.h>
using namespace WindowsServiceControl;
std::string utf8ToGbk(const std::string& utf8Str)
{
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &utf8Str[0], (int)utf8Str.size(), NULL, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &utf8Str[0], (int)utf8Str.size(), &wstrTo[0], size_needed);
int size_needed_gbk = WideCharToMultiByte(CP_ACP, 0, &wstrTo[0], -1, NULL, 0, NULL, NULL);
std::string strTo(size_needed_gbk, 0);
WideCharToMultiByte(CP_ACP, 0, &wstrTo[0], -1, &strTo[0], size_needed_gbk, NULL, NULL);
return strTo;
}
int main()
{
try {
std::wcout.imbue(std::locale("chs"));
std::wcout << "控制台输出中文" << std::endl;
auto svcCtrl = std::make_shared<WindowsServiceControl::microsoftStoreService>(L"Delivery Optimization");
// 检查是否已具有管理员权限
if (svcCtrl->isAdmin()) {
std::cout << "Running with administrator permissions.\n";
//提升权限
svcCtrl->elevatePermissionsAndRun();
// 启动服务 DoSvc : Delivery Optimization,注意Delivery Optimization对应的服务名称是DoSvc
svcCtrl->startService(L"Delivery Optimization");
std::cout << "Service started successfully.\n";
// 停止服务
svcCtrl->stopService(L"Delivery Optimization");
std::cout << "Service stopped successfully.\n";
}
else {
// 如果没有管理员权限,尝试提升权限
svcCtrl->elevatePermissionsAndRun();
}
}
catch (const std::runtime_error& e) {
std::string gbkStr = utf8ToGbk(e.what());
std::cerr << "An error occurred: " << gbkStr << std::endl;
return 1;
}
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
四、源码下载
源码下载地址