//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <Wbemidl.h> // <- dołączyć bibliotekę
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
# pragma comment(lib, "wbemuuid.lib") // <- dołączyć bibliotekę
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
CoInitialize(NULL);
if(CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0) != S_OK)
{
ShowMessage("Błąd inicjowania interfejsu WMI");
}
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1() // destruktor klasy formularza, trzeba dodać w pliku nagłówkowym w sekcji public:
{
CoUninitialize();
}
//---------------------------------------------------------------------------
void WMIInfo(TStrings *lpList, String wsClass)
{
IWbemLocator *pWbemLocator = NULL;
if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL,
CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
{
IWbemServices *pWbemServices = NULL;
String wsNamespace = String("root\\cimv2");
if(pWbemLocator->ConnectServer(wsNamespace.t_str(), NULL, NULL, NULL, 0, NULL,
NULL, &pWbemServices) == S_OK)
{
IEnumWbemClassObject *pEnumClassObject = NULL;
String wsWQL = "WQL",
wsQuery = "Select * from " + wsClass;
if(pWbemServices->ExecQuery(wsWQL.t_str(), wsQuery.t_str(), WBEM_FLAG_RETURN_IMMEDIATELY,
NULL, &pEnumClassObject) == S_OK)
{
IWbemClassObject *pClassObject = NULL;
ULONG uCount = 1, uReturned;
if(pEnumClassObject->Reset() == S_OK)
{
int iEnumIdx = 0;
while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
{
lpList->Add("");
SAFEARRAY *pvNames = NULL;
if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
{
long vbl, vbu;
SafeArrayGetLBound(pvNames, 1, &vbl);
SafeArrayGetUBound(pvNames, 1, &vbu);
for(long idx = vbl; idx <= vbu; idx++)
{
long aidx = idx;
wchar_t *wsName = 0;
VARIANT vValue;
VariantInit(&vValue);
SafeArrayGetElement(pvNames, &aidx, &wsName);
BSTR bs = SysAllocString(wsName);
HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
SysFreeString(bs);
if(hRes == S_OK)
{
AnsiString lValue;
Variant vVar = *(Variant*)&vValue;
if(vVar.IsArray())
{
for(int i = vVar.ArrayLowBound(); i <= vVar.ArrayHighBound(); i++)
{
Variant vTmp = vVar.GetElement(i);
if(!lValue.IsEmpty()) lValue += ", ";
lValue += VarToStr(vTmp);
}
}
else
{
lValue = VarToStr(vVar);
}
if(!lValue.IsEmpty())
lpList->Add(String(wsName) + ": " + lValue);
}
VariantClear(&vValue);
SysFreeString(wsName);
}
}
if(pvNames) SafeArrayDestroy(pvNames);
iEnumIdx++;
}
}
if(pClassObject) pClassObject->Release();
}
if(pEnumClassObject) pEnumClassObject->Release();
}
if(pWbemServices) pWbemServices->Release();
}
if(pWbemLocator) pWbemLocator->Release();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
::SetCursor(LoadCursor(NULL, IDC_WAIT));
Button1->Enabled = false;
ListBox1->Items->Clear();
WMIInfo(ListBox1->Items, "Win32_UserAccount Where LocalAccount = True"); /* klucz: Win32_UserAccount */
::SetCursor(LoadCursor(NULL, NULL));
Button1->Enabled = true;
}