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
|
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "UWmiSample.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm45 *Form45;
//---------------------------------------------------------------------------
__fastcall TForm45::TForm45(TComponent* Owner)
: TForm(Owner),
pLoc(0),
pSvc(0),
pEnumerator(0)
{
// hres = CoInitialize(NULL);
// Initialize COM. hres = CoInitializeEx(0, COINIT_MULTITHREADED);
}
//---------------------------------------------------------------------------
void __fastcall TForm45::Button1Click(TObject *Sender)
{
hres= CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if(FAILED(hres))
{
MessageDlg("Failed to initialize COM. Error code = 0x"+IntToHex((int)hres,8), mtError, TMsgDlgButtons() << mbOK, 0);
CoUninitialize();
return;
}
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))
{
MessageDlg("Failed to initialize security. Error code = 0x"+IntToHex((int)hres,8), mtError, TMsgDlgButtons() << mbOK, 0);
CoUninitialize();
return;
}
hres = CoCreateInstance(CLSID_WbemLocator, 0,
CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
MessageDlg("WMI Access Error 0x"+IntToHex((int)hres,8), mtError, TMsgDlgButtons() << mbOK, 0);
CoUninitialize();
return;
}
hres = pLoc->ConnectServer(
L"ROOT\\CIMV2", // 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))
{
MessageDlg("WMI Server Access Error 0x"+IntToHex((int)hres,8), mtError, TMsgDlgButtons() << mbOK, 0);
pLoc->Release();
CoUninitialize();
return;
}
hres = CoSetProxyBlanket(
pSvc, // the proxy to set
RPC_C_AUTHN_WINNT, // authentication service
RPC_C_AUTHZ_NONE, // authorization service
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // authentication level
RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation level
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
MessageDlg("WMI Server Access Error 0x"+IntToHex((int)hres,8), mtError, TMsgDlgButtons() << mbOK, 0);
pLoc->Release();
CoUninitialize();
return;
}
hres = pSvc->ExecQuery(
L"WQL",
L"SELECT * FROM Win32_Process",
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
MessageDlg("Query failed. Error code = 0x"+IntToHex((int)hres,8), mtError, TMsgDlgButtons() << mbOK, 0);
pSvc->Release();
pLoc->Release();
CoUninitialize();
return ; // Program has failed.
}
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
IWbemClassObject *pclsObj;
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);
Memo1->Lines->Add("Process Name: "+UnicodeString(vtProp.bstrVal));
VariantClear(&vtProp);
}
pSvc->Release();
pLoc->Release();
CoUninitialize();
}
//--------------------------------------------------------------------------- |
Partager