联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2018-08-15 03:50


#pragma comment(lib, "wbemuuid.lib")  

#pragma comment(lib, "comsuppw.lib")

#pragma comment(lib,"ws2_32.lib")

static bool readConfigFile(const char * cfgfilepath, const string & key, string & value);

std::string GBToUTF8(const char* str)

{

std::string result;

WCHAR *strSrc;

TCHAR *szRes;


//获得临时变量的大小

int i = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);

strSrc = new WCHAR[i + 1];

MultiByteToWideChar(CP_ACP, 0, str, -1, strSrc, i);


//获得临时变量的大小

i = WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, NULL, 0, NULL, NULL);

szRes = new TCHAR[i + 1];

int j = WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, szRes, i, NULL, NULL);


result = szRes;

delete[]strSrc;

delete[]szRes;


return result;

}

void printhost();

class CMyWMI {

IWbemLocator *pLoc_;

IWbemServices *pSvc_;


void GetInfo(WCHAR* wszQueryInfo, IWbemClassObject *pclsObj);

public:

CMyWMI() :pLoc_(NULL), pSvc_(NULL) {}

~CMyWMI() { ClearWMI(); }

bool InitWMI();

bool ClearWMI();

bool QueryCPUInfo();

bool QueryPHYMEMInfo();

bool QueryProcessNumber();

bool QuerySwapInfo();

bool QueryDiskInfo();

bool QueryDiskIOInfo();

bool QueryNETINTINFO();

bool QueryProcessInfo();

bool QueryMemorySizeINFO();

bool QueryUSERINFO();

bool QuerySYSINFO();

};


bool CMyWMI::InitWMI()

{

HRESULT hres; //定义COM调用的返回  

bool bRet = false;


try {

hres = CoInitializeEx(0, COINIT_MULTITHREADED);

if (FAILED(hres))

{

throw exception("CoInitializeEx() error.");

}


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))

{

throw exception("CoInitializeEx() error.");

}


hres = CoCreateInstance(

CLSID_WbemLocator,

0,

CLSCTX_INPROC_SERVER,

IID_IWbemLocator, (LPVOID *)&pLoc_);


if (FAILED(hres))

{

throw exception("CoCreateInstance() error.");

}


// to make IWbemServices calls.  

hres = pLoc_->ConnectServer(

_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace  

NULL,                    // User name. NULL = current user  

NULL,                    // User password. NULL = current  

0,                       // Locale. NULL indicates current  

NULL,                    // Security flags.  

0,                       // Authority (e.g. Kerberos)  

0,                       // Context object  

&pSvc_                    // pointer to IWbemServices proxy  

);


if (FAILED(hres))

{

throw exception("ConnectServer() error.");

}


hres = CoSetProxyBlanket(

pSvc_,                        // Indicates the proxy to set  

RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx  

RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx  

NULL,                        // Server principal name  

RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx  

RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx  

NULL,                        // client identity  

EOAC_NONE                    // proxy capabilities  

);


if (FAILED(hres))

{

throw exception("CoSetProxyBlanket() error.");

}



}

catch (exception& e)

{

cout << e.what() << endl;

return bRet;

}


bRet = true;

return bRet;

}

bool CMyWMI::ClearWMI()

{

bool bRet = false;


if (NULL != pSvc_)

pSvc_->Release();


if (pLoc_ != NULL)

pLoc_->Release();



CoUninitialize();

bRet = true;

return bRet;

}

void CMyWMI::GetInfo(WCHAR* wszQueryInfo, IWbemClassObject *pclsObj)

{

if (wszQueryInfo == NULL || NULL == pclsObj)

return;

VARIANT vtProp;

char* lpszText = NULL;


HRESULT hr = pclsObj->Get(wszQueryInfo, 0, &vtProp, 0, 0);

if (SUCCEEDED(hr) && (V_VT(&vtProp) == VT_BSTR))

{

lpszText = _com_util::ConvertBSTRToString(V_BSTR(&vtProp));

//printf_s("\t%s", GBToUTF8(lpszText));

cout << "\t" << GBToUTF8(lpszText);

}

else

{

if (SUCCEEDED(hr) && (V_VT(&vtProp) == VT_I1 || V_VT(&vtProp) == VT_I2 ||

V_VT(&vtProp) == VT_I4 || V_VT(&vtProp) == VT_I8 || V_VT(&vtProp) == VT_INT))

{

printf_s("\t%d", vtProp.intVal);

}


else

{

if (SUCCEEDED(hr) && (V_VT(&vtProp) == VT_BOOL))

{

if (vtProp.intVal)

{

printf_s("\ttrue");

}

else

{

printf_s("\tfalse");

}

}

else

{

cout << "\t0";

}

}


}





delete[] lpszText;

VariantClear(&vtProp);

}

bool CMyWMI::QueryCPUInfo()

{

HRESULT hres; //定义COM调用的返回  

IEnumWbemClassObject* pEnumerator = NULL;

bool bRet = false;


try {

hres = pSvc_->ExecQuery(

bstr_t("WQL"),

bstr_t("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor where Name=\"_Total\""),

WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

NULL,

&pEnumerator);


if (FAILED(hres))

{

throw exception("ExecQuery() error.");

}


while (pEnumerator)

{

IWbemClassObject *pclsObj;

ULONG uReturn = 0;


HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,

&pclsObj, &uReturn);

if (0 == uReturn)

{

break;

}

printhost();

cout << "\tNT_CPUINFO";

/*GetInfo(L"LoadPercentage", pclsObj);

GetInfo(L"NumberofCores", pclsObj);

GetInfo(L"NumberofLogicalProcessors", pclsObj);

GetInfo(L"MaxClockSpeed", pclsObj);*/

GetInfo(L"PercentProcessorTime", pclsObj);

cout << endl;

pclsObj->Release();

}



}

catch (exception& e)

{

cout << e.what() << endl;

if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}

return bRet;

}



if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}


bRet = true;

return bRet;

}

bool CMyWMI::QueryPHYMEMInfo()

{

HRESULT hres; //定义COM调用的返回  

IEnumWbemClassObject* pEnumerator = NULL;

bool bRet = false;


try {

hres = pSvc_->ExecQuery(

bstr_t("WQL"),

bstr_t("SELECT * FROM Win32_OperatingSystem"),

WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

NULL,

&pEnumerator);


if (FAILED(hres))

{

throw exception("ExecQuery() error.");

}


while (pEnumerator)

{

IWbemClassObject *pclsObj;

ULONG uReturn = 0;


HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,

&pclsObj, &uReturn);

if (0 == uReturn)

{

break;

}

printhost();

cout << "\tNT_PHYMEMINFO";

GetInfo(L"freePhysicalMemory", pclsObj);

GetInfo(L"TotalVisibleMemorySize", pclsObj);


pclsObj->Release();

}



}

catch (exception& e)

{

cout << e.what() << endl;

if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}

return bRet;

}



if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}



try {

hres = pSvc_->ExecQuery(

bstr_t("WQL"),

bstr_t("SELECT * FROM Win32_PerfFormattedData_PerfOS_Memory"),

WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

NULL,

&pEnumerator);


if (FAILED(hres))

{

throw exception("ExecQuery() error.");

}


while (pEnumerator)

{

IWbemClassObject *pclsObj;

ULONG uReturn = 0;


HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,

&pclsObj, &uReturn);

if (0 == uReturn)

{

break;

}


GetInfo(L"AvailableMBytes", pclsObj);


pclsObj->Release();

}



}

catch (exception& e)

{

cout << e.what() << endl;

if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}

return bRet;

}



if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}




try {

hres = pSvc_->ExecQuery(

bstr_t("WQL"),

bstr_t("SELECT * FROM win32_ComputerSystem"),

WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

NULL,

&pEnumerator);


if (FAILED(hres))

{

throw exception("ExecQuery() error.");

}


while (pEnumerator)

{

IWbemClassObject *pclsObj;

ULONG uReturn = 0;


HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,

&pclsObj, &uReturn);

if (0 == uReturn)

{

break;

}


GetInfo(L"TotalPhysicalMemory", pclsObj);

cout << endl;

pclsObj->Release();

}



}

catch (exception& e)

{

cout << e.what() << endl;

if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}

return bRet;

}



if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}

bRet = true;

return bRet;

}

bool CMyWMI::QuerySwapInfo()

{

HRESULT hres; //定义COM调用的返回  

IEnumWbemClassObject* pEnumerator = NULL;

bool bRet = false;


try {

hres = pSvc_->ExecQuery(

bstr_t("WQL"),

bstr_t("SELECT * FROM win32_PagefileUsage"),

WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

NULL,

&pEnumerator);


if (FAILED(hres))

{

throw exception("ExecQuery() error.");

}


while (pEnumerator)

{

IWbemClassObject *pclsObj;

ULONG uReturn = 0;


HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,

&pclsObj, &uReturn);

if (0 == uReturn)

{

break;

}

printhost();

cout << "\tNT_SWAPINFO";

GetInfo(L"AllocatedBaseSize", pclsObj);

GetInfo(L"CurrentUsage", pclsObj);

cout << endl;

pclsObj->Release();

}



}

catch (exception& e)

{

cout << e.what() << endl;

if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}

return bRet;

}



if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}


bRet = true;

return bRet;

}

bool CMyWMI::QueryDiskInfo()

{

HRESULT hres; //定义COM调用的返回  

IEnumWbemClassObject* pEnumerator = NULL;

bool bRet = false;


try {

hres = pSvc_->ExecQuery(

bstr_t("WQL"),

bstr_t("SELECT * FROM win32_LogicalDisk where DriveType=\"3\""),

WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

NULL,

&pEnumerator);


if (FAILED(hres))

{

throw exception("ExecQuery() error.");

}


while (pEnumerator)

{

IWbemClassObject *pclsObj;

ULONG uReturn = 0;


HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,

&pclsObj, &uReturn);

if (0 == uReturn)

{

break;

}

printhost();

cout << "\tNT_DISKINFO";

GetInfo(L"Caption", pclsObj);

GetInfo(L"Size", pclsObj);

GetInfo(L"freeSpace", pclsObj);

cout << endl;

pclsObj->Release();

}



}

catch (exception& e)

{

cout << e.what() << endl;

if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}

return bRet;

}



if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}


bRet = true;

return bRet;

}

bool CMyWMI::QueryDiskIOInfo()

{

HRESULT hres; //定义COM调用的返回  

IEnumWbemClassObject* pEnumerator = NULL;

bool bRet = false;


try {

hres = pSvc_->ExecQuery(

bstr_t("WQL"),

bstr_t("SELECT * FROM win32_PerfFormattedData_PerfDisk_LogicalDisk"),

WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

NULL,

&pEnumerator);


if (FAILED(hres))

{

throw exception("ExecQuery() error.");

}


while (pEnumerator)

{

IWbemClassObject *pclsObj;

ULONG uReturn = 0;


HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,

&pclsObj, &uReturn);

if (0 == uReturn)

{

break;

}


printhost();

cout << "\tNT_DISKIOINFO";

GetInfo(L"Name", pclsObj);

GetInfo(L"DiskBytesPersec", pclsObj);

GetInfo(L"DiskReadBytesPersec", pclsObj);

GetInfo(L"DiskWriteBytesPersec", pclsObj);

cout << endl;

pclsObj->Release();

}



}

catch (exception& e)

{

cout << e.what() << endl;

if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}

return bRet;

}



if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}


bRet = true;

return bRet;

}

bool CMyWMI::QueryNETINTINFO()

