当前位置:编程学习 > VC++ >>

VC之获得系统安装的反病毒软件[Using WMI in C++]

在学习本文前,你需要一点点的VBS基础,WMI了解常识,COM接口皮毛就行。

今天的问题是,如何获得系统中安装的杀毒软件?有人会说遍历注册表之类的。其实用不着这么麻烦。每个正式的AV(Anti-Virus)软件,都要向系统注册自己。通过查看WMI中的ootSecurityCenter这个名称空间,我们就能知道其中装了那些反病毒产品。

我们来看一段Vbs代码:

strComputer = "." 
Set objComputer = CreateObject("Shell.LocalMachine")  
Set oWMI = GetObject("winmgmts:\" & strComputer & "ootSecurityCenter")  
Set colAV = oWMI.ExecQuery("Select * from AntiVirusProduct")  
  For Each objAntiVirusProduct In colAV  
   If IsNull(objAntiVirusProduct.instanceGuid) Then 
    strSubject = "Anti-virus is not running on " & objComputer.MachineName  
    strTextbody = "You will need to check on " & objComputer.MachineName   
    Call SmtpServer    
   Else 
    strCompany = objAntiVirusProduct.companyName  
    strAV = objAntiVirusProduct.displayName  
    strScanning = objAntiVirusProduct.onAccessScanningEnabled  
    strUptodate = objAntiVirusProduct.productUptoDate 
strComputer = "."
Set objComputer = CreateObject("Shell.LocalMachine")
Set oWMI = GetObject("winmgmts:\" & strComputer & "ootSecurityCenter")
Set colAV = oWMI.ExecQuery("Select * from AntiVirusProduct")
  For Each objAntiVirusProduct In colAV
   If IsNull(objAntiVirusProduct.instanceGuid) Then
    strSubject = "Anti-virus is not running on " & objComputer.MachineName
    strTextbody = "You will need to check on " & objComputer.MachineName
    Call SmtpServer 
   Else
    strCompany = objAntiVirusProduct.companyName
    strAV = objAntiVirusProduct.displayName
    strScanning = objAntiVirusProduct.onAccessScanningEnabled
    strUptodate = objAntiVirusProduct.productUptoDate

如果以上代码你觉得很难懂的话,那么不推荐你继续看下去,请稍微补习下Vbs和Wmi相关的知识。

Select * from AntiVirusProduct是一条WQL语句,用来查询所有的反病毒软件,并返回一个集合。我们用WMITools里面的WMI CIM Studio进行查看下ootSecurityCenter这个名称空间里面有哪些Win32 Class

 


 在左边,看到了么,AntiVirusProduct。还有FireWallProduct,防火墙产品,本机没有防火墙,就用查看反病毒软件来做说明吧。

右边的呢,都是AntiVirusProduct这个类的属性(Properties)。里面有产品名,产品公司名,版本号等等。那么我现在需要的,就是通过C++获取到和Vbs一样的信息。上面的Vbs脚本不知道你执行过了么?

我们接下来看C++代码

#define _WIN32_DCOM  
 
#include <iostream>  
 
using namespace std;  
 
#include <comdef.h>  
 
#include <Wbemidl.h>  
 
 
# pragma comment(lib, "wbemuuid.lib")  
 
 
int main(int argc, char **argv)  
 
{  
 
    HRESULT hres;  
 
 
    // Step 1: --------------------------------------------------  
 
    // Initialize COM. ------------------------------------------  
 
 
    hres =  CoInitializeEx(0, COINIT_MULTITHREADED);   
 
    if (FAILED(hres))  
 
    {  
 
        cout << "Failed to initialize COM library. Error code = 0x"   
 
            << hex << hres << endl;  
 
        return 1;                  // Program has failed.  
 
    }  
 
 
    // Step 2: --------------------------------------------------  
 
    // Set general COM security levels --------------------------  
 
 
    hres =  CoInitializeSecurity(  
 
        NULL,   
 
        -1,                          // COM authentication  
 
        NULL,                        // Authentication services  
 
        NULL,                        // Reserved  
 
        RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication   
 
        RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation    
 
        NULL,                        // Authentication info  
 
        EOAC_NONE,                   // Additional capabilities   
 
        NULL                         // Reserved  
 
        );            
 
    if (FAILED(hres))  
 
    {  
 
        cout << "Failed to initialize security. Error code = 0x"   
 
            << hex << hres << endl;  
 
        CoUninitialize();  
 
        return 1;                    // Program has failed.  
 
    }  
 
 
    // Step 3: ---------------------------------------------------  
 
    // Obtain the initial locator to WMI -------------------------  
 
 
    IWbemLocator *pLoc = NULL;  
 
 
    hres = CoCreateInstance(  
 
        CLSID_WbemLocator,               
 
        0,   
 

补充:软件开发 , Vc ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,