- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用 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/
我在网上搜索但没有找到任何合适的文章解释如何使用 javascript 使用 WCF 服务,尤其是 WebScriptEndpoint。 任何人都可以对此给出任何指导吗? 谢谢 最佳答案 这是一篇关于
我正在编写一个将运行 Linux 命令的 C 程序,例如: cat/etc/passwd | grep 列表 |剪切-c 1-5 我没有任何结果 *这里 parent 等待第一个 child (chi
所以我正在尝试处理文件上传,然后将该文件作为二进制文件存储到数据库中。在我存储它之后,我尝试在给定的 URL 上提供文件。我似乎找不到适合这里的方法。我需要使用数据库,因为我使用 Google 应用引
我正在尝试制作一个宏,将下面的公式添加到单元格中,然后将其拖到整个列中并在 H 列中复制相同的公式 我想在 F 和 H 列中输入公式的数据 Range("F1").formula = "=IF(ISE
问题类似于this one ,但我想使用 OperatorPrecedenceParser 解析带有函数应用程序的表达式在 FParsec . 这是我的 AST: type Expression =
我想通过使用 sequelize 和 node.js 将这个查询更改为代码取决于在哪里 select COUNT(gender) as genderCount from customers where
我正在使用GNU bash,版本5.0.3(1)-发行版(x86_64-pc-linux-gnu),我想知道为什么简单的赋值语句会出现语法错误: #/bin/bash var1=/tmp
这里,为什么我的代码在 IE 中不起作用。我的代码适用于所有浏览器。没有问题。但是当我在 IE 上运行我的项目时,它发现错误。 而且我的 jquery 类和 insertadjacentHTMl 也不
我正在尝试更改标签的innerHTML。我无权访问该表单,因此无法编辑 HTML。标签具有的唯一标识符是“for”属性。 这是输入和标签的结构:
我有一个页面,我可以在其中返回用户帖子,可以使用一些 jquery 代码对这些帖子进行即时评论,在发布新评论后,我在帖子下插入新评论以及删除 按钮。问题是 Delete 按钮在新插入的元素上不起作用,
我有一个大约有 20 列的“管道分隔”文件。我只想使用 sha1sum 散列第一列,它是一个数字,如帐号,并按原样返回其余列。 使用 awk 或 sed 执行此操作的最佳方法是什么? Accounti
我需要将以下内容插入到我的表中...我的用户表有五列 id、用户名、密码、名称、条目。 (我还没有提交任何东西到条目中,我稍后会使用 php 来做)但由于某种原因我不断收到这个错误:#1054 - U
所以我试图有一个输入字段,我可以在其中输入任何字符,但然后将输入的值小写,删除任何非字母数字字符,留下“。”而不是空格。 例如,如果我输入: 地球的 70% 是水,-!*#$^^ & 30% 土地 输
我正在尝试做一些我认为非常简单的事情,但出于某种原因我没有得到想要的结果?我是 javascript 的新手,但对 java 有经验,所以我相信我没有使用某种正确的规则。 这是一个获取输入值、检查选择
我想使用 angularjs 从 mysql 数据库加载数据。 这就是应用程序的工作原理;用户登录,他们的用户名存储在 cookie 中。该用户名显示在主页上 我想获取这个值并通过 angularjs
我正在使用 autoLayout,我想在 UITableViewCell 上放置一个 UIlabel,它应该始终位于单元格的右侧和右侧的中心。 这就是我想要实现的目标 所以在这里你可以看到我正在谈论的
我需要与 MySql 等效的 elasticsearch 查询。我的 sql 查询: SELECT DISTINCT t.product_id AS id FROM tbl_sup_price t
我正在实现代码以使用 JSON。 func setup() { if let flickrURL = NSURL(string: "https://api.flickr.com/
我尝试使用for循环声明变量,然后测试cols和rols是否相同。如果是,它将运行递归函数。但是,我在 javascript 中执行 do 时遇到问题。有人可以帮忙吗? 现在,在比较 col.1 和
我举了一个我正在处理的问题的简短示例。 HTML代码: 1 2 3 CSS 代码: .BB a:hover{ color: #000; } .BB > li:after {
我是一名优秀的程序员,十分优秀!