'DLLVERSIONINFO'项目从VS2008更新到VS2012后编译报错

'DLLVERSIONINFO' Compile error after updating project to VS2012 from VS2008

我有一个非常古老的 C++ 应用程序,多年来,在我之前,源代码已经迁移了很多次。它现在在 VS2008 下构建和编译。我决定迁移到 VS2012,这样做之后,我现在收到以下编译器错误:error C2371: 'DLLVERSIONINFO' : redefinition;不同的基本类型

我没有更改任何一行代码。我只是将项目从 VS2008 更新到 VS2012。

在访问 MSDN 并稍作尝试后,我在 _DllVersionInfo 结构的最后注释掉了结构“DLLVERSIONINFO”的最后一部分。

现在项目编译并且 运行!!!

我的问题如下: 1) 这是否意味着微软改变了这个结构在 VS2012 中的工作方式? 2) 如果我永远离开评论部分,我可能 运行 遇到什么样的问题?

下面是源代码。

// 1998 Microsoft Systems Journal
//
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// http://www.microsoft.com/msj/0498/c0498.aspx

#ifndef __MODULEVER_H
#define __MODULEVER_H

// tell linker to link with version.lib for VerQueryValue, etc.
#pragma comment(linker, "/defaultlib:version.lib")

#ifndef DLLVERSIONINFO
// following is from shlwapi.h, in November 1997 release of the Windows SDK

typedef struct _DllVersionInfo
{
    DWORD cbSize;
    DWORD dwMajorVersion;                   // Major version
    DWORD dwMinorVersion;                   // Minor version
    DWORD dwBuildNumber;                    // Build number
    DWORD dwPlatformID;                     // DLLVER_PLATFORM_*
} /*DLLVERSIONINFO*/; // commented out this part

// Platform IDs for DLLVERSIONINFO
#define DLLVER_PLATFORM_WINDOWS         0x00000001      // Windows 95
#define DLLVER_PLATFORM_NT              0x00000002      // Windows NT

#endif // DLLVERSIONINFO

// value must pair with static LPCTSTR Keys[]  in method CString
//  CMainVersion::GetFile_VS_VERSION_INFO(...)
typedef  enum  {
    eCompanyName = 0,
    eFileDescription = 1,
    eFileVersion = 2,
    eInternalName = 3,
    eLegalCopyright = 4,
    eOriginalFilename = 5,
    eProductName = 6,
    eProductVersion = 7,
    eAllInfo = 8
} E_VS_VERSION_INFO;

class CModuleVersion : public VS_FIXEDFILEINFO 
{
protected:
 BYTE* m_pVersionInfo;  // all version info

 struct TRANSLATION {
    WORD langID;            // language ID
    WORD charset;           // character set (code page)
} m_translation;

public:
 CModuleVersion();
 virtual ~CModuleVersion();
 static CString GetModuleVersion();
 static CString GetModuleHistory();

 BOOL  GetFileVersionInfo(LPCTSTR modulename);
 CString    GetValue(LPCTSTR lpKeyName);
 static BOOL DllGetVersion(LPCTSTR modulename, DLLVERSIONINFO& dvi);
};

class CMainVersion 
{
public:
 CMainVersion();
 virtual ~CMainVersion();

static CString GetFileVersion(const CString & csModuleName);
static CString GetFile_VS_VERSION_INFO(const CString & csModuleName,
E_VS_VERSION_INFO eInfo);
static CString GetFileAndDllVersion(const CString & csModuleName);
static CString GetAllVersions();
};
#endif

显然,Microsoft 在 VS2008 和 VS2012 之间的某个时间添加了他们自己的 DLLVERSIONINFO 声明(在 shlwapi.h 中)。您不应该再手动声明它,只需使用 #include <shlwapi.h> 即可。

无论哪种方式,#ifndef DLLVERSIONINFO 都不会起作用,因为 DLLVERSIONINFO 不是使用 #define 语句声明的。如果要根据编译器版本有条件地定义 DLLVERSIONINFO,请改用 _MSC_VER,例如:

#if _MSC_VER >= 1700 // 1700 == VS2012
#include <shlwapi.h>
#else
// following is from shlwapi.h, in November 1997 release of the Windows SDK

typedef struct _DllVersionInfo
{
    DWORD cbSize;
    DWORD dwMajorVersion;                   // Major version
    DWORD dwMinorVersion;                   // Minor version
    DWORD dwBuildNumber;                    // Build number
    DWORD dwPlatformID;                     // DLLVER_PLATFORM_*
} DLLVERSIONINFO;

// Platform IDs for DLLVERSIONINFO
#define DLLVER_PLATFORM_WINDOWS         0x00000001      // Windows 95
#define DLLVER_PLATFORM_NT              0x00000002      // Windows NT

#endif // _MSC_VER