Qt-解决异常报错“QAxBase::setControl: requested control XXX could not be instantiated”

简介: Qt-解决异常报错“QAxBase::setControl: requested control XXX could not be instantiated”

问题说明

      使用Qt开发的过程中,QAxObject是经常用到的一个类,用于操作Windows中各种COM接口,进而实现各类功能。


      WbemScripting.SWbemLocator是我想要通过QAxObject访问的对象,使用下方代码,一般来说会完成实例化并继续执行后续操作。

QAxObject *objIWbemLocator = new QAxObject("WbemScripting.SWbemLocator");

   然而很多人会触发下方错误提示,翻译过来就是无法实例化请求的对象。

      经过我多方调研,查清了问题缘由,是因为COM未初始化导致。QAxObject既然用于操作COM接口,那COM的初始化也显得至关重要。

      解决问题的办法如下所示。

解决方案

      QleInitialize函数是一个Windows API函数。它的作用是在当前单元(apartment)初始化组件对象模型(COM)库,将当前的并发模式标识为STA(single-thread apartment——单线程单元),并启用一些特别用于OLE技术的额外功能。除了CoGetMalloc和内存分配函数,应用程序必须在调用COM库函数之前初始化COM库。

      我们在实例化前调用QleInitialize函数来初始化COM,如下所示。并在操作后调用OleUninitialize函数取消COM初始化。至此,该问题解决。

OleInitialize(0);
QAxObject *objIWbemLocator = new QAxObject("WbemScripting.SWbemLocator");
// 系列操作
OleUninitialize();

      近期在回顾用Qt开发的许多项目,对比较好或者我觉得会有人需要的内容,会同步更新在CSDN,希望能帮助到其他人。近几年受疫情影响,大家的生活和工作都十分不易,希望未来一年比一年好,我们一同努力!


      如果文章帮助到你了,可以点个赞让我知道,我会很快乐~加油!

相关文章
|
Java
Appium问题解决方案(8)- selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Could not sign with default certificate.
Appium问题解决方案(8)- selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Could not sign with default certificate.
903 0
Appium问题解决方案(8)- selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Could not sign with default certificate.
|
5月前
|
JavaScript
Uncaught runtime errors: × ERROR Avoided redundant navigation to current location: “/xxx“.
Uncaught runtime errors: × ERROR Avoided redundant navigation to current location: “/xxx“.
62 0
|
12月前
|
开发者 iOS开发
Xcode运行报错The operation couldn’t be completed. Unable to launch xxx because it has an invalid code...
Xcode运行报错The operation couldn’t be completed. Unable to launch xxx because it has an invalid code...
664 0
|
Java Spring
Initialization failed for ‘https://start.spring.io‘ Please check URL, network and proxy settings解决办法
Initialization failed for ‘https://start.spring.io‘ Please check URL, network and proxy settings解决办法
2084 1
Initialization failed for ‘https://start.spring.io‘ Please check URL, network and proxy settings解决办法
编译mate-control-center:error: required directory ./help does not exist
编译mate-control-center:error: required directory ./help does not exist
82 0
|
程序员 iOS开发 开发者
iOS开发:程序打包提示Ignoring file XXX missing required architecture arm7 in file XXX错误
前段时间在开发过程中遇到一个iOS打包报错Ignoring file XXX missing required architecture arm7 in file XXX的问题,然后回想了一下问题所在,是因为集成了ijk播放器插件,然后ijk三方库不支持arm7造成的报错。
144 0
iOS开发:程序打包提示Ignoring file XXX missing required architecture arm7 in file XXX错误
成功解决404 Not Found Not Found The requested URL was not found on the server. If yo
成功解决404 Not Found Not Found The requested URL was not found on the server. If yo