Failed CoCreateInstance
I have a function where I am calling 开发者_如何学GoCoCreateInstance. 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?
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.
- You must first call the CoInitialize()
The CoInitialize function must be called prior to calling the CoCreateInstance function
- Incorrect COM object registration
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.
- Missing, corrupt of incorrect versions of DLLs
For me, this was happening in different projects configuration, in Debug mode I caught the exception but in Release it did not appear.
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.
精彩评论