怎样用编程的方法操作杀毒软件
来源:互联网 发布:武汉电视台网络直播 编辑:程序博客网 时间:2024/06/05 12:50
本文以金山毒霸为例将介绍怎样完全关闭和开启杀毒软件及其服务。
主要运用的技术:注册表的编程,进程的操作。
废话少说,看代码。(以下程序为无窗口的应用程序,运行完毕即关闭)
#include <windows.h>
#include <tlhelp32.h>
#include <string.h>
//打开金山毒霸(网镖)及其服务
int OpenKing()
...{
int nKeyValue;//注册表键值
int flag=0;
int kr;//注册表函数结果
HKEY hKey1,hKey2;//定义有关的hKey,在查询结束时要关闭
//操作注册表将金山毒霸(网镖)服务该为自动
kr=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SYSTEM/CurrentControlSet/Services/KPfwSvc",
0,
KEY_ALL_ACCESS,&hKey1);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法打开有关的键!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKey1);
return 0;
}
kr=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SYSTEM/CurrentControlSet/Services/KWatchSvc",
0,
KEY_ALL_ACCESS,&hKey2);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法打开有关的键!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKey2);
return 0;
}
nKeyValue=2;
kr=RegSetValueEx(hKey1,"Start",0,REG_DWORD,(const unsigned char *)&nKeyValue,4);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法修改有关的键值!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKey1);
return 0;
}
kr=RegSetValueEx(hKey2,"Start",0,REG_DWORD,(const unsigned char *)&nKeyValue,4);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法修改有关的键值!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKey2);
return 0;
}
RegCloseKey(hKey1);
RegCloseKey(hKey2);
//调用命令行工具打开金山毒霸(网镖)服务
WinExec("cmd /c net start KWatchSvc",SW_HIDE);
Sleep(2000);//延迟两秒,让机器不要太忙
WinExec("cmd /c net start KPfwSvc",SW_HIDE);
Sleep(2000);//延迟两秒,让机器不要太忙
//打开金山毒霸(网镖)程序
WinExec("C:/KAV2006/KPFW32.exe",SW_HIDE);
Sleep(2000);//延迟两秒,让机器不要太忙
WinExec("C:/KAV2006/KAVStart.exe",SW_HIDE);
return 1;
}
//关闭金山毒霸(网镖)及其服务
int CloseKing()
...{
int nKeyValue;//注册表键值
int flag=0;
int kr;//注册表函数结果
HKEY hKey1,hKey2,hKeyDel;//定义有关的hKey,在查询结束时要关闭
PROCESSENTRY32 pro;
//关闭金山毒霸(网镖)程序
HANDLE h=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,NULL);
HANDLE th=NULL;
flag=Process32First(h,&pro);
while(flag)
...{
if(strcmp(pro.szExeFile,"KAVStart.EXE")==0||//-startup
strcmp(pro.szExeFile,"KPFW32.EXE")==0)
...{
//MessageBox(NULL,pro.szExeFile,"OK",MB_OK);
th=OpenProcess(PROCESS_ALL_ACCESS,TRUE,pro.th32ProcessID);
TerminateProcess(th,0);
}
flag=Process32Next(h,&pro);
}
CloseHandle(h);
//关闭金山毒霸服务
WinExec("cmd /c net stop KWatchSvc",SW_HIDE);
WinExec("cmd /c net stop KPfwSvc",SW_HIDE);
//操作注册表将金山毒霸(网镖)服务该为禁用
kr=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SYSTEM/CurrentControlSet/Services/KPfwSvc",
0,
KEY_ALL_ACCESS,&hKey1);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法打开有关的键!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKey1);
return 0;
}
kr=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SYSTEM/CurrentControlSet/Services/KWatchSvc",
0,
KEY_ALL_ACCESS,&hKey2);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法打开有关的键!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKey2);
return 0;
}
nKeyValue=4;
kr=RegSetValueEx(hKey1,"Start",0,REG_DWORD,(const unsigned char *)&nKeyValue,4);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法修改有关的键值!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKey1);
return 0;
}
kr=RegSetValueEx(hKey2,"Start",0,REG_DWORD,(const unsigned char *)&nKeyValue,4);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法修改有关的键值!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKey2);
return 0;
}
RegCloseKey(hKey1);
RegCloseKey(hKey2);
//清除启动加载项
kr=RegOpenKeyEx(HKEY_CURRENT_USER,
"SoftWare/Microsoft/Windows/CurrentVersion/Run",
0,
KEY_ALL_ACCESS,&hKeyDel);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法打开有关的键!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKeyDel);
return 0;
}
RegDeleteValue(hKeyDel,"KavPFW");
RegDeleteValue(hKeyDel,"KAVStart");
RegCloseKey(hKeyDel);
return 1;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
...{
//OpenKing();
CloseKing();
return 0;
}
#include <tlhelp32.h>
#include <string.h>
//打开金山毒霸(网镖)及其服务
int OpenKing()
...{
int nKeyValue;//注册表键值
int flag=0;
int kr;//注册表函数结果
HKEY hKey1,hKey2;//定义有关的hKey,在查询结束时要关闭
//操作注册表将金山毒霸(网镖)服务该为自动
kr=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SYSTEM/CurrentControlSet/Services/KPfwSvc",
0,
KEY_ALL_ACCESS,&hKey1);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法打开有关的键!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKey1);
return 0;
}
kr=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SYSTEM/CurrentControlSet/Services/KWatchSvc",
0,
KEY_ALL_ACCESS,&hKey2);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法打开有关的键!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKey2);
return 0;
}
nKeyValue=2;
kr=RegSetValueEx(hKey1,"Start",0,REG_DWORD,(const unsigned char *)&nKeyValue,4);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法修改有关的键值!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKey1);
return 0;
}
kr=RegSetValueEx(hKey2,"Start",0,REG_DWORD,(const unsigned char *)&nKeyValue,4);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法修改有关的键值!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKey2);
return 0;
}
RegCloseKey(hKey1);
RegCloseKey(hKey2);
//调用命令行工具打开金山毒霸(网镖)服务
WinExec("cmd /c net start KWatchSvc",SW_HIDE);
Sleep(2000);//延迟两秒,让机器不要太忙
WinExec("cmd /c net start KPfwSvc",SW_HIDE);
Sleep(2000);//延迟两秒,让机器不要太忙
//打开金山毒霸(网镖)程序
WinExec("C:/KAV2006/KPFW32.exe",SW_HIDE);
Sleep(2000);//延迟两秒,让机器不要太忙
WinExec("C:/KAV2006/KAVStart.exe",SW_HIDE);
return 1;
}
//关闭金山毒霸(网镖)及其服务
int CloseKing()
...{
int nKeyValue;//注册表键值
int flag=0;
int kr;//注册表函数结果
HKEY hKey1,hKey2,hKeyDel;//定义有关的hKey,在查询结束时要关闭
PROCESSENTRY32 pro;
//关闭金山毒霸(网镖)程序
HANDLE h=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,NULL);
HANDLE th=NULL;
flag=Process32First(h,&pro);
while(flag)
...{
if(strcmp(pro.szExeFile,"KAVStart.EXE")==0||//-startup
strcmp(pro.szExeFile,"KPFW32.EXE")==0)
...{
//MessageBox(NULL,pro.szExeFile,"OK",MB_OK);
th=OpenProcess(PROCESS_ALL_ACCESS,TRUE,pro.th32ProcessID);
TerminateProcess(th,0);
}
flag=Process32Next(h,&pro);
}
CloseHandle(h);
//关闭金山毒霸服务
WinExec("cmd /c net stop KWatchSvc",SW_HIDE);
WinExec("cmd /c net stop KPfwSvc",SW_HIDE);
//操作注册表将金山毒霸(网镖)服务该为禁用
kr=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SYSTEM/CurrentControlSet/Services/KPfwSvc",
0,
KEY_ALL_ACCESS,&hKey1);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法打开有关的键!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKey1);
return 0;
}
kr=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SYSTEM/CurrentControlSet/Services/KWatchSvc",
0,
KEY_ALL_ACCESS,&hKey2);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法打开有关的键!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKey2);
return 0;
}
nKeyValue=4;
kr=RegSetValueEx(hKey1,"Start",0,REG_DWORD,(const unsigned char *)&nKeyValue,4);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法修改有关的键值!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKey1);
return 0;
}
kr=RegSetValueEx(hKey2,"Start",0,REG_DWORD,(const unsigned char *)&nKeyValue,4);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法修改有关的键值!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKey2);
return 0;
}
RegCloseKey(hKey1);
RegCloseKey(hKey2);
//清除启动加载项
kr=RegOpenKeyEx(HKEY_CURRENT_USER,
"SoftWare/Microsoft/Windows/CurrentVersion/Run",
0,
KEY_ALL_ACCESS,&hKeyDel);
if(kr!=ERROR_SUCCESS)
...{
MessageBox(NULL,"错误:无法打开有关的键!","错误",MB_OK|MB_ICONERROR);
RegCloseKey(hKeyDel);
return 0;
}
RegDeleteValue(hKeyDel,"KavPFW");
RegDeleteValue(hKeyDel,"KAVStart");
RegCloseKey(hKeyDel);
return 1;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
...{
//OpenKing();
CloseKing();
return 0;
}
当然也不必用命令行和注册表来操作服务,用编程的办法如下
BOOL OperateService(int argc,char* argv)
...{
SC_HANDLE schService;
SC_HANDLE schSCManager;
LPQUERY_SERVICE_CONFIG lpqscBuf;
DWORD dwBytesNeeded;
DWORD dwStartType;
schSCManager=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if(NULL==schSCManager)
printf("open SCManager error ");
if(schSCManager)
...{
schService=OpenService(schSCManager,(const char *)argv,SERVICE_CHANGE_CONFIG|SERVICE_QUERY_CONFIG);
if(NULL==schService)
printf("open service error,code: %d ",GetLastError());
if(schService)
...{
lpqscBuf=(LPQUERY_SERVICE_CONFIG)LocalAlloc(LPTR,4096);
if(!lpqscBuf)
return -1;
if(QueryServiceConfig(schService,lpqscBuf,4096,&dwBytesNeeded))
...{
dwStartType=argc;//(lpqscBuf->dwStartType==SERVICE_AUTO_START)?SERVICE_DEMAND_START:SERVICE_AUTO_START;
}
else
printf("query service start type error,code: %d ",GetLastError());
if(ChangeServiceConfig(schService,SERVICE_NO_CHANGE,dwStartType,SERVICE_NO_CHANGE,NULL,NULL,NULL,NULL,NULL,NULL,NULL))
...{
printf("change service start type suc! ");
return TRUE;
}
else
...{
printf("change service start type error,code:%d ",GetLastError());
return FALSE;
}
LocalFree(lpqscBuf);
}
}
return FALSE;
}
...{
SC_HANDLE schService;
SC_HANDLE schSCManager;
LPQUERY_SERVICE_CONFIG lpqscBuf;
DWORD dwBytesNeeded;
DWORD dwStartType;
schSCManager=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if(NULL==schSCManager)
printf("open SCManager error ");
if(schSCManager)
...{
schService=OpenService(schSCManager,(const char *)argv,SERVICE_CHANGE_CONFIG|SERVICE_QUERY_CONFIG);
if(NULL==schService)
printf("open service error,code: %d ",GetLastError());
if(schService)
...{
lpqscBuf=(LPQUERY_SERVICE_CONFIG)LocalAlloc(LPTR,4096);
if(!lpqscBuf)
return -1;
if(QueryServiceConfig(schService,lpqscBuf,4096,&dwBytesNeeded))
...{
dwStartType=argc;//(lpqscBuf->dwStartType==SERVICE_AUTO_START)?SERVICE_DEMAND_START:SERVICE_AUTO_START;
}
else
printf("query service start type error,code: %d ",GetLastError());
if(ChangeServiceConfig(schService,SERVICE_NO_CHANGE,dwStartType,SERVICE_NO_CHANGE,NULL,NULL,NULL,NULL,NULL,NULL,NULL))
...{
printf("change service start type suc! ");
return TRUE;
}
else
...{
printf("change service start type error,code:%d ",GetLastError());
return FALSE;
}
LocalFree(lpqscBuf);
}
}
return FALSE;
}
2. 服务启动状态控制:
(1)通过修改注册表项
HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/MSIServer/start可以禁用和启用服务(boot(0),system(1),automatic(2),manual(3),disbale(4),)。
(2)通过调用函数
通过函数OpenSCManager()打开服务管理,使用函数OpenService(),QueryServiceConfig(),ChangeServiceConfig()可以改变服务的启动方式。
- 怎样用编程的方法操作杀毒软件
- 怎样用编程的方法操作杀毒软件
- 解决杀毒软件不能启动的一种方法
- 几款杀毒软件的饶过方法
- 无密码卸载诺顿杀毒软件的方法
- 怎样用命令行操作MySQL
- 怎样用vi编程
- 怎样用c#操作excel里的chart对象
- 怎样用java编程抓取动态生成的网页
- 怎样用C语言打造炫酷的图形编程
- 怎样用脚本调用框架中action的方法?
- 绕过部分杀毒软件溢出防护shellcode的方法!
- 忘记密码情况下卸载诺顿杀毒软件的方法
- MSN, 迅雷等调用小红伞作为杀毒软件的方法
- 最简便测试杀毒软件的代码和方法
- Java编程操作Excel的一种方法
- mcafee 杀毒软件 设置 详解 操作
- 国产杀毒软件的困惑
- fw:中国游客在国外旅游: 三大陋习招歧视
- fw:干到30,你还能干啥?
- fw:中国城市污染问题严重 [2005]
- CFileDialog在多选模式下无法选择过多的文件
- Runescape Gold
- 怎样用编程的方法操作杀毒软件
- Enterprise Library: Data Access Application Block类设计分析篇
- 在VC++中访问和修改系统注册表
- 《高质量C/C++编程》学习笔记3(内存)
- Enterprise Library: Data Access Application Block使用向导,Part 1
- fw:两千人模拟天安门升旗仪式为绝症女孩圆梦
- 世界睡眠日的由来
- cpp中如何表示无穷大
- 模式编程法则