WMI C++
WMI가 정말 흥미로워서 공부를 해보았는데 공부한건 천천히 쓸게요.
코드 부분입니다.
wmi.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #pragma once #define _WIN32_DCOM #include <comdef.h> #include <WbemIdl.h> #pragma comment(lib, "wbemuuid.lib") #include <iostream> using namespace std; class Wmi { private: HRESULT hRes; IWbemLocator *pLoc; IWbemServices *pSvc; IEnumWbemClassObject *pEnum; IWbemClassObject *pclsObj; int WmiComInit(); int WmiComSecurity(); int WmiComCreate(); int WmiComConnect(); int WmiComProxy(); public: Wmi(); ~Wmi(); int WmiComExcute(bstr_t, const wchar_t*); }; | cs |
wmi.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | #include "wmi.h" Wmi::Wmi() { WmiComInit(); WmiComSecurity(); WmiComCreate(); WmiComConnect(); WmiComProxy(); } Wmi::~Wmi() { pSvc->Release(); pLoc->Release(); pEnum->Release(); CoUninitialize(); } int Wmi::WmiComInit() { hRes = CoInitializeEx(0, COINIT_MULTITHREADED); if (FAILED(hRes)) { cout << "CoInitializeEx func err : " << hex << hRes << endl; return 1; } return 0; } int Wmi::WmiComSecurity() { hRes = CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); if (FAILED(hRes)) { cout << "CoInitializeSecurity Func err : " << hex << hRes << endl; CoUninitialize(); return 1; } return 0; } int Wmi::WmiComCreate() { pLoc = NULL; hRes = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *)&pLoc); if (FAILED(hRes)) { cout << "CoCreateInstance Func err : " << hex << hRes << endl; CoUninitialize(); return 1; } return 0; } int Wmi::WmiComConnect() { pSvc = NULL; hRes = pLoc->ConnectServer( _bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc); if (FAILED(hRes)) { cout << "ConnectServer Func err : " << hex << hRes << endl; pLoc->Release(); CoUninitialize(); return 1; } return 0; } int Wmi::WmiComProxy() { hRes = CoSetProxyBlanket( pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE); if (FAILED(hRes)) { cout << "CoSetProxyBlanket Func err : " << hex << hRes << endl; pSvc->Release(); pLoc->Release(); CoUninitialize(); return 1; } return 0; } //윗 부분까지는 공통부분이고 이 부분부터는 수정을 하셔서 사용하셔됩니다. int Wmi::WmiComExcute(bstr_t query,const wchar_t *object) { pEnum = NULL; hRes = pSvc->ExecQuery( bstr_t("WQL"), query, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnum); if (FAILED(hRes)) { cout << "ExecQuery Func err : " << hex << hRes << endl; pSvc->Release(); pLoc->Release(); CoUninitialize(); return 1; } pclsObj = NULL; ULONG uRet = 0; while (pEnum) { HRESULT hr = pEnum->Next(WBEM_INFINITE, 1, &pclsObj, &uRet); if (0 == uRet) break; VARIANT vtProp; hr = pclsObj->Get(object, 0, &vtProp, 0, 0); VariantClear(&vtProp); //example wcout << vtProp.bstrVal << endl; pclsObj->Release(); } return 0; } | cs |
main.cpp
1 2 3 4 5 6 7 8 9 10 | #include "wmi.h" int main() { Wmi t; //query ex) SELECT * FROM Win32_LogicalDisk //object ex) Name t.WmiComExcute("SELECT * FROM Win32_LogicalDisk", L"Name"); } | cs |
호출하실 땐 그냥 WmiComExcute 만 호출하시면 됩니다.
첫번째 파라미터에는 쿼리문을 넣어주시면 되고 두번째는 클래스 인자가 들어가는데 이건 가변 인자로 만들려다 사용되는게 그때 그때 달라서 임시로 wchar_t 형으로 넣었어요.
'My Study > C, C++' 카테고리의 다른 글
윈도우 콘솔 프로그램 숨기기(??) (0) | 2016.06.22 |
---|---|
디렉토리 탐색 함수 (0) | 2016.06.22 |
단순 연결 리스트(Linked List) (0) | 2015.02.25 |