QApplication-无法在Qt中使用IWbemLocator

时间:2018-11-25 16:11:12

标签: qt winapi wmi

我想使用WMI中的Windows API和COM库编写一个Qt应用程序。

我包含<qt_windows.h>来访问窗口API,我之前在 Visual Studio 中尝试过此操作。

但是当我包含Wbemidl.h来声明IWbemLocator变量时,编译器将给我以下error

error: Wbemidl.h: No such file or directory

这是我的code的一部分:

HRESULT hResult = S_OK;                 // Result of initializing com library
BOOL    bIsComLibUninit = FALSE;        // A flag that determine if com library is loaded

// Initialize COM library
hResult = CoInitializeEx(0, COINIT_MULTITHREADED);
// If a previous call of CoInitializeEx occured in this thread
if(hResult == RPC_E_CHANGED_MODE)
{
    OleUninitialize();
    bIsComLibUninit = TRUE;
}
else if(hResult == S_OK)
{
    bIsComLibUninit = TRUE;
}

// If COM library uninitialized
if(bIsComLibUninit == TRUE)
{
    // Initialize COM library
    hResult = CoInitializeEx(0, COINIT_MULTITHREADED);
    if(hResult == S_OK)
    {
        // Initialize security
        hResult = CoInitializeSecurity(NULL, -1, NULL, NULL,
                                       RPC_C_AUTHN_LEVEL_DEFAULT,
                                       RPC_C_IMP_LEVEL_IMPERSONATE,
                                       NULL, EOAC_NONE, NULL);
        if(hResult == S_OK)
        {
            IWbemLocator *pLoc = NULL;
            CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
                             IID_IWbemLocator, (LPVOID *) &pLoc);
            // Do stuffs
        }
    }

    // Uninitialize COM library
    CoUninitialize();
}

1 个答案:

答案 0 :(得分:0)

错误消息告诉编译器找不到头文件。

  1. 尝试在您所在的位置(例如,我本地的路径:C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um)中找到“ Wbemidl.h”,然后将PATH添加到编译器中。另外,请不要忘记.lib参考。
  2. 如果找不到该头文件,请尝试从here下载sdk,然后选择一个适合您的文件: