CoCreateInstance 失败
我有一个调用 CoCreateInstance 的函数。该函数被调用多次并且它可以工作,但是有一次 CoCreateInstance 失败并出现“错误的变量类型”错误。问题是无论成功还是失败,参数总是相同的。什么可能导致这种情况发生?
I have a function where I am calling CoCreateInstance. This function is called multiple times and it works, however one time the CoCreateInstance fails with the "Bad variable type" error. The thing is the parameters are always the same both when it succedes and when it fails. What could cause this to happen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的代码中还有一个函数调用 CoCreateInstance(...)。有时它有效,有时则无效,而无需更改代码。
但是当查看返回的 HRESULT 时,结果是内存不足错误,“E_OUTOFMEMORY 内存不足”。我确实做了一些有关该主题的研究,发现发生这种情况的原因如下。
必须在调用 CoCreateInstance 函数之前调用 CoInitialize 函数
当 COM 对象(或代理存根)未正确注册时,通常会发生这种情况。您可以使用 Oleview 工具通过尝试实例化对象来检查 COM 对象的注册情况。
Oleview.exe 包含在 Windows SDK 中,位置通常位于 C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin。
如果不存在,您可以尝试安装适用于 Windows Server 2008 的 Microsoft Windows SDK:http://www.microsoft.com/downloads/details.aspx?FamilyId=F26B1AA4-741A-433A-9BE5-FA919850BDBF&displaylang=en。
注意:它不仅适用于Windows 2008,它只是以最新的Windows版本命名,并且支持Windows Server 2003; Windows Server 2008; Windows Vista; Windows XP。
对我来说,这种情况发生在不同的项目配置中,在调试模式下我捕获了异常,但在发布中它没有出现。
I also had a function in my code calling CoCreateInstance(...). When one time it worked and another time it did not, without changing the code.
But when looking at the HRESULT which was returned, the result was an Out of Memeory Error, "E_OUTOFMEMORY Ran out of memory." I did do some research regarding the subjects and found the following reasons that this can occur.
The CoInitialize function must be called prior to calling the CoCreateInstance function
This can often occurred when the COM object (or the proxy-stub) wasn't registered properly. You can check the registration of the COM object using the Oleview tool by trying to instantiate the object.
The Oleview.exe is included in Windows SDK, the location is typically at C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin.
If it is not there you can try installing Microsoft Windows SDK for Windows Server 2008: http://www.microsoft.com/downloads/details.aspx?FamilyId=F26B1AA4-741A-433A-9BE5-FA919850BDBF&displaylang=en.
Note: it is not only for Windows 2008, it is just named after the recent Windows version and it supports Windows Server 2003; Windows Server 2008; Windows Vista; Windows XP.
For me, this was happening in different projects configuration, in Debug mode I caught the exception but in Release it did not appear.
我修好了这个。
该调用是从不同的线程进行的。当调用成功时,是从主线程调用的。失败时,源是另一个线程,并且不会事先调用 CoInitialize()。
调用CoInitialize解决了这个问题。
I fixed this.
The call was made from different threads. When the call succeded, it was called from the main thread. On fail, a different thread was the source and CoInitialize() wasn't called beforehand.
Calling CoInitialize solved the problem.