{

HRESULT hres; //定义COM调用的返回  

IEnumWbemClassObject* pEnumerator = NULL;


bool bRet = false;


try {

hres = pSvc_->ExecQuery(

bstr_t("WQL"),

bstr_t("SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface"),

WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

NULL,

&pEnumerator);



if (FAILED(hres))

{

throw exception("ExecQuery() error.");

}



while (pEnumerator)

{

IWbemClassObject *pclsObj;

ULONG uReturn = 0;


HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,

&pclsObj, &uReturn);


if (0 == uReturn)

{

break;

}


printhost();

cout << "\tNT_NETINTINFO";

GetInfo(L"Name", pclsObj);

GetInfo(L"BytesReceivedPersec", pclsObj);

GetInfo(L"BytesSentPersec", pclsObj);

GetInfo(L"BytesTotalPersec", pclsObj);

GetInfo(L"CurrentBandwidth", pclsObj);

cout << endl;

pclsObj->Release();

}



}

catch (exception& e)

{

cout << e.what() << endl;

if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}


return bRet;

}



if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}


bRet = true;

return bRet;

}

bool CMyWMI::QueryProcessNumber()

{

HRESULT hres; //定义COM调用的返回  

IEnumWbemClassObject* pEnumerator = NULL;

bool bRet = false;


//===================================================================================

//====从process.txt文件中获取需要的进程名============================================


fstream cFile;

cFile.open("process.txt");

if (!cFile.is_open())

{

cout << "open fail" << endl;

return false;

}

char tmp[1000];

string line[100];

int n = 0;

while (!cFile.eof())

{

cFile.getline(tmp, 1000);

line[n] = tmp;

n++;

}

cFile.close();


//==============================================================================================

/*for (int i = 0; i < n; i++)

{

string wql = "SELECT * FROM win32_Process WHERE Name like ";

string newLine = "";

newLine = "\"%" + line[i] + "%\"";

wql += newLine;

char *cwql = (char*)wql.c_str();

try {

hres = pSvc_->ExecQuery(

bstr_t("WQL"),

bstr_t(cwql),

WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

NULL,

&pEnumerator);


if (FAILED(hres))

{

throw exception("ExecQuery() error.");

}

int number = 0;

while (pEnumerator)

{

IWbemClassObject *pclsObj;

ULONG uReturn = 0;


HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,

&pclsObj, &uReturn);

if (0 == uReturn)

{

break;

}



++number;


pclsObj->Release();

}

printhost();

cout << "\tNT_PROCESSNUMBER"<<"\t"<<line[i]<<"\t"<<number<<endl;


}

catch (exception& e)

{

cout << e.what() << endl;

if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}

return bRet;

}



if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}

}

bRet = true;

return bRet;*/

char buf[255];

string text;

for (int i = 0; i<n; i++)

{

string title[100];

DWORD id[100];

int count = 0;

int number = 0;

DWORD processId = 0;

HWND hwnd = FindWindow(0, 0);

HWND h = GetWindow(hwnd, GW_HWNDFIRST);

while (h)

{

GetWindowTextA(h, buf, 255);

if (strlen(buf) > 1)

{

text = buf;

if (line[i].compare("") != 0 && text.find(line[i]) != text.npos)

{

GetWindowThreadProcessId(h, &processId);

title[count] = text;

id[count] = processId;

count++;

number++;

}

}

h = GetWindow(h, GW_HWNDNEXT);

}

if (line[i].compare("") != 0)

{

printhost();

cout << "\tNT_PROCESSNUMBER\t" << GBToUTF8(line[i].c_str()) << "\t" << number << endl;

}

for (int a = 0; a < count; a++)

{

string wql = "SELECT * FROM win32_PerfFormattedData_PerfProc_Process WHERE IDProcess = ";

string newLine = "";

newLine = "\"" + to_string(id[a]) + "\"";

wql += newLine;

char *cwql = (char*)wql.c_str();

try {

hres = pSvc_->ExecQuery(

bstr_t("WQL"),

bstr_t(cwql),

WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

NULL,

&pEnumerator);


if (FAILED(hres))

{

throw exception("ExecQuery() error.");

}

int number = 0;

while (pEnumerator)

{

IWbemClassObject *pclsObj;

ULONG uReturn = 0;


HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,

&pclsObj, &uReturn);

if (0 == uReturn)

{

break;

}





printhost();

cout << "\tNT_PROCESSINFOCPU";

GetInfo(L"Name", pclsObj);

cout << "\t" << GBToUTF8(title[a].c_str());

GetInfo(L"IDProcess", pclsObj);

GetInfo(L"PercentProcessorTime", pclsObj);

cout << endl;

printhost();

cout << "\tNT_PROCESSINFOMEMORY";

GetInfo(L"Name", pclsObj);

cout << "\t" << GBToUTF8(title[a].c_str());

GetInfo(L"IDProcess", pclsObj);

GetInfo(L"WorkingSet", pclsObj);

QueryMemorySizeINFO();

cout << endl;

printhost();

cout << "\tNT_PROCESSINFOREADY";

GetInfo(L"Name", pclsObj);

cout << "\t" << GBToUTF8(title[a].c_str());

GetInfo(L"IDProcess", pclsObj);

GetInfo(L"IOReadBytesPersec", pclsObj);

cout << endl;

printhost();

cout << "\tNT_PROCESSINFOWRITE";

GetInfo(L"Name", pclsObj);

cout << "\t" << GBToUTF8(title[a].c_str());

GetInfo(L"IDProcess", pclsObj);

GetInfo(L"IOWriteBytesPersec", pclsObj);

cout << endl;

pclsObj->Release();

}



}

catch (exception& e)

{

cout << e.what() << endl;

if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}

return bRet;

}



if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}

}

}

