Com.2 using examples to illustrate the use of COM (set wallpaper with IActiveDesktop)

Source: Internet
Author: User

There are many ways to set up your wallpaper, you can call SystemParametersInfo, modify the registry, and you can do so through the IActiveDesktop interface provided by the Windows shell. The IActiveDesktop interface is a COM interface that needs to be used in a way that calls COM components. This explains how COM is used by applying the IActiveDesktop interface. Strangely, the wininet.h must be included, and the order cannot be wrong, see the following order:#include <afxwin.h> //MFC core components and standard components #include <wininet.h> #include <afxext.h> //MFC ExtendedOtherwise, you will be prompted not to find the definition of iactivedesktop. 1. Call CoInitializeInitializes the COM library on the current thread.:: CoInitialize(NULL);2. Use CoCreateInstancefunction, create an instance of the Activedesktop object, get the IActiveDesktop interface pointer of the Activedesktop object, and add one to the use count of the successful object. iactivedesktop *pactivedesktop=NULL; //create an instance of the Active Desktop HRESULT HR = ::CoCreateInstance(
clsid_activedesktop, NULL, clsctx_inproc_server, iid_iactivedesktop, (void*) &pactivedesktop);
if (FAILED(hr))     { printf("Create activedesktop failure:0x%08x\n",hr); return -1;     }2.1 Call the SetWallpaper method to set the desktop wallpaper. uses_conversion; hr=pactivedesktop-setwallpaper( a2w("F:\\My Documents\\My pictures\\xinsrc_55203051817196711110236.jpg "), 0); if (FAILED(hr))     { printf("SetWallpaper failure:0x%08x\n",hr); return -1;     }2.2 Call the ApplyChanges method to apply the current settings. hr=pactivedesktop-applychanges(ad_apply _all); if (FAILED(hr))     { printf("ApplyChanges failure:0x%08x\n",hr); return -1;     }3. Call the release method to release the interface, when the object's usage count is reduced by one.Pactivedesktop -Release();4. Call CoUninitialize to release the COM library on the current thread.:: CoUninitialize();

Com.2 using an instance to illustrate the use of COM (set wallpaper with IActiveDesktop)

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.