局域网远程关机程序

局域网远程关机程序帮朋友写的一个小程序,抄了一些网上大神的代码,加上自己的代码。控制端:main.c#include”shutdown.h”#include#include#includeintmain(intargc,char*argv[]){QApplicationa(argc,argv);QSound::play(“music.w

大家好,又见面了,我是你们的朋友全栈君。

帮朋友写的一个远程关机的小程序,参考一些网上大神的代码,加上自己的代码。

局域网远程关机程序

控制端:main.c

#include "shutdown.h"
#include <QApplication>
#include <QSound>
#include <QPalette>

int main(int argc, char *argv[])
{
  
  
    QApplication a(argc, argv);
    QSound::play("music.wav");
    shutdown w;
    w.setFixedSize(280,260);
    w.setWindowTitle(QObject::tr("关机程序"));

    w.setWindowFlags(Qt::WindowCloseButtonHint);
    w.setWindowOpacity(1);
    w.setAutoFillBackground(true);

    QPalette palette;
    palette.setBrush(QPalette::WindowText, Qt::red);
    palette.setBrush(QPalette::ButtonText, Qt::red);
    palette.setBrush(QPalette::Button, Qt::red);
    w.setPalette(palette);
    w.show();
    return a.exec();
}

shutdown.cpp

#include "shutdown.h"
#include "ui_shutdown.h"
#include <QDebug>
#include <QTimer>
#include <QTime>
#include <QDateTime>
#include <QLCDNumber>
#include <QMessageBox>
#include <QPalette>
#include <windows.h>
#include <QInputDialog>
#include <QByteArray>
#include <QDebug>
int shutdownTime = 0;
int second = -1;
shutdown::shutdown(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::shutdown)
{
  
  
    ui->setupUi(this);
    setAutoFillBackground(true);
    QPalette p = QPalette();
    p.setColor(QPalette::WindowText , Qt::red);    //设置某一个控件颜色的方法
    ui->lcdNumber->setPalette(p);

    QTimer *timer = new QTimer(this);   //新建一个定时器
    connect(timer,SIGNAL(timeout()),this,SLOT(showTime())); //信号与槽,时间到便触发
    connect(timer,SIGNAL(timeout()),this,SLOT(showCountDownTime()));    //倒计时的信号与槽
    connect(ui->setShutDownbt,SIGNAL(clicked(bool)),this,SLOT(showCountDownTime())); //倒计时信号与槽
    connect(timer,SIGNAL(timeout()),this,SIGNAL(showCountDownTime));
    timer->start(1000); //一秒刷新一次
    showTime(); //显示时间
    showCountDownTime();
    showColon=true; //显示分号
    ui->shutDownTime->setText(tr("      尚未设置")) ; //初始化关机时间lebel
    sender = new QUdpSocket(this);
    receiver = new QUdpSocket(this);
    receiver->bind(QHostAddress::LocalHost, 6665);
    connect(receiver, SIGNAL(readyRead()),this, SLOT(readPendingDatagrams()));

}

shutdown::~shutdown()
{
  
  
    delete ui;
}

void shutdown::readPendingDatagrams()
 {
  
  
    qDebug()<<"111122";
     while (receiver->hasPendingDatagrams())
     {
  
  
         QByteArray datagram;
         datagram.resize(receiver->pendingDatagramSize());
         receiver->readDatagram(datagram.data(), datagram.size());
          qDebug()<<datagram.data();
         //数据接收在datagram里
/* readDatagram 函数原型
qint64 readDatagram(char *data,qint64 maxSize,QHostAddress *address=0,quint16 *port=0)
*/
     }
 }
void shutdown::showTime()//显示系统时间
{
  
  
    QTime time = QTime::currentTime();
    QString text = time.toString("hh:mm:ss");
    if(showColon)
    {
  
  
        text[2]=':';
        text[5]=':';
        showColon=false;
    }
    else
    {
  
  
        text[2]=' ';
        text[5]=' ';
        showColon=true;
    }
    ui->lcdNumber->display(text);
}