return true;

}

//bool CMyWMI::QueryProcessInfo()

//{

//HRESULT hres; //定义COM调用的返回  

//IEnumWbemClassObject* pEnumerator = NULL;

//bool bRet = false;

//

////===================================================================================

////====从process.txt文件中获取需要的进程名============================================

//

//fstream cFile;

//cFile.open("process.txt");

//if (!cFile.is_open())

//{

//cout << "open fail" << endl;

//}

//char tmp[1000];

//string line[100];

//int n = 0;

//while (!cFile.eof())

//{

//cFile.getline(tmp, 1000);

//line[n] = tmp;

//n++;

//}

//cFile.close();

//

////==============================================================================================

//for (int i = 0; i < n; i++)

//{

//string wql = "SELECT * FROM win32_PerfFormattedData_PerfProc_Process WHERE Name like ";

//string newLine = "";

//newLine = "\"%" + line[i] + "%\"";

//wql += newLine;

//char *cwql = (char*)wql.c_str();

//try {

//hres = pSvc_->ExecQuery(

//bstr_t("WQL"),

//bstr_t(cwql),

//WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

//NULL,

//&pEnumerator);

//

//if (FAILED(hres))

//{

//throw exception("ExecQuery() error.");

//}

//int number = 0;

//while (pEnumerator)

//{

//IWbemClassObject *pclsObj;

//ULONG uReturn = 0;

//

//HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,

//&pclsObj, &uReturn);

//if (0 == uReturn)

//{

//break;

//}

//

//

//

//

//printhost();

//cout << "\tNT_PROCESSINFOCPU";

//GetInfo(L"Name", pclsObj);

//GetInfo(L"IDProcess", pclsObj);

//GetInfo(L"PercentProcessorTime", pclsObj);

//cout << endl;

//printhost();

//cout << "\tNT_PROCESSINFOMEMORY";

//GetInfo(L"Name", pclsObj);

//GetInfo(L"IDProcess", pclsObj);

//GetInfo(L"WorkingSet", pclsObj);

//QueryMemorySizeINFO();

//cout << endl;

//printhost();

//cout << "\tNT_PROCESSINFOREADY";

//GetInfo(L"Name", pclsObj);

//GetInfo(L"IDProcess", pclsObj);

//GetInfo(L"IOReadBytesPersec", pclsObj);

//cout << endl;

//printhost();

//cout << "\tNT_PROCESSINFOWRITE";

//GetInfo(L"Name", pclsObj);

//GetInfo(L"IDProcess", pclsObj);

//GetInfo(L"IOWriteBytesPersec", pclsObj);

//cout << endl;

//pclsObj->Release();

//}

//

//

//}

//catch (exception& e)

//{

//cout << e.what() << endl;

//if (pEnumerator != NULL)

