'IActiveDesktop' : undeclared identifier 的处理

在一个COM例子中,编译出现了错误,这个例子很简单,用vc新建一个Win32 Console Application项目,然后选择An application that supports MFC,然后加入如下函数,并在_tmain()里调用。

TestIActiveDesktop()
{
    WCHAR   wszWallpaper [MAX_PATH];
    CString strPath;
    HRESULT hr;
    IActiveDesktop* pIAD; 

    // 1. 初始化COM库(让Windows加载DLLs)。通常是在程序的InitInstance()中调用
    // CoInitialize ( NULL )或其它启动代码。MFC程序使用AfxOleInit()。 

    CoInitialize ( NULL ); 

    // 2. 使用外壳提供的活动桌面组件对象类创建COM对象。
    // 第四个参数通知COM需要什么接口(这里是IActiveDesktop). 

    hr = CoCreateInstance ( CLSID_ActiveDesktop,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_IActiveDesktop,
        (void**) &pIAD ); 

    if ( SUCCEEDED(hr) )
    {
        // 3. 如果COM对象被创建成功,则调用这个对象的GetWallpaper() 方法。
        hr = pIAD->GetWallpaper ( wszWallpaper, MAX_PATH, 0 ); 

        if ( SUCCEEDED(hr) )
        {
            // 4. 如果 GetWallpaper() 成功,则输出它返回的文件名字。
            // 注意这里使用wcout 来显示Unicode 串wszWallpaper.  wcout 是
            // Unicode 专用,功能与cout.相同。
            wcout << L"Wallpaper path is:    " << wszWallpaper << endl << endl;
        }
        else
        {
            cout << _T("GetWallpaper() failed.") << endl << endl;
        } 

        // 5. 释放接口。
        pIAD->Release();
    }
    else
    {
        cout << _T("CoCreateInstance() failed.") << endl << endl;
    } 

    // 6. 收回COM库。MFC 程序不用这一步,它自动完成。
    CoUninitialize();
}

  编译,发生错误.编译错误信息为 error C2065: ‘IActiveDesktop‘ : undeclared identifier .其实在微软的文档中,对此错误已经有了说明,文章内容如下:

/*
http://support.microsoft.com/kb/196342/en-us

PRB: ActiveDesktop Compilation Errors Under Visual C++ 6.0 MFC
View products that this article applies to.
Article ID : 196342
Last Review : June 18, 2001
Revision : 1.0
This article was previously published under Q196342
SYMPTOMS
Under Visual C++ 6.0 Microsoft Foundation Classes (MFC), compiling code related to the IActiveDesktop interface will fail with multiple errors, including an "undeclared identifier" for LPACTIVEDESKTOP. 

Note that this occurs specifically when compiling an 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 are being compiled.
Back to the top

CAUSE
When an MFC application includes the Stdafx.h header file, the file *Vc98MfcIncludeAfxdisp.h is included by default. Within Afxdisp.h the file Shlobj.h (required for ActiveDesktop) is included. But at this point, Wininet.h has not been included, so certain portions of Shlobj.h are 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 an MFC application explicitly and correctly includes Wininet.h followed by Shlobj.h, the previously set variable prevents the second Shlobj.h inclusion and also prevents critical ActiveDesktop definitions, such as for LPACTIVEDESKTOP, from being made.
Back to the top

RESOLUTION
Add a #include for the Wininet.h header to the Stdafx.h precompiled header file, just above the Afxdisp.h include. Here‘s an 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 that the precompiled header is re-compiled. 

*/

也就是说,在使用vc6.0的向导生成的项目中,生成的project都默认生成并包含Stdafx.h,此头文件中又默认包涵Afxdisp.h(系统头文件),而Shlobj.h又默认包含在Afxdisp.h中,而在此之前Wininet.h并没有包含进来,导致_WININET_没有预定义,IActiveDesktop的声明就不会编译(需要首先有_WININET_的声明).而就算后面明确的包含了Wininet.h和Shlobj.h,由于头文件的有宏保护重定义,所以依然无效.

具体到我的项目,有所不同,看代码:

//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 _WININET_
//
//  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 _tagCOMPPOS
{
    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 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 is at the top

// iCompType values
#define COMP_TYPE_HTMLDOC       0
#define COMP_TYPE_PICTURE       1
#define COMP_TYPE_WEBSITE       2
#define COMP_TYPE_CONTROL       3
#define COMP_TYPE_CFHTML        4
#define COMP_TYPE_MAX           4

typedef 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_APPLY_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 for IActiveDesktop::GetWallpaperOptions()
//           IActiveDesktop::SetWallpaperOptions()
#define WPSTYLE_CENTER      0
#define 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_LEFT      0x00000010
#define COMP_ELEM_POS_TOP       0x00000020
#define COMP_ELEM_SIZE_WIDTH    0x00000040
#define COMP_ELEM_SIZE_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,
    DTI_ADDUI_DISPSUBWIZARD         = 0x00000001,
};

