A workaround for CoCreateInstance call return code 0x80040154

Source: Internet
Author: User
Tags call back

Introduction

A previous blog post summarizes some of the experiences of developing Windows Thumbnail handler. In a company's actual project, thumbnails need to be implemented for both images and videos. Also add a logo to the top of the image and video file. such as the following:

So consider the implementation of the scheme: (1) The logo resources are compiled into the DLL using BASE64 encoding (2) the company's own pictures and video files for panoramic stitching depends on a string of parameters, and this string of parameters need to be extracted from the file. therefore adopt RecipethumbnailproviderRealize IinitializewithfileThe interface is more appropriate, so that the file path can be obtained, with more flexible operability. (3) The logo resource is decoded using the decoding library that comes with Windows, that is, Windows Image Component ( WIC). Such a thinking, it is very reliable. So I started coding and wrote a function to decode the logo resource:
/** * Decode The base64-encoded string to get the logo resources. */hresult recipethumbnailprovider::getlogofromstring (lpcwstr encodedstring, uint* width, UINT* height, PBYTE* Rawpixels) {istream* pimagestream = NULL; HRESULT hr = E_fail;dword Dwdecodedimagesize = 0;dword Dwskipchars = 0;dword Dwactualformat = 0;if (Cryptstringtobinary (en Codedstring, NULL, CRYPT_STRING_BASE64, NULL, &dwdecodedimagesize, &dwskipchars, &dwactualformat)) {BYTE* Pbdecodedimage = static_cast<byte*> (LocalAlloc (LPTR, dwdecodedimagesize)), if (pbdecodedimage) {if ( Cryptstringtobinary (encodedstring, Lstrlen (encodedstring), Crypt_string_base64, Pbdecodedimage, & Dwdecodedimagesize, &dwskipchars, &dwactualformat)) {Pimagestream = Shcreatememstream (PbDecodedImage, Dwdecodedimagesize); if (pimagestream! = NULL) {iwicimagingfactory* pimagefactory;hr = CoCreateInstance (CLSID_ Wicimagingfactory, NULL, Clsctx_inproc_server, Iid_ppv_args (&pimagefactory)); Loginfo (L "cocreateintance () returns 0x%x", HR); if (SUCCEEDED (HR)) {iwicbitmapdecoder* pdecoder;hr = Pimagefactory->createdecoderfromstream (PImageStream, & Guid_vendormicrosoft, Wicdecodemetadatacacheondemand, &pdecoder); if (SUCCEEDED (HR)) {iwicbitmapframedecode* PBITMAPFRAMEDECODE;HR = Pdecoder->getframe (0, &pbitmapframedecode); if (SUCCEEDED (HR)) {iwicbitmapsource* pbitmapsourceconverted = NULL; WICPixelFormatGUID guidpixelformatsource;hr = Pbitmapframedecode->getpixelformat (&guidPixelFormatSource); if (SUCCEEDED (HR) && (guidpixelformatsource! = guid_wicpixelformat24bppbgr)) {iwicformatconverter* PFORMATCONVERTER;HR = Pimagefactory->createformatconverter (&pformatconverter); if (SUCCEEDED (hr)) {hr = Pformatconverter->initialize (Pbitmapframedecode, GUID_WICPIXELFORMAT24BPPBGR, WICBitmapDitherTypeNone, NULL, 0, Wicbitmappalettetypecustom); if (SUCCEEDED (HR)) {hr = Pformatconverter->queryinterface (& pbitmapsourceconverted);} Pformatconverter->release ();}} ELSE{HR = Pbitmapframedecode->queryinTerface (&pbitmapsourceconverted);} if (SUCCEEDED (HR)) {hr = Pbitmapsourceconverted->getsize (width, height), if (SUCCEEDED (HR)) {WICRect rect = {0, 0, *widt H, *height};*rawpixels = static_cast<byte*> (LocalAlloc (LPTR, (*width) * (*height)); hr = Pbitmapsourceconverted->copypixels (&rect, (*width) * 3, (*width) * (*height) * 3, *rawpixels);} Else{*width = 0;*height = 0;*rawpixels = NULL;} Pbitmapsourceconverted->release ();} Pbitmapframedecode->release ();} Pdecoder->release ();} Pimagefactory->release ();} Pimagestream->release ();}}} LocalFree (pbdecodedimage);} return HR;}

Testing on this machine is perfect when I sign up for COM components to start using. Thought it was done, and yet there was. When testing on another Win7 machine, there is no imaginary logo in the thumbnail. A look at the log file, the discovery has been reported: CoCreateInstance () call back 0x80040154. So the following code is not executed, the logo resources naturally did not load successfully. So why would CoCreateInstance () return to 0x80040154? What does this code mean, eh? Judging from the search results on the web, 0x80040154 is "Class not registered". This means that the COM class is not registered, and there is no class ID under the registry \hkey_classes_root\clsid\. The WIC component is used in our program to decode the image, so is the WIC component class not registered?

Another thought, the development of the use of the WINDOWS10 has been, can be normal operation. Why not on the Windows7? is WIC not supported on the WINDOWS7? This suspicion is obviously not tenable, from the MSDN point of view from the XP SP2 began to support AH: Then it is the wrong argument? With clsid_wicimagingfactory as the keyword, a search was found. one postclsid_wicimagingfactorywas parsed on WINDOWS10 in order to Clsid_wicimagingfactory2: And this GUID does not exist on Windows7 (search the registry to see the result): The natural CoCreateInstance () call will return 0x80040154. The solution is to pass Clsid_wicimagingfactory1To CoCreateInstance (). This will be compatible with both WINDOWS10 and Windows7. Reference links
    1. Http://stackoverflow.com/questions/16697112/why-using-wic-in-my-32-bit-application-fails-in-windows-7-32-bit

A workaround for CoCreateInstance call return code 0x80040154

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.