블로그는 나의 힘!
[ Programing ]/Algorithm2013. 11. 14. 09:10

#include "stdafx.h"

//#define _WIN32_DCOM
#include <comdef.h>
#include <Wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")
#include <string>
#include <sstream>

bool getOSName(std::string& outOSName );

int main(int argc, char **argv)
{
std::string outOSName;
getOSName( outOSName );

printf(outOSName.c_str());

return 0;
}



bool getOSName(std::string& outOSName )
{
#define SAFERELEASE_FUNC(x) if (x) { x->Release(); x=NULL; } 

HRESULT hres;

// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres =  CoInitializeEx(0, COINIT_MULTITHREADED); 
if (FAILED(hres))
{
std::ostringstream os;
os << "Failed to initialize COM library. Error code = 0x" << std::hex << hres << std::endl;
outOSName =  os.str();

return false;                  // Program has failed.
}

// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
// Note: If you are using Windows 2000, you need to specify -
// the default authentication credentials for a user by using
// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
// parameter of CoInitializeSecurity ------------------------
hres =  CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL );
if (FAILED(hres))
{
std::ostringstream os;
os << "Failed to initialize security. Error code = 0x" << std::hex << hres << std::endl;
outOSName =  os.str();

CoUninitialize();
return false;                    // Program has failed.
}

// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{  
std::ostringstream os;
os << "Failed to create IWbemLocator object." << " Err code = 0x" << std::hex << hres << std::endl;
outOSName =  os.str();

CoUninitialize();
return false;                 // Program has failed.
}

// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
hres = pLoc->ConnectServer( _bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc );
if (FAILED(hres))
{  
std::ostringstream os;
os << "Could not connect. Error code = 0x" << std::hex << hres << std::endl;
outOSName =  os.str();

SAFERELEASE_FUNC(pLoc);
CoUninitialize();
return false;                // Program has failed.
}

// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
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))
{  
std::ostringstream os;
os << "Could not set proxy blanket. Error code = 0x" << std::hex << hres << std::endl;
outOSName =  os.str();

SAFERELEASE_FUNC(pSvc);
SAFERELEASE_FUNC(pLoc);
CoUninitialize();
return false;               // Program has failed.
}

// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
IEnumWbemClassObject* pEnumerator = NULL;
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))
{  
std::ostringstream os;
os << "Query for operating system name failed."<< " Error code = 0x" << std::hex << hres << std::endl;
outOSName =  os.str();

SAFERELEASE_FUNC(pSvc);
SAFERELEASE_FUNC(pLoc);
CoUninitialize();
return false;               // Program has failed.
}


// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
IWbemClassObject *pclsObj(NULL);
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);

if(0 == uReturn)
{
break;
}

VARIANT vtProp;

// Get the value of the Name property
hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);

// 문자열 변환
std::wstring wstrOSName = vtProp.bstrVal;
outOSName.assign (wstrOSName.begin (), wstrOSName.end ());  
VariantClear(&vtProp);
SAFERELEASE_FUNC(pclsObj);
}

// Cleanup
// ========
SAFERELEASE_FUNC(pSvc);
SAFERELEASE_FUNC(pLoc);
SAFERELEASE_FUNC(pEnumerator);
SAFERELEASE_FUNC(pclsObj);
CoUninitialize();

return true;

 

Posted by Mister_Q