void shutdown::showCountDownTime()
{
  
  
    if(second == -1)
    {
  
  
          ui->QlcdTime->display("00:00");
    }
    else
    {
  
  
        if(second == 0)
        {
  
  
             on_shutdownButton_clicked();
        }
        QString str;
        str.sprintf("%02d:%02d",second/60,second%60);
        ui->QlcdTime->display( str );
        second--;
    }
}

void shutdown::on_shutdownButton_clicked()
{
  
  
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
    {
  
  
        return ;
    }
    //获取关机特权的LUID
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,    &tkp.Privileges[0].Luid);
    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    //获取这个进程的关机特权
    AdjustTokenPrivileges(hToken, false, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
    if (GetLastError() != ERROR_SUCCESS)
    {
  
  
            return ;
    }
     QByteArray datagram = "1";

    //UDP广播
    sender->writeDatagram(datagram.data(),datagram.size(),QHostAddress::Broadcast,6665);
    //向特定IP发送
    QHostAddress serverAddress = QHostAddress(QHostAddress::LocalHost);
    sender->writeDatagram(datagram.data(), datagram.size(),serverAddress, 6665);
    ui->shutDownTime->setText(tr("      尚未设置")) ; //初始化关机时间lebel
    // 强制关闭计算机
//    if ( !ExitWindowsEx(EWX_SHUTDOWN , 0))  //关机
//    {
  
  
//        return ;
 //   }
}

void shutdown::on_setShutDownbt_clicked()
{
  
  
     bool ok;
     shutdownTime = QInputDialog::getInt(this,
                                             tr("输入关机时间"),
                                             tr("请输入关机时间"),
                                             ui->shutDownTime->text().toInt(&ok),
                                             0,
                                             60,
                                             1,
                                             &ok);
     if(ok)
     {
  
  
         ui->shutDownTime->setText(QString(tr(" %1 分钟后关机")).arg(shutdownTime));
         second = shutdownTime * 60;
     }
     else
     {
  
  
         return ;
     }
}

void shutdown::on_reboot_clicked()
{
  
  
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    //获取进程标志
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
    {
  
  
        return ;
    }
    //获取关机特权的LUID
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,    &tkp.Privileges[0].Luid);
    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    //获取这个进程的关机特权
    AdjustTokenPrivileges(hToken, false, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
    if (GetLastError() != ERROR_SUCCESS)
    {
  
  
            return ;
    }
    QByteArray datagram = "2";
    //UDP广播
    sender->writeDatagram(datagram.data(),datagram.size(),QHostAddress::Broadcast,6665);
    //向特定IP发送
    QHostAddress serverAddress = QHostAddress(QHostAddress::LocalHost);
    sender->writeDatagram(datagram.data(), datagram.size(),serverAddress, 6665);
#if 0
    // 强制关闭计算机
    if ( !ExitWindowsEx(EWX_REBOOT  , 0))   //重启
    {
  
  
        return ;
    }
#endif
}

void shutdown::on_logoutButton_clicked()
{
  
  
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    //获取进程标志
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
    {
  
  
         return ;
    }
    //获取关机特权的LUID
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,    &tkp.Privileges[0].Luid);
    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    //获取这个进程的关机特权
    AdjustTokenPrivileges(hToken, false, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
    if (GetLastError() != ERROR_SUCCESS)
    {
  
  
        return ;
    }
    QByteArray datagram = "3";
    //UDP广播
    sender->writeDatagram(datagram.data(),datagram.size(),QHostAddress::Broadcast,6665);
    //向特定IP发送
    QHostAddress serverAddress = QHostAddress(QHostAddress::LocalHost);
    sender->writeDatagram(datagram.data(), datagram.size(),serverAddress, 6665);
#if 0
    if ( !ExitWindowsEx(EWX_LOGOFF  , 0))   //注销
    {
  
  
        return ;
    }
#endif
}


void shutdown::on_exitButtion_clicked()
{
  
  
    QMessageBox msgBox;
    msgBox.setWindowModality(Qt::ApplicationModal); //设置此消息框为模式对话框

    msgBox.setIcon(QMessageBox::Warning);   //设置对话框的显示图标
    msgBox.setText("真的要退出程序吗?");
    msgBox.setInformativeText("怎么会狠心伤害我?");
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::Yes);

    int ret = msgBox.exec();

    switch (ret)
    {
  
  
      case QMessageBox::Yes :
          close();    //退出程序
          break;
      case QMessageBox::No :
          return;   //返回主程序
          break;
    }
}

