在IEnumWbemClassObject调用next会挂起应用程序
Calling next at IEnumWbemClassObject hangs the app
我有一个简单的函数,可以使用WMI获取硬盘序列号,但以下调用将挂起应用程序,就像它进入无限循环一样:
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
完整代码为:
std::string GetHarddiskSerialNumber()
{
HRESULT hres;
// No need to initialize COM in openFL, as it seems that its already initialized when starting windows
// 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 "Failed to initialize COM library. Error code = 0x"; // 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 "Failed to initialize security. Error code = 0x"; // 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 rootcimv2 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.
}
// 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_OperatingSystem"),
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;
ULONG uReturn = 0;
std::string result= "";
while (pEnumerator)
{
// this call will hang the app:
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"SerialNumber", 0, &vtProp, 0, 0);
wstring ws(vtProp.bstrVal, SysStringLen(vtProp.bstrVal));
result = string(ws.begin(), ws.end());
VariantClear(&vtProp);
hr = pclsObj->Get(L"Manufacturer", 0, &vtProp, 0, 0);
wstring ws2(vtProp.bstrVal, SysStringLen(vtProp.bstrVal));
result+=",";
result+= string(ws2.begin(), ws2.end());
VariantClear(&vtProp);
pclsObj->Release();
}
// Cleanup
// ========
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
//pclsObj->Release();
CoUninitialize();
return result;
}
}// name space
知道吗?
我不太确定,但您可以使用0
timeout:进行绑定
HRESULT hr = pEnumerator->Next(WBEM_NO_WAIT, 1, &pclsObj, &uReturn);
来自MSDN:
指定调用的最长时间(以毫秒为单位)返回前阻止。如果使用常量WBEM_INLIMITED(0xFFFFFFFF),调用将被阻止,直到对象可用为止。如果您使用如果值为0(WBEM_NO_WAIT),则调用立即返回,无论是否有对象是否可用。
相关文章:
- 有时ShowWindow从不调用OnShowWindow,主应用程序挂起
- C++多线程应用程序将永远挂起
- 如何在Cocos2d Android应用程序中获得呼叫/挂起事件
- 当后端响应需要很长时间时,WXWIDGET应用程序会挂起
- Eclipse PTP挂起OpenMPI应用程序的启动调试会话
- 安卓 |如何调试应用启动或挂起应用,直到调试器连接
- 调试Win32应用程序挂起
- 为什么我的多线程应用程序挂起
- 在自定义组合框下拉列表控件上等待超过5秒会导致win32 C++应用程序在Windows7中挂起
- linux上的c++应用程序,在终止之前挂起
- X由于应用程序(使用C++、Qt、OpenGL)而挂起
- 使用应用程序验证程序调试时应用程序挂起
- QGraphicsScene - 添加带有少量不透明点的大透明像素图时,应用程序会挂起很长时间
- 应用程序在 的协同创建实例期间挂起.基于 NET 的 COM 对象
- 分析应用程序的挂起转储
- 在IEnumWbemClassObject调用next会挂起应用程序
- MFC应用程序挂起在发出终止信号的线程中
- 在QImage上渲染QWebElement时,QT应用程序挂起
- 查找位置(寻找c++初学者)以创建Windows Vista x64 Ultimate的挂起进程程序
- visualstudio2015在调试期间挂起默认控制台应用程序文件