< prev index next >

src/hotspot/os_cpu/windows_x86/vm_version_windows_x86.cpp

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2006, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 24,28 **** --- 24,121 ---- #include "precompiled.hpp" #include "runtime/os.hpp" #include "runtime/vm_version.hpp" + #include <comdef.h> + #include <Wbemidl.h> + + #pragma comment(lib, "wbemuuid.lib") + + #define VM_MODEL L"Virtual Machine" + + + bool VM_Version::is_in_VM() { + HRESULT hres; + + hres = CoInitializeEx(0, COINIT_MULTITHREADED); + if (hres != S_OK) { + return false; + } + + hres = CoInitializeSecurity(NULL, -1, NULL, NULL, + RPC_C_AUTHN_LEVEL_DEFAULT, + RPC_C_IMP_LEVEL_IMPERSONATE, + NULL, EOAC_NONE, NULL); + if (hres != S_OK) { + CoUninitialize(); + return false; + } + + IWbemLocator* pLoc = NULL; + hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, + IID_IWbemLocator, (LPVOID*)&pLoc); + if (hres != S_OK) { + CoUninitialize(); + return false; + } + + IWbemServices* pSvc = NULL; + BSTR resource = SysAllocString(L"ROOT\\CIMV2"); + hres = pLoc->ConnectServer(resource, + NULL, NULL, 0, NULL, 0, 0, &pSvc); + SysFreeString(resource); + if (hres != S_OK) { + pLoc->Release(); + CoUninitialize(); + return false; + } + + 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 (hres != S_OK) { + pSvc->Release(); + pLoc->Release(); + CoUninitialize(); + return false; + } + + IEnumWbemClassObject* pEnumerator = NULL; + BSTR lang = SysAllocString(L"WQL"); + BSTR query = SysAllocString(L"SELECT Model FROM Win32_ComputerSystem"); + hres = pSvc->ExecQuery(lang, query, + WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, + NULL, &pEnumerator); + SysFreeString(lang); + SysFreeString(query); + if (hres != S_OK) { + pSvc->Release(); + pLoc->Release(); + CoUninitialize(); + return false; + } + + bool result = false; + IWbemClassObject* pclsObj = NULL; + ULONG uReturn = 0; + hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); + if (uReturn != 0) { + VARIANT vtProp; + hres = pclsObj->Get(L"Model", 0, &vtProp, 0, 0); + if (hres == S_OK) { + if (wcscmp(VM_MODEL, vtProp.bstrVal) == 0) { + result = true; + } + VariantClear(&vtProp); + } + pclsObj->Release(); + } + + pSvc->Release(); + pLoc->Release(); + pEnumerator->Release(); + CoUninitialize(); + + return result; + }
< prev index next >