void shutdown::on_pushButtonSend_clicked()
{
  
  
 #if 0
    //UDP广播
     sender->writeDatagram(datagram.data(),datagram.size(),QHostAddress::Broadcast,6665);
     //向特定IP发送
     QHostAddress serverAddress = QHostAddress(QHostAddress::LocalHost);
     sender->writeDatagram(datagram.data(), datagram.size(),serverAddress, 6665);
     /* writeDatagram函数原型,发送成功返回字节数,否则-1
     qint64 writeDatagram(const char *data,qint64 size,const QHostAddress &address,quint16 port)
     qint64 writeDatagram(const QByteArray &datagram,const QHostAddress &host,quint16 port)
     */
 #endif
}

shutdown.h

#ifndef SHUTDOWN_H
#define SHUTDOWN_H

#include <QDialog>
#include <QtNetwork>
namespace Ui {
  
  
class shutdown;
}

class shutdown : public QDialog
{
  
  
    Q_OBJECT

public:
    explicit shutdown(QWidget *parent = 0);
    ~shutdown();

private slots:
    void showTime();
    void showCountDownTime();
    void on_shutdownButton_clicked();

    void on_setShutDownbt_clicked();

    void on_reboot_clicked();

    void on_logoutButton_clicked();

    void on_exitButtion_clicked();

    void on_pushButtonSend_clicked();

    void readPendingDatagrams();

private:
    bool showColon;
    QUdpSocket *sender;
    QUdpSocket *receiver;


private:
    Ui::shutdown *ui;
};

#endif // SHUTDOWN_H

局域网远程关机程序

受控端:

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
  
  
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    w.hide();
    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QTime>
#include <QDateTime>
#include <QLCDNumber>
#include <QMessageBox>
#include <QPalette>
#include <windows.h>
#include <QInputDialog>
#include <QByteArray>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
  
  
    ui->setupUi(this);
     receiver = new QUdpSocket(this);
     receiver->bind(QHostAddress::LocalHost, 6665);
     connect(receiver, SIGNAL(readyRead()),this, SLOT(readPendingDatagrams()));
}

MainWindow::~MainWindow()
{
  
  
    delete ui;
}

void on_shutdown()
{
  
  
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
    {
  
  
        return ;
    }
    //获取关机特权的LUID
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,    &tkp.Privileges[0].Luid);
    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    //获取这个进程的关机特权
    AdjustTokenPrivileges(hToken, false, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
    if (GetLastError() != ERROR_SUCCESS)
    {
  
  
            return ;
    }
#ifndef DEBUG
    // 强制关闭计算机
    if ( !ExitWindowsEx(EWX_SHUTDOWN , 0))  //关机
    {
  
  
        return ;
    }
#endif
}

void reboot()
{
  
  
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    //获取进程标志
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
    {
  
  
      return ;
    }
    //获取关机特权的LUID
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,    &tkp.Privileges[0].Luid);
    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    //获取这个进程的关机特权
    AdjustTokenPrivileges(hToken, false, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
    if (GetLastError() != ERROR_SUCCESS)
    {
  
  
       return ;
    }
#ifndef DEBUG
    // 强制关闭计算机
    if ( !ExitWindowsEx(EWX_REBOOT  , 0))   //重启
    {
  
  
      return ;
    }
#endif
}