////////////////////////////////////////////
// Flags for IActiveDesktop::AddUrl()
#define ADDURL_SILENT           0X0001

//
//  Interface for manipulating the Active Desktop.
//

#undef INTERFACE
#define INTERFACE IActiveDesktop

DECLARE_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_
...

  

可以看出,IActiveDesktop 的定义是在#ifdef  _WININET_   后才有意义的,但是默认是没有_WININET_定义的,所以解决方案也很简单,在包含头文件shlobj.h前包含Wininet.h即可,如下所示:

//File stdafx.h
...
#include <afx.h>
#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions

#include <wininet.h>		//在shlobj的前面include这个wininet头文件

#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>
...

  

原因就是在wininet头文件中有这样的定义:

//File wininet.h
...
#if !defined(_WININET_)
#define _WININET_
...

  

'IActiveDesktop' : undeclared identifier 的处理

时间: 2024-10-09 03:38:36

'IActiveDesktop' : undeclared identifier 的处理的相关文章

error C2065: &apos;TIME_UTC&apos; : undeclared identifier

boost_1_55_0\boost\threadpool\task_adaptors.hpp(138) : error C2065: 'TIME_UTC' : undeclared identifier boost_1_55_0   version threadpool-0_2_5   version 解决办法:(This patch fixes compilation) --- dependencies/threadpool/include/threadpool/task_adaptors.

error C2065: &#39;assert&#39; : undeclared identifier

F:\VC6.0 : error C2065: 'assert' : undeclared identifier 导入#include <assert.h> error C2065: 'assert' : undeclared identifier

error C2065: &#39;CArchiveStream&#39; : undeclared identifier

release:模式下 问题: 在导入JPEG文件时要使用到  CArchiveStream类  但是编译的时候会出现  'CArchiveStream'   :   undeclared   identifier    编译错误.即使追加了 #include <afxpriv.h> #include <afxpriv2.h> 有时候还是不能解决问题. 解决方法: 在预编译文件stdafx.h文件中追加  #ifndef _AFX_NO_OLE_SUPPORT #include &

【原】iOS 同时重写setter和getter时候报错:Use of undeclared identifier &#39;_name&#39;;did you mean &#39;name&#39;

写了那么多的代码了,平时也没有怎么注意会报这个错误,因为平时都很少同时重写setter和getter方法,一般的话,我们大概都是使用懒加载方法,然后重写getter方法,做一个非空判断.然后有时候根据需求,要同时重写属性的setter和getter方法.系统就会报错误::Use of undeclared identifier '_name';did you mean 'name' MARK:如果不想了解太多,直接的解决方法: 在@implementation 实现中添加一行代码就OK了 @sy

VS2008编译错误:error C2065: &#39;PMIB_TCPSTATS&#39; : undeclared identifier c:\program files (x86)\microsoft sdks\windows\v7.0a\include\iphlpapi.h 411

安装了VS2008编译之前的程序,结果出现了编译错误,以为是VS2008的Sp1补丁没装好,重装补丁后还是不行,编译错误如下: 双击错误会定位在iphlpapi.h中, 一个可行的解决办法是:把iphlpapi.h文件的line386---line421 都注释掉了: 然后再尝试编译,编译成功,运行也没有问题. 看起来,这几行代码不会影响程序运行. VS2008编译错误:error C2065: 'PMIB_TCPSTATS' : undeclared identifier c:\program

【MFC】error C2065: &#39;AfxGetFileName&#39; : undeclared identifier

一开始在debug目录下无任何错误 在release下总是 --------------------Configuration: maizang - Win32 Debug--------------------Compiling...APPINIT.CPPC:\Program Files\Microsoft Visual Studio\VC98\MFC\SRC\APPINIT.CPP(90) : error C2065: 'AfxGetFileName' : undeclared identi

use of undeclared identifier *** , did you mean ***. in xcode

A property is not the same thing os a instance variable, you should read a little bit of them, there's plenty of sources in the internet. Summarizing, a property is the combination of the instance variable (by default, automaticated declaraded with a

Undeclared identifier:XXX

未识别错误,是因拼写错误或找不到定义文件. 下面列举一些类型和函数用到的单元. _Stream ADODB_TLB akTop, akLeft, akRight, akBottom Controls Application (the variable not a type) Forms Beep SysUtils or Windows (different functions) CGID_EXPLORER ShlObj CN_BASE Controls CoInitialize ActiveX

apache源码编译时报错ssl_engine_init.c:546: error: &#39;STACK&#39; undeclared (first use in this function)

操作系统:centos6.4 X86_64 apache版本:httpd-2.2.6 openssl版本:openssl-1.0.1e 源码编译apache时报如下错误: ssl_engine_init.c: In function 'ssl_init_ctx_protocol': ssl_engine_init.c:390: warning: assignment discards qualifiers from pointer target type ssl_engine_init.c:39