#include "WMIManager.h" #include <vector> #include <string> #include <boost/thread/thread.hpp> #include <boost/array.hpp> #include <boost/asio.hpp> /************************************************************** 技术博客 http://www.cnblogs.com/itdef/ 技术交流群 群号码:324164944 欢迎c c++ windows驱动爱好者 服务器程序员沟通交流 **************************************************************/ using namespace DEF; using boost::asio::ip::tcp; void WMIManager::Transfer() { tcp::resolver resolver(io_); tcp::resolver::query query("127.0.0.1", "8893"); tcp::resolver::iterator iterator = resolver.resolve(query); try { tcp::socket s(io_); boost::asio::connect(s, iterator); while (1) { std::unique_lock<std::mutex> lck(mtx_); while (!ready_) // 如果标志位不为 true, 则等待... cv_.wait(lck); while (!strVec_.empty()) { std::cout << strVec_.back() << std::endl; size_t n = boost::asio::write(s, boost::asio::buffer((strVec_.back()).c_str(), (strVec_.back()).size()+1)); strVec_.pop_back(); } ready_ = false; } } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; } } bool WMIManager::Start() { if (false == wmiInfo_.Init(L"ROOT\\CIMV2")) { return false; } boost::thread t(&WMIManager::Transfer,this); t_.async_wait(boost::bind(&WMIManager::Loop,this, boost::asio::placeholders::error, &t_, &wmiInfo_)); io_.run(); t.join(); return true; } void WMIManager::Loop(const boost::system::error_code& e, boost::asio::deadline_timer* t, WMIInfo* wmiInfo) { if ( wmiInfo->QueryWin32Process(L"WQL", L"SELECT * FROM Win32_Process", strVec_) ) { // transfer std::vector<std::string> } t->expires_at(t->expires_at() + boost::posix_time::seconds(1)); t->async_wait(boost::bind(&WMIManager::Loop, this, boost::asio::placeholders::error, t, wmiInfo)); }
代码片段
具体见
http://www.oschina.net/code/snippet_614253_56135
时间: 2024-11-05 20:28:10