need help to #undef a typedef

Hello, first of all let me say that I do know that it's not possible to undefine a typedef as I already did a search in that direction.

Now to my problem:
I have a rather old application (not programmed by myself) which uses precompiled headers, the MFC libary and DirectX 6 (with the d3drm libary).

These are the contents of stdafx.h:
1
2
3
4
5
6
7
#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>			// MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

#include "appincludes.h" 

then appincludes.h includes the d3drm header files
1
2
#include <d3drm.h>
#include <d3drmdef.h> 


The problem with that is that afxwin.h includes among other things d2dbasetypes.h which typedefs D3DCOLORVALUE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef D3DCOLORVALUE_DEFINED

//+-----------------------------------------------------------------------------
//
//  Struct:
//      D3DCOLORVALUE
//
//------------------------------------------------------------------------------
typedef struct D3DCOLORVALUE
{
    FLOAT r;
    FLOAT g;
    FLOAT b;
    FLOAT a;

} D3DCOLORVALUE;

#define D3DCOLORVALUE_DEFINED
#endif 


however this conflicts afterwards with the d3drm headers because those include d3dtypes.h which typedef again D3DCOLORVALUE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
typedef struct _D3DCOLORVALUE {
    union {
    D3DVALUE r;
    D3DVALUE dvR;
    };
    union {
    D3DVALUE g;
    D3DVALUE dvG;
    };
    union {
    D3DVALUE b;
    D3DVALUE dvB;
    };
    union {
    D3DVALUE a;
    D3DVALUE dvA;
    };
} D3DCOLORVALUE, *LPD3DCOLORVALUE;


and including the d3drm headers before including afxwin.h is impossible because he then cancels with the error fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>

So I hope anybody can give me some help to solve this problem because I have no idea how to do it :-P
As you say the
d2dbasetypes.h
looks like this:
1
2
3
4
5
6
7
#ifndef D3DCOLORVALUE_DEFINED
typedef struct D3DCOLORVALUE
{
//----SNIP-----
} D3DCOLORVALUE;

#define D3DCOLORVALUE_DEFINED 


So it will only make that typedef if D3DCOLORVALUE_DEFINED is
NOT defined - so see what happens if you do this:



What happens if you do this:
1
2
3
4
5
6
7
8
9
10
//try not to include the D3DCOLORVALUE struct from d2dbasetypes.h header
#define D3DCOLORVALUE_DEFINED

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>			// MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

#include "appincludes.h"  


It's worth a try
Last edited on
As you know, you can't #undef a typedef. But you can use #undef is to reverse a #define, which is the trick I use in my second (main) suggestion below.

Note that with my version VC++, 2008, the MFC headers don't include d2dbasetypes.h. So I couldn't repro your problem or check my "shunt" solution properly.

#1 If you haven't done so already, check the MFC and Windows SDK headers to see it the include is protected by a #ifndef at some level, to see if d2dbasetypes.h can be excluded by #defining something?

#2 Try to shunt the old definition out of the way

1
2
3
4
5
6
7
8
9
10
11
#define D3DCOLORVALUE  D3DCOLORVALUE_OLD_VERSION

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>			// MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

#undef D3DCOLORVALUE

#include "appincludes.h"  


which should work along the lines of this toy program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;

#define D3DCOLORVALUE D3DCOLORVALUE_OLDVERSION
#define display display_old_version

struct D3DCOLORVALUE {
	int a;
	int b;
	int c;
};

void display(const D3DCOLORVALUE& test);

#undef D3DCOLORVALUE
#undef display

struct D3DCOLORVALUE {
	double x;
	double y;
	double z;
};

void display(const D3DCOLORVALUE& test);

int main() {
	D3DCOLORVALUE_OLDVERSION oldTest = {3, 4, 5};

	display_old_version(oldTest);

	D3DCOLORVALUE test = {1.0, 2.0, 3.0};

	display(test);

	return 0;
}

void display_old_version(const D3DCOLORVALUE_OLDVERSION& test) {
	cout << test.a << endl;
	cout << test.b << endl;
	cout << test.c << endl;
}

void display(const D3DCOLORVALUE& test) {
	cout << test.x << endl;
	cout << test.y << endl;
	cout << test.z << endl;
}


#? Failing that, and cleverer ideas from elsewhere, you might need to hack one of the headers. :-(

Andy
Last edited on
@guestgulkan: thanks but I already tried that and he failed because the d2dbasetypes.h afterwards continues to use the D3DCOLORVALUE

@andywestken: you are right, actually I'm using Visual Studio 2010, perhaps it's simply too new?
I'm downloading Visual Studio 2008 right now and see if the problem persists (and if it does I'll try your suggestion)

Thanks again for the help ;)
i havent read all of the posts so I dont know if this is incorrect, but you can #undef typedefs? i thought u could only do that to define macros?
@Aramil of Elixia

Read the very first part of the very first post.
It it still worth to support that application ?
D3DRM.DLL is Direct3D Retained Mode, an older graphics display technology from 1997 that was removed from Windows 7 and Vista.
@modoran:
considering that there is no compiled version of that application available I would say so. I mean I don't want to "keep it alive", I just want it running somewhere :-P

@andywestken:
alright, I got past that problem simply by using an older version of Visual Studio. now I'm facing other problems but these are rather offtopic here and I guess I can solve them myself, thanks ;-)
Topic archived. No new replies allowed.