Meu código não está funcionando. Se alguém souber conserta-lo iria ajudar muito.
c#include <iostream>
#include <windows.h>
#include <winsvc.h>
#include <sstream>
// Função personalizada para converter um valor inteiro em uma string wide
std::wstring intToWideString(int value) {
std::wstringstream ss;
ss << value;
return ss.str();
}
int main() {
// Abra o serviço DPS
SC_HANDLE scManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (scManager == NULL) {
std::cerr << "Falha ao abrir o Gerenciador de Controle de Serviços." << std::endl;
return 1;
}
SC_HANDLE service = OpenServiceW(scManager, L"DPS", SERVICE_ALL_ACCESS);
if (service == NULL) {
std::cerr << "Falha ao abrir o serviço DPS." << std::endl;
CloseServiceHandle(scManager);
return 1;
}
// Pare o serviço DPS
SERVICE_STATUS serviceStatus;
if (!ControlService(service, SERVICE_CONTROL_STOP, &serviceStatus)) {
std::cerr << "Falha ao parar o serviço DPS." << std::endl;
CloseServiceHandle(service);
CloseServiceHandle(scManager);
return 1;
}
// Aguarde até que o serviço DPS pare completamente
while (serviceStatus.dwCurrentState != SERVICE_STOPPED) {
if (!QueryServiceStatus(service, &serviceStatus)) {
std::cerr << "Falha ao obter o status do serviço DPS." << std::endl;
CloseServiceHandle(service);
CloseServiceHandle(scManager);
return 1;
}
Sleep(1000);
}
// Obter o horário de início do sistema
SYSTEMTIME systemTime;
GetSystemTime(&systemTime);
// Converter os valores numéricos em strings wide usando a função personalizada
std::wstring startTime = L"!" +
intToWideString(systemTime.wYear) + L"/" +
intToWideString(systemTime.wMonth) + L"/" +
intToWideString(systemTime.wDay) + L":" +
intToWideString(systemTime.wHour) + L":" +
intToWideString(systemTime.wMinute) + L":" +
intToWideString(systemTime.wSecond) + L"!";
// Alterar a configuração do serviço DPS para usar o mesmo horário de início do sistema
if (!ChangeServiceConfigW(service, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, startTime.c_str())) {
std::cerr << "Falha ao alterar a configuração do serviço DPS: " << GetLastError() << std::endl;
CloseServiceHandle(service);
CloseServiceHandle(scManager);
return 1;
}
// Iniciar o serviço DPS
if (!StartService(service, 0, NULL)) {
std::cerr << "Falha ao iniciar o serviço DPS: " << GetLastError() << std::endl;
CloseServiceHandle(service);
CloseServiceHandle(scManager);
return 1;
}
std::cout << "O serviço DPS foi iniciado com sucesso." << std::endl;
// Fechar as alças de serviço
CloseServiceHandle(service);
CloseServiceHandle(scManager);
return 0;
}