您的位置:首页 > 编程语言

怎样用编程的方法操作杀毒软件

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

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