In a COM example, the compilation error, this example is very simple, a VC to create a new Win32 Console application project, and then select an application that supports MFC, and then add the following function, and in _tmain ( ) Call in.
Testiactivedesktop () {WCHAR wszwallpaper [MAX_PATH]; CString strpath; HRESULT hr; iactivedesktop* PIAD; 1. Initialize the COM library (let Windows load DLLs). This is usually called//CoInitialize (NULL) or other startup code in the program's InitInstance (). MFC programs use AfxOleInit (). CoInitialize (NULL); 2. Creates a COM object using the Active Desktop Component object class provided by the shell. The fourth parameter informs COM what interface is required (this is iactivedesktop). hr = CoCreateInstance (Clsid_activedesktop, NULL, Clsctx_inproc_server, Iid_iactivedesktop, (void**) &piad); if (SUCCEEDED (HR)) {//3. If the COM object is created successfully, the Getwallpaper () method of the object is called. hr = Piad->getwallpaper (wszwallpaper, MAX_PATH, 0); if (SUCCEEDED (HR)) {//4. If Getwallpaper () succeeds, it outputs the name of the file it returns. Note that Wcout is used here to display the Unicode string wszwallpaper. Wcout is//Unicode-specific, the function is the same as cout. Wcout << L "Wallpaper path is:" << wszwallpaper << endl << Endl; } else { cout << _t ("Getwallpaper () failed.") << Endl << Endl; }//5. Releases the interface. Piad->release (); } else {cout << _t ("CoCreateInstance () failed.") << Endl << Endl; }//6. Reclaims the COM library. MFC program does not use this step, it automatically completes. CoUninitialize (); }
Compile, error occurred. Error C2065: ' iactivedesktop ': undeclared identifier. In fact, in Microsoft's documentation, this error has been explained, the article reads as follows:
/*http://support.microsoft.com/kb/196342/en-usPrb:activedesktop compilation Errors under Visual C + + 6.0 Mfcview products, this article applies to. Article id:196342 last Review:june, 2001 revision:1.0 The article was previously published under Q196342symptoms Under Visual C + + 6.0 Microsoft Foundation Classes (MFC), compiling code related to the IActiveDesktop interface would fail With multiple errors, including a "undeclared identifier" for Lpactivedesktop. Note that this occurs specifically when compiling a MFC program using Visual C + + 6.0. There is no report of the problem occurring in previous versions of MFC or in the general Visual C + + 6.0 case where C or C + + programs is being compiled. Back to the Topcausewhen a MFC application includes the Stdafx.h header file, the file *vc98mfcincludeafxdisp.h is includ Ed by default. Within Afxdisp.h the file Shlobj.h (required for activedesktop) is included. But at the this point, Wininet.h have not been included, so certain portions of Shlobj.h is not correctly initialized foR activedesktop Support. The inclusion of Shlobj.h by Afxdisp.h also sets a variable preventing Shlobj.h from being included twice. Later, when a MFC application explicitly and correctly includes Wininet.h followed by Shlobj.h, the previously set Variab Le prevents the second Shlobj.h inclusion and also prevents critical activedesktop definitions, such as for Lpactivedeskto P, from being made. Back to the Topresolutionadd a #include for the Wininet.h header to the Stdafx.h precompiled header file, just above the A Fxdisp.h include. Here's a example of Stdafx.h:///Stdafx.h:include file for standard system include files,... #include <wininet.h># Include <afxdisp.h>//MFC Automation classes ... #endif//!defined (afx_stdafx_h__d5e466a9 ... After correcting Stdafx.h, rebuild the entire project to ensure the precompiled header is re-compiled.*/
That is, in a project built with vc6.0, the generated projects are generated by default and contain Stdafx.h, which in turn contains Afxdisp.h (System header file), and Shlobj.h is included by default in Afxdisp.h. Prior to this, Wininet.h was not included, resulting in _wininet_ not being predefined, and IActiveDesktop's declaration would not compile (requires a _wininet_ declaration first). And even if the latter explicitly contains Wininet.h and Shlobj.h , because the header file has a macro-protected redefinition, it is still invalid.
Specific to my project, different, look at the code:
File stdafx.h ... #include <afx.h> #include <afxwin.h> //MFC core and standard Components#include < Afxext.h> //MFC Extensions#include <afxdtctl.h> //MFC support for Internet Explorer 4 Common controls# ifndef _afx_no_afxcmn_support#include <afxcmn.h> //MFC support for Windows Common controls#endif//_afx_no_ Afxcmn_support#include <iostream>
File afxdtctl.h ... #ifndef __afxdisp_h__ #include <afxdisp.h> #endif ...
File afxdisp.h...//Shell interface support#ifndef _shlobj_h_ #include <shlobj.h> #endif ...
File shlobj.h...////We need to make sure that WININET. H is included before this interface is//used because the COMPONENT structure uses internet_max_url_length//#ifdef _winine t_////Flags and structures used by iactivedesktop//typedef struct _tagwallpaperopt{DWORD dwsize; Size of this Structure. DWORD dwstyle; Wpstyle_* mentioned above}wallpaperopt;typedef wallpaperopt *lpwallpaperopt;typedef Const WALLPAPEROPT * lpcwallpaperopt;typedef struct _tagcomponentsopt{DWORD dwsize; Size of this structure BOOL fenablecomponents; Enable components? BOOL Factivedesktop; Active Desktop enabled?} Componentsopt;typedef componentsopt *lpcomponentsopt;typedef const componentsopt *lpccomponentsopt;typedef struct _TA gcomppos{DWORD dwsize; Size of this structure int ileft; Left of Top-left Corner in screen co-ordinates. int iTop; Top of Top-left Corner in screen co-Ordinates. DWORD Dwwidth; Width in pixels. DWORD Dwheight; Height in pixels. int izindex; Indicates the z-order of the component. BOOL fcanresize; Is the component resizeable? BOOL Fcanresizex; Resizeable in x-direction? BOOL Fcanresizey; Resizeable in y-direction? int ipreferredleftpercent; Left of Top-left corner as percent of the screen width int ipreferredtoppercent; Top of Top-left Corner as percent of screen height}comppos;typedef comppos *lpcomppos;typedef const Comppos *lpccomppos; #define COMPONENT_TOP (0X7FFFFFFF)//Izorder value meaning COMPONENT are at the top//icomptype values#define comp_type_h Tmldoc 0#define comp_type_picture 1#define comp_type_website 2#define comp_type_control 3#define C omp_type_cfhtml 4#define comp_type_max 4typedef struct _tagcomponent{DWORD dwsize; Size of this STructure DWORD DwID; Reserved:set it always to zero. int icomponenttype; One of comp_type_* BOOL fchecked; Is this component enabled? BOOL Fdirty; Had the component been modified and not yet saved to disk? BOOL Fnoscroll; Is the component scrollable? Comppos Cppos; Width, height etc., WCHAR Wszfriendlyname[max_path]; Friendly name of component. WCHAR Wszsource[internet_max_url_length]; URL of the component. WCHAR Wszsubscribedurl[internet_max_url_length]; subscrined url}component;typedef COMPONENT *lpcomponent;typedef const COMPONENT *lpccomponent;//////////////////// Flags for Iactivedesktop::applychanges () #define Ad_apply_save 0x00000001#define AD_APPL Y_htmlgen 0x00000002#define Ad_apply_refresh 0x00000004#define ad_apply_all (Ad_apply_save | Ad_apply_htmlgen | Ad_apply_refresh) #define Ad_apply_Force 0x00000008#define Ad_apply_buffered_refresh 0x00000010//////////////////////////////////////////////Flags fo R iactivedesktop::getwallpaperoptions ()//Iactivedesktop::setwallpaperoptions () #define Wpstyle_center 0#def Ine wpstyle_tile 1#define wpstyle_stretch 2#define Wpstyle_max 3/////////////////////////////////////// Flags for Iactivedesktop::modifycomponent () #define Comp_elem_type 0x00000001#define comp_elem_checked 0x00000002#define comp_elem_dirty 0x00000004#define comp_elem_noscroll 0x00000008#define COMP_ELEM_POS_LEF T 0x00000010#define comp_elem_pos_top 0x00000020#define comp_elem_size_width 0x00000040#define COMP_ELEM_SIZ E_height 0x00000080#define comp_elem_pos_zindex 0x00000100#define comp_elem_source 0x00000200#define COMP_ELEM _friendlyname 0x00000400#define comp_elem_subscribedurl 0x00000800#define comp_elem_all (COMP_ELEM_TYPE | comp_elem_checked | Comp_elem_dirty | Comp_elem_noscroll | Comp_elem_pos_left | Comp_elem_size_width Comp_elem_size_height | Comp_elem_pos_zindex | Comp_elem_source comp_elem_friendlyname)//////////////////////////////////////////////Flags for Iactivedesktop::adddesktopitemwithui () typedef enum tagdti_adtiwui{Dti_addui_default = 0x00000000, DT I_addui_dispsubwizard = 0x00000001,};//////////////////////////////////////////////Flags for IActiveDesktop::AddU RL () #define Addurl_silent 0x0001////Interface for manipulating the Active desktop.//#undef interface#define in Terface Iactivedesktopdeclare_interface_ (IActiveDesktop, IUnknown) {//IUnknown methods Stdmethod (QueryInterface) (This_ refiid riid, void * * PPV) PURE; Stdmethod_ (ULONG, AddRef) (this) PURE; Stdmethod_ (ULONG, Release) (this) PURE; IActiveDesktop methods Stdmethod (applychanges) (This_ DWORD dwFlags) PURE; StdmEthod (Getwallpaper) (This_ lpwstr pwszwallpaper, UINT cchwallpaper, DWORD dwreserved) PURE; STDMETHOD (SetWallpaper) (This_ lpcwstr Pwszwallpaper, DWORD dwreserved) PURE; STDMETHOD (getwallpaperoptions) (This_ lpwallpaperopt Pwpo, DWORD dwreserved) PURE; STDMETHOD (setwallpaperoptions) (This_ lpcwallpaperopt Pwpo, DWORD dwreserved) PURE; STDMETHOD (Getpattern) (This_ lpwstr Pwszpattern, UINT Cchpattern, DWORD dwreserved) PURE; STDMETHOD (Setpattern) (This_ lpcwstr Pwszpattern, DWORD dwreserved) PURE; STDMETHOD (getdesktopitemoptions) (This_ lpcomponentsopt PCO, DWORD dwreserved) PURE; STDMETHOD (setdesktopitemoptions) (This_ lpccomponentsopt PCO, DWORD dwreserved) PURE; STDMETHOD (Adddesktopitem) (This_ lpccomponent Pcomp, DWORD dwreserved) PURE; STDMETHOD (Adddesktopitemwithui) (This_ hwnd hwnd, Lpcomponent Pcomp, DWORD dwreserved) PURE; STDMETHOD (Modifydesktopitem) (This_ lpccomponent Pcomp, DWORD dwFlags) PURE; STDMETHOD (Removedesktopitem) (This_ lpccomponent Pcomp, DWORD dwreserved) PURE; STDMETHOD (Getdesktopitemcount) (This_ lpint lpicount, DWORD dwreserved) PURE; STDMETHOD (Getdesktopitem) (this_ int ncomponent, lpcomponent pcomp, DWORD dwreserved) PURE; STDMETHOD (Getdesktopitembyid) (This_ DWORD DwID, Lpcomponent Pcomp, DWORD dwreserved) PURE; STDMETHOD (generatedesktopitemhtml) (This_ lpcwstr pwszFileName, lpcomponent pcomp, DWORD dwreserved) PURE; STDMETHOD (Addurl) (This_ hwnd hwnd, LPCWSTR Pszsource, Lpcomponent pcomp, DWORD dwFlags) PURE; STDMETHOD (Getdesktopitembysource) (This_ lpcwstr Pwszsource, lpcomponent pcomp, DWORD dwreserved) PURE;}; typedef iactivedesktop * LPACTIVEDESKTOP; #endif//_wininet_ ...
As you can see, the definition of iactivedesktop is meaningful after the #ifdef _wininet_ , but the default is no _wininet_ definition, so the solution is simple, Include the Wininet.h before including the header file Shlobj.h, as follows:
File stdafx.h ... #include <afx.h> #include <afxwin.h> //MFC core and standard Components#include < Afxext.h> //MFC extensions#include <wininet.h>//in front of shlobj include this WinInet header file # include <afxdtctl.h > //MFC support for Internet Explorer 4 Common controls#ifndef _afx_no_afxcmn_support#include <afxcmn.h>< c5/>//MFC support for Windows Common controls#endif//_afx_no_afxcmn_support#include <iostream>..
The reason is that there is such a definition in the WinInet header file:
File wininet.h ... #if!defined (_wininet_) #define _WININET_ ...
' iactivedesktop ': Handling of undeclared identifier