gpt4 book ai didi

c++ - 在 C++ 中使用 Win32_Process.GetOwner

转载 作者:搜寻专家 更新时间:2023-10-31 01:33:39 25 4
gpt4 key购买 nike

我正在尝试使用 WMI 类 Win32_Process 来获取正在运行的进程的列表以及作为每个进程所有者的用户。使用 MSDN (MSDN) 中的枚举示例并简单地将 Win32_OperatingSystem 更改为 Win32_Process,在 C++ 中使用 Win32_Process 枚举进程并不困难。我发现通过使用 Win32_Process 的 GetOwner 方法我可以获得进程所有者的用户和域。在 VB ( MSDN ) 中有一个很好的例子,它表明我可以使用枚举器对象调用 GetOwner 的特定实例以获取枚举中任何给定点的进程信息。

我试图使用“调用提供者方法”的代码示例 ( MSDN ) 来弄清楚如何调用 GetOwner 方法,但我不知道如何让它工作。我不断遇到障碍。通常我得到无效的方法参数。采取以下代码块

        BSTR MethodName = SysAllocString(L"GetOwner");
BSTR ClassName = SysAllocString(L"Win32_Process");

IWbemClassObject* pClass = NULL;
hres = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);
printf("[1] hres = %08x\n", hres);

IWbemClassObject* pInParamsDefinition = NULL;
IWbemClassObject* pOutParams = NULL;
hres = pClass->GetMethod(MethodName, 0, &pInParamsDefinition, &pOutParams);
printf("[2] hres = %08x (%08x, %08x)\n", hres, pInParamsDefinition, pOutParams);

// Execute Method
hres = pSvc->ExecMethod(L"Win32_Process", MethodName, 0, NULL, NULL, &pOutParams, NULL);

VARIANT varReturnValue;
hres = pOutParams->Get(_bstr_t(L"ReturnValue"), 0,
&varReturnValue, NULL, 0);
wprintf(L"The command is: %s\n", V_BSTR(&varReturnValue));

GetOwner 没有输入参数,当我调用 GetMethod 时,pInParamsDefinition 总是返回 NULL,而 pOutParams 返回一个 ptr。由于没有返回到 pInParamsDefinition 的指针,我无法提供输入,因此我不知道如何解决无效方法参数问题。显然 WMI 编程不是我的强项:)

我在这里错过了什么?

最佳答案

断断续续折腾了一段时间后,我终于找到了自己问题的答案。问题是您需要指定要针对哪个对象执行该方法。为此,您调用 GetObject("__PATH", ...) 以获取当前正在枚举的对象的路径,并将 GetObject(一个 BSTR)的结果作为第一个参数传递给 ExecMethod。这告诉方法,在我的例子中是 GetOwner,你试图针对什么执行。这是我编写的一个示例程序,它转储当前的进程列表及其所有者。

#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>

#pragma comment(lib, "wbemuuid.lib")

int main(int argc, char **argv)
{
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 --------------------------

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 to create 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.
hres = pLoc->ConnectServer(
_bstr_t(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 (for example, 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.
}

BSTR MethodName = SysAllocString(L"GetOwner");
BSTR ClassName = SysAllocString(L"Win32_Process");

// get the object containing our desired method
IWbemClassObject* pClass = NULL;
hres = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);
if (FAILED(hres))
{
printf("GetObject hres = %08x\n", hres);
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}

// get the desired method
// in our case, we only need pmethodGetOwner since GetOwner really only has output
IWbemClassObject* pmethodGetOwner = NULL;
hres = pClass->GetMethod(MethodName, 0, NULL, &pmethodGetOwner);
if (FAILED(hres))
{
printf("GetMethod hres = %08x\n", hres);
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}

// spawn the instance of the method
IWbemClassObject* pInInst = NULL;
hres = pmethodGetOwner->SpawnInstance(0, &pInInst);
if (FAILED(hres))
{
printf("SpawnInstance hres = %08x\n", hres);
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}

// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----

// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_Process"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);

if (FAILED(hres))
{
cout << "Query for operating system name failed."
<< " Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // 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;
VARIANT vtProcName;
VARIANT vtDomain;
VARIANT vtUsername;

// Get the value of the Name property
hr = pclsObj->Get(L"Name", 0, &vtProcName, 0, 0);
if (FAILED(hres))
{
printf("Failed to get the process's name\n");
pclsObj->Release();
continue;
}

// Get the PATH to the object in question
// the result in vtProp is similar to '\\name_of_computer\ROOT\CIMV2:Win32_Process.Handle="pid_of_process"'
hr = pclsObj->Get(L"__PATH", 0, &vtProp, 0, 0);
if (FAILED(hres))
{
printf("Failed to get the path to the object\n");
pclsObj->Release();
continue;
}

// Execute Method against the object defined by the __PATH variable
hres = pSvc->ExecMethod(vtProp.bstrVal, MethodName, 0, NULL, NULL, &pmethodGetOwner, NULL);
if (FAILED(hres))
{
wprintf(L"Failed to execute the method against %s\n", vtProp.bstrVal);
pclsObj->Release();
continue;
}

// extract the results
hres = pmethodGetOwner->Get(L"User", 0, &vtUsername, NULL, 0);
if (FAILED(hres))
{
printf("Failed to get the owner's name\n");
pclsObj->Release();
continue;
}

pmethodGetOwner->Get(L"Domain", 0, &vtDomain, NULL, 0);
if (FAILED(hres))
{
printf("Failed to get the owner's domain\n");
pclsObj->Release();
continue;
}

// print the output to screen
wprintf(L"Process: %s. Domain\\User: %s\\%s\n", V_BSTR(&vtProcName), V_BSTR(&vtDomain), V_BSTR(&vtUsername));

// release/cleanup resources we used this go around
VariantClear(&vtProcName);
VariantClear(&vtProp);
VariantClear(&vtDomain);
VariantClear(&vtUsername);
pclsObj->Release();

}

// Cleanup
// ========

pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();

return 0; // Program successfully completed.

}

多亏了 2002 年在 google 群组上的随机发帖 ( "Here is a WBem WMI C++ example" ),我才能够弄清楚这一切。

我希望这对以后的其他人有帮助。

关于c++ - 在 C++ 中使用 Win32_Process.GetOwner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41025357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com