4

I'm trying to include the following definitions for GDI+ into my Win32 C++ project that is compiled under Visual Studio 2017:

#include <objidl.h>
#include <gdiplus.h>
#pragma comment (lib,"Gdiplus.lib")

I need to compile this project to support Windows XP. So in the project properies I selected: Platform Toolset as Visual Studio 2017 - Windows XP (v141_xp):

enter image description here

But when I compile it the GDI+ library gives me this:

1>c:\program files (x86)\microsoft sdks\windows\v7.1a\include\objbase.h(239): error C2760: syntax error: unexpected token 'identifier', expected 'type specifier'
1>c:\program files (x86)\microsoft sdks\windows\v7.1a\include\gdiplusheaders.h(891): error C4596: 'EmfToWmfBits': illegal qualified name in member declaration
1>c:\program files (x86)\microsoft sdks\windows\v7.1a\include\gdiplusstringformat.h(220): error C4596: 'GetTrimming': illegal qualified name in member declaration

Any idea how to fix this?

c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • This is extremely frustrating. Does anyone even use that GDI+ now? I [found this](https://stackoverflow.com/questions/7305614/include-gdiplus-h-causes-error), but none of it works if I try to build for `Windows XP (v141_xp)`. I'm still getting the same compiler errors as I've shown above. – c00000fd Jul 13 '19 at 17:27

4 Answers4

9

Add this line before the very first(!) #include of COM-related header to fix objbase.h(239): error C2760: syntax error: unexpected token 'identifier', expected 'type specifier' :

typedef struct IUnknown IUnknown;

This fix works, because the line in objbase.h(239) mentioned in the error contains static_cast<IUnknown*>(*pp); despite that IUnknown still haven't been declared in that place.

Кое Кто
  • 445
  • 5
  • 9
1

I kinda got it to compile, but this is definitely not a good solution. I'm posting it here as a temp workaround until Microsoft gets their heads out of their ___es. Also if anyone finds a better way, please let me know.

I basically had to downgrade the entire project to Visual Studio 2015 - Windows XP (v140_xp) just to compile one badly written library:

enter image description here

This created a problem of its own with the std libraries:

1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cstdio(50): error C4995: 'sprintf': name was marked as #pragma deprecated
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cstdio(53): error C4995: 'vsprintf': name was marked as #pragma deprecated
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cstring(20): error C4995: 'strcat': name was marked as #pragma deprecated
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cstring(21): error C4995: 'strcpy': name was marked as #pragma deprecated
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cwchar(29): error C4995: 'swprintf': name was marked as #pragma deprecated
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cwchar(30): error C4995: 'vswprintf': name was marked as #pragma deprecated
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cwchar(32): error C4995: 'wcscat': name was marked as #pragma deprecated
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cwchar(34): error C4995: 'wcscpy': name was marked as #pragma deprecated

So I had to shunt those errors of unsafe functions:

#pragma warning( push )
#pragma warning( disable: 4995 )
#include <stdio.h>
#include <new>
#include <string>
#pragma warning( pop )

Which is far from ideal!

(You're basically sacrificing security of the app just to compile that damn GDI+ library.)

c00000fd
  • 20,994
  • 29
  • 177
  • 400
1

There's a way to get this to work if you're prepared to edit the Windows header files.

In objbase.h, comment out line 239 or change it to:

static_assert (std::is_base_of <IUnknown *, *pp>::value, "pp must derive from IUnknown");

In gdiplusheaders.h, line 891, remove the redundant qualifier (Metafile::).

In gdiplusstringformat.h, line 220, remove the redundant qualifier (StringFormat::).

Hopefully, that will fix things for you without breaking anything.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
1

Although the question is old, just adding what worked for me.

In my case including windows.h and compiling with VS2017 v141_xp toolset was causing me error: syntax error: unexpected token 'identifier', expected 'type specifier'.

This resolved my issue link

Ishwar
  • 68
  • 6