设为首页 加入收藏

TOP

VC之获得系统安装的反病毒软件[Using WMI in C++](一)
2014-11-23 20:26:20 】 浏览:751
Tags:获得 系统 安装 病毒 软件 Using WMI

在学习本文前,你需要一点点的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

using namespace std;

#include

#include


# 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();

re

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇最简单的MFC程序 下一篇VC调用VBS

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目