void logout()
{
  
  
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    //获取进程标志
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
    {
  
  
         return ;
    }
    //获取关机特权的LUID
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,    &tkp.Privileges[0].Luid);
    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    //获取这个进程的关机特权
    AdjustTokenPrivileges(hToken, false, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
    if (GetLastError() != ERROR_SUCCESS)
    {
  
  
        return ;
    }
#ifndef DEBUG
    if ( !ExitWindowsEx(EWX_LOGOFF  , 0))   //注销
    {
  
  
        return ;
    }
#endif
}
void MainWindow::readPendingDatagrams()
{
  
  
     while (receiver->hasPendingDatagrams())
     {
  
  
         QByteArray datagram;
         datagram.resize(receiver->pendingDatagramSize());
         receiver->readDatagram(datagram.data(), datagram.size());
         //qDebug()<<"222";
         qDebug()<<datagram.toInt();
         switch(datagram.toInt())
         {
  
  
            case 1:on_shutdown();break; //关机
            case 2:reboot();break;
            case 3:logout();break;
            default:break;


         }
     }
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtNetwork>
#include <QDebug>
namespace Ui {
  
  
class MainWindow;
}

class MainWindow : public QMainWindow
{
  
  
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    QUdpSocket *receiver;
    //信号槽
private slots:
    void readPendingDatagrams();
private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/162989.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • intellij idea 全局搜索_idea设置全局搜索

    intellij idea 全局搜索_idea设置全局搜索IntelliJIDEA使用教程(总目录篇)我们用Eclipse或者IntelliJIDEA编程,有时候需要将整个项目的某个字符串替换成其他的。全局搜索我会,我还给调成ctrl+g了呢,但是遇到要全局(整个项目)替换字符串。哎哟,我有点蒙了。这不换了编辑器吗。我用的是eclipse的keymap而且电脑又不是mac。那么问题来啦。怎么找快捷键呢。如下;额,顺便说下…

    2022年9月27日
    7
  • mysql yuancheng nv_GPU前沿:NVLink与PCIe的对比学习

    mysql yuancheng nv_GPU前沿:NVLink与PCIe的对比学习本文主要是对 PumpUptheVol ProcessingLa 的阅读 同时记录了自己的一些想法 本文针对 GPU 的 NVLink 进行进一步研究 阅读下来感觉非常前沿 能学习很多 insight 所以我将本文总结出来 加深自己的理解 也方便读者阅读 本文为 SIGMOD 20 的文章 感兴趣的同学可以下载来自行

    2025年11月28日
    6
  • 【C++】volatile关键字的作用「建议收藏」

    【C++】volatile关键字的作用「建议收藏」volatile的作用volatile关键字是防止在共享的空间发生读取的错误。只保证其可见性,不保证原子性;使用volatile指每次从内存中读取数据,而不是从编译器优化后的缓存中读取数据,简单来讲就是防止编译器优化。在单任务环境中,如果在两次读取变量之间不改变变量的值,编译器就会发生优化,会将RAM中的值赋值到寄存器中;由于访问寄存器的效率要高于RAM,所以在需要读取变量时,直接寄存器中…

    2022年5月31日
    35
  • phpspreadsheet中文手册_php打开文件

    phpspreadsheet中文手册_php打开文件本文介绍PhpSpreadsheet读写excel文件的一些使用方法。

    2025年12月13日
    5
  • Python 安装 【Pycharm interpreter field is empty(解释器为空)】

    Python 安装 【Pycharm interpreter field is empty(解释器为空)】原因:未安装Python引起,解决办法:直接安装Anaconda即可。Anaconda是一个基于Python的数据处理和科学计算平台,它已经内置了许多非常有用的第三方库,装上Anaconda,就相当于把Python和一些如Numpy、Pandas、Scrip、Matplotlib等常用的库自动安装好了,使得安装比常规Python安装要容易。1.Anaconda官方下载…

    2022年8月29日
    6
  • 毫米波雷达跟激光雷达_毫米波雷达市场

    毫米波雷达跟激光雷达_毫米波雷达市场文章目录激光雷达超声波雷达摄像头毫米波雷达激光雷达激光雷达的波长介于750nm-950nm之间,以单线或多线束机制辐射光束,接收目标或环境的反射信号,以回波时间差和波束指向测量目标的距离和角度等空间位置参数。激光雷达主要优点如下:(1)波长短,测量精度高(2)多线束的探测,可以实现对场景的三维成像。激光雷达的主要缺点是:(1)抗干扰能力低,易受天气影响,在雨雪雾等天气的作用下,激光雷达使用受限。(2)激光发射、被测目标表面粗糙等因素都对测量精度有影响。(3)结构复杂,除激光

    2025年10月27日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号