Ir ao conteúdo
  • Cadastre-se

C++ Parar o serviço DPS e iniciar novamente com o horário de inicio do PC.


corsa

Posts recomendados

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;
}

 

Link para o comentário
Compartilhar em outros sites

  • BCP alterou o título para Parar o serviço DPS e iniciar novamente com o horário de inicio do PC.

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

 

GRÁTIS: ebook Redes Wi-Fi – 2ª Edição

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!