怎样用编程的方法操作杀毒软件
2007-11-09 10:21
501 查看
怎样用编程的方法操作杀毒软件
本文以金山毒霸为例将介绍怎样完全关闭和开启杀毒软件及其服务。
主要运用的技术:注册表的编程,进程的操作。
废话少说,看代码。(以下程序为无窗口的应用程序,运行完毕即关闭)
#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;
}
当然也不必用命令行和注册表来操作服务,用编程的办法如下
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;
}
1.服务状态控制:通过函数OpenSCManager()打开服务管理,使用函数OpenService(),ControlService(),DeleteService()可以控制特定服务的起停或者删除
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()可以改变服务的启动方式。
转载自:http://blog.csdn.net/y___y/archive/2007/03/21/1536399.aspx
本文以金山毒霸为例将介绍怎样完全关闭和开启杀毒软件及其服务。
主要运用的技术:注册表的编程,进程的操作。
废话少说,看代码。(以下程序为无窗口的应用程序,运行完毕即关闭)
#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;
}
当然也不必用命令行和注册表来操作服务,用编程的办法如下
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;
}
1.服务状态控制:通过函数OpenSCManager()打开服务管理,使用函数OpenService(),ControlService(),DeleteService()可以控制特定服务的起停或者删除
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()可以改变服务的启动方式。
转载自:http://blog.csdn.net/y___y/archive/2007/03/21/1536399.aspx
相关文章推荐
- 怎样用编程的方法操作杀毒软件
- Java并发编程笔记 Native方法,CAS操作与ABA问题
- VS2008编程软件过期的问题,过期弹出须要升级窗体的解决的方法
- .NET框架下编程操作XML文档的三种方法
- 全能免费杀毒软件——Avast及其MacOS下的卸载方法
- ghost.pif新变种导致杀毒软件0xc00000ba失败的解决方法
- // D:\SaveLog\_SaveLog.dpr立即备份晓亮的电脑操作记录热键(快捷键) F11由于原来的 AutoIt 杀毒软件总是误报没办法只好麻烦一点用 Delphi XE4 做了
- Bashware - Bypass 杀毒软件的新方法
- 对软件开发有利的5个敏捷编程方法
- 市面上所有号称"虚拟机","防火墙"的实时监控杀毒软件无一不是使用的IFSHOOK技术.但是同时也有一些朋友不断写MAIL给我打听如何实现读写的监控.下面给出用VTOOLSD写的代码.也就是所有实时杀毒软件的奥秘.同时,很多拦截文件操作的软件,例如对目录加
- C/C++编程软件C-Free破解方法
- linux 下 C 编程和make的方法 (七、从项目组织化开发谈make操作模块)
- 360杀毒软件安装在windows2003系统的解决方法
- Defender杀毒软件冲突 win8系统Defender关闭方法
- 2011年世界著名的杀毒软件排名是怎样的?
- 【图形化编程软件】 sikuli常用函数、简单事件操作 键盘操作 个人总结笔记
- Windows编程中各种操作文件的方法
- 【图形化编程软件】 sikuli常用函数、简单事件操作 键盘操作 个人总结笔记
- 对软件开发有利的5个敏捷编程方法
- 怎样获取开源软件演化过程中某个方法的修改次数