#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")
BOOL wmi_run();
BOOL wmi_checkVolumeBitlocker();
BOOL wmi_close();
IWbemLocator *pLoc = NULL;
IWbemServices *pSvc = NULL;
int main(int argc, char **argv)
{
wmi_run();
wmi_checkVolumeBitlocker();
system("pause");
wmi_close();
}
//
// get Win32_EncryptableVolume Class
BOOL wmi_checkVolumeBitlocker()
{
HRESULT hres;
IEnumWbemClassObject* pEnumerator = NULL;
// get volumes
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_EncryptableVolume"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres)) {
cout << "Query for processes failed. "
<< "Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return FALSE; // Program has failed.
}
else {
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
while (pEnumerator) {
hres = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(0 == uReturn)
break;
VARIANT vtProp;
hres = pclsObj->Get(_bstr_t(L"DeviceID"), 0, &vtProp, 0, 0);
// out the VolumeID
printf("\nVolume: %ls\n", vtProp.bstrVal);
// fit string
wstring tmp = vtProp.bstrVal;
tmp = tmp.substr(4);
wstring wstrQuery = L"Win32_EncryptableVolume.DeviceID='\\\\?\\";
wstrQuery += tmp;
wstrQuery += L"\'";
//
// GET CONVERSION STATUS METHOD
// http://msdn.microsoft.com/en-us/library/aa376433(VS.85).aspx
IWbemClassObject *pclsObj1 = NULL;
hres = pSvc->ExecMethod(
bstr_t(wstrQuery.c_str()),
bstr_t(L"GetConversionStatus"),
0,
NULL,
NULL,
&pclsObj1,
NULL );
if ( FAILED(hres) ) {
cout << "Query for processes failed. "
<< "Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return FALSE; // Program has failed.
}
else {
VARIANT vtProp1;
hres = pclsObj1->Get(_bstr_t(L"ConversionStatus"), 0, &vtProp1, 0, 0);
if(hres == 2150694912)
printf("Volume is locked");
else {
printf("ConversionStatus:\t");
switch((int)vtProp1.bstrVal) {
case 0:
printf("FULLY DECRYPTED");
break;
case 1:
printf("FULLY ENCRYPTED");
break;
case 2:
printf("ENCRYPTION IN PROGRESS");
break;
case 3:
printf("DECRYPTION IN PROGRESS");
break;
case 4:
printf("ENCRYPTION PAUSED");
break;
case 5:
printf("DECRYPTION PAUSED");
break;
default:
printf("unknown");
break;
}
}
printf("\n");
VariantClear( &vtProp1 );
}
//
// GET ENCRYPTION METHOD
// http://msdn.microsoft.com/en-us/library/aa376434(VS.85).aspx
hres = pSvc->ExecMethod(
bstr_t(wstrQuery.c_str()),
bstr_t(L"GetEncryptionMethod"),
0,
NULL,
NULL,
&pclsObj1,
NULL );
if ( FAILED(hres) ) {
cout << "Query for processes failed. "
<< "Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return FALSE; // Program has failed.
}
else {
VARIANT vtProp1;
hres = pclsObj1->Get(_bstr_t(L"EncryptionMethod"), 0, &vtProp1, 0, 0);
printf("EncryptionMethod:\t");
switch((int)vtProp1.bstrVal) {
case 0:
printf("NONE");
break;
case 1:
printf("AES 128 WITH DIFFUSER");
break;
case 2:
printf("AES 256 WITH DIFFUSER");
break;
case 3:
printf("AES 128");
break;
case 4:
printf("AES 256");
break;
default:
printf("unknown");
break;
}
printf("\n");
VariantClear( &vtProp1 );
}
VariantClear( &vtProp );
}
pEnumerator->Release();
}
return TRUE;
}
//
// Step 1-5 at:
// http://msdn.microsoft.com/en-us/library/aa390423(VS.85).aspx
BOOL wmi_run()
{
HRESULT hres;
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres)) {
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // 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,
// 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 )) {
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // 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 )) {
cout << "Failed tocreate IWbemLocator object."
<< " Err code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
//IWbemServices *pSvc = NULL;
// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
// Security MUST RUN AS ADMINISTRATOR
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2\\Security\\MicrosoftVolumeEncryption"),
// 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)) {
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
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)) {
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
return 0;
}
//
// WMI Session close
BOOL wmi_close()
{
// Cleanup
// ========
pSvc->Release();
pLoc->Release();
CoUninitialize();
return TRUE; // Program successfully completed.
}