//{

//pEnumerator->Release();

//pEnumerator = NULL;

//}

//return bRet;

//}

//

//

//if (pEnumerator != NULL)

//{

//pEnumerator->Release();

//pEnumerator = NULL;

//}

//}

//bRet = true;

//return bRet;

//}

bool CMyWMI::QueryMemorySizeINFO()

{

HRESULT hres; //定义COM调用的返回  

IEnumWbemClassObject* pEnumerator = NULL;

bool bRet = false;


try {

hres = pSvc_->ExecQuery(

bstr_t("WQL"),

bstr_t("SELECT * FROM win32_OperatingSystem"),

WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

NULL,

&pEnumerator);


if (FAILED(hres))

{

throw exception("ExecQuery() error.");

}


while (pEnumerator)

{

IWbemClassObject *pclsObj;

ULONG uReturn = 0;


HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,

&pclsObj, &uReturn);

if (0 == uReturn)

{

break;

}


GetInfo(L"TotalVisibleMemorySize", pclsObj);

pclsObj->Release();

}



}

catch (exception& e)

{

cout << e.what() << endl;

if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}

return bRet;

}



if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}


bRet = true;

return bRet;

}

bool CMyWMI::QueryUSERINFO()

{

HRESULT hres; //定义COM调用的返回  

IEnumWbemClassObject* pEnumerator = NULL;

bool bRet = false;


try {

hres = pSvc_->ExecQuery(

bstr_t("WQL"),

bstr_t("SELECT * FROM win32_UserAccount"),

WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

NULL,

&pEnumerator);


if (FAILED(hres))

{

throw exception("ExecQuery() error.");

}


while (pEnumerator)

{

IWbemClassObject *pclsObj;

ULONG uReturn = 0;


HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,

&pclsObj, &uReturn);

if (0 == uReturn)

{

break;

}


printhost();

cout << "\tNT_USERINFO";

GetInfo(L"Name", pclsObj);

GetInfo(L"LocalAccount", pclsObj);

GetInfo(L"Domain", pclsObj);

GetInfo(L"Status", pclsObj);

cout << endl;

pclsObj->Release();

}

}

catch (exception& e)

{

cout << e.what() << endl;

if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}

return bRet;

}



if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}


bRet = true;

return bRet;

}

bool CMyWMI::QuerySYSINFO()

{

HRESULT hres; //定义COM调用的返回  

IEnumWbemClassObject* pEnumerator = NULL;

bool bRet = false;


try {

hres = pSvc_->ExecQuery(

bstr_t("WQL"),

bstr_t("SELECT * FROM win32_OperatingSystem"),

WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

NULL,

&pEnumerator);


if (FAILED(hres))

{

throw exception("ExecQuery() error.");

}


while (pEnumerator)

{

IWbemClassObject *pclsObj;

ULONG uReturn = 0;


HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,

&pclsObj, &uReturn);

if (0 == uReturn)

{

break;

}


printhost();

cout << "\tNT_SYSINFO";

GetInfo(L"Caption", pclsObj);

GetInfo(L"CSDVersion", pclsObj);

GetInfo(L"manufacturer", pclsObj);

GetInfo(L"NumberofProcesses", pclsObj);

GetInfo(L"NumberOfLicensedUsers", pclsObj);

GetInfo(L"NumberOfUsers", pclsObj);

GetInfo(L"CSName", pclsObj);

GetInfo(L"Status", pclsObj);

GetInfo(L"OSArchitecture", pclsObj);

cout << endl;

pclsObj->Release();

}



}

catch (exception& e)

{

cout << e.what() << endl;

if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}

return bRet;

}



if (pEnumerator != NULL)

{

pEnumerator->Release();

pEnumerator = NULL;

}


bRet = true;

return bRet;

}


static bool readConfigFile(const char * cfgfilepath, const string & key, string & value)

{

fstream cfgFile;

cfgFile.open(cfgfilepath);//打开文件      

if (!cfgFile.is_open())

{

cout << "can not open cfg file!" << endl;

return false;

}

char tmp[1000];

while (!cfgFile.eof())//循环读取每一行  

{

cfgFile.getline(tmp, 1000);//每行读取前1000个字符,1000个应该足够了  

string line(tmp);

size_t pos = line.find('=');//找到每行的“=”号位置,之前是key之后是value  

if (pos == string::npos) return false;

string tmpKey = line.substr(0, pos);//取=号之前  

if (key == tmpKey)

{

value = line.substr(pos + 1);//取=号之后  

return 0;

}

}

return false;

}



void printhost()

{

string localhost;

string localip;

readConfigFile("config.txt", "localhost", localhost);

readConfigFile("config.txt", "localip", localip);

cout << GBToUTF8(localhost.c_str()) << "\t" << GBToUTF8(localip.c_str());

}


void sysuptime()

{

DWORD time = GetTickCount();

int timelength = time / 1000;

printhost();

cout << "\tNT_SYSUPTIME\t" << timelength << endl;

}

int main(int argc, char *argv[])

{

std::string appPath = argv[0];

appPath = appPath.substr(0, appPath.rfind('\\'));

_chdir(appPath.c_str());

//do

//{

freopen("jdnt.txt", "w", stdout);

CMyWMI *myWMI = new CMyWMI;

myWMI->InitWMI();



//freopen("jdnt.txt", "w", stdout);

sysuptime();

myWMI->QueryCPUInfo();

myWMI->QueryPHYMEMInfo();

myWMI->QuerySwapInfo();

myWMI->QueryDiskInfo();

myWMI->QueryDiskIOInfo();

myWMI->QueryNETINTINFO();

myWMI->QueryProcessNumber();

//myWMI->QueryProcessInfo();

//myWMI->QueryUSERINFO();

//myWMI->QuerySYSINFO();



delete myWMI;

myWMI = NULL;

string port;

string ipAddress;

string time;

int ds;

readConfigFile("config.txt", "port", port);

readConfigFile("config.txt", "ipAddress", ipAddress);

readConfigFile("config.txt", "time", time);

////目标IP地址

string IP;

IP = "tcp://";

IP += ipAddress;

IP += ":";

IP += port;

ds = atoi(time.c_str());


////获取文件大小

ifstream in("jdnt.txt");

in.seekg(0, ios::end);      //设置文件指针到文件流的尾部

streampos ps = in.tellg();  //读取文件指针的位置

int m;

m = ps;

in.close();                 //关闭文件流



//zeromq

int iSndTimeout = 60000;// millsecond

FILE *stream = NULL;

void * pCtx = NULL;

void * pSock = NULL;

////使用tcp协议进行通信,需要连接的目标机器IP地址为192.168.1.2

////通信使用的网络端口 为7766



////const char *pAddr = "tcp://192.168.1.104:5559";

const char *pAddr = IP.c_str();

char *buffer = new char[m];

memset(buffer, 0, m);


////创建context

if ((pCtx = zmq_ctx_new()) == NULL)

{

return 0;

}


////创建socket

if ((pSock = zmq_socket(pCtx, ZMQ_REQ)) == NULL)

{

zmq_ctx_destroy(pCtx);


return 0;

}


////设置接收超时

if (zmq_setsockopt(pSock, ZMQ_RCVTIMEO, &iSndTimeout, sizeof(iSndTimeout)) < 0)

{

zmq_close(pSock);

zmq_ctx_destroy(pCtx);

return 0;

}


////连接目标IP192.168.1.2,端口7766

if (zmq_connect(pSock, pAddr) < 0)

{

zmq_close(pSock);

zmq_ctx_destroy(pCtx);

return 0;

}


////打开发送文件



stream = fopen("jdnt.txt", "r");

fread(buffer, m, 1, stream);


zmq_send(pSock, buffer, strlen(buffer), 0);

memset(buffer, 0, m);




////关闭发送文件

delete[] buffer;

buffer = NULL;

fclose(stream);

zmq_close(pSock);

zmq_ctx_destroy(pCtx);

fclose(stdout);

//Sleep(ds);

//}while (1);



return 0;

}


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp