How to get around thoes system defines

Started by
12 comments, last by magic_man 14 years, 5 months ago
Hi I am facing a problem with thoes defines by the system header files such as winbase.h I have a method in a class: class MyClass { public: void CreateFile(); }; However in winbase.h CreateFile is define to be CreateFileW due to the unicode stuff. And being a #define thing, it is making my method name change too. Is there anyway to avoid this problem, other than changing my method name? I understand that i can #undef it, but that would cause problems to people who wishes to use the actual CreateFile function by windows.
Advertisement
This is just one of those annoying things unfortunately.

So long as you #include your header before any Windows headers, you'll be fine - but you need to ensure that the users of your class do the same.
EDIT: Ignore that, see mattd's point below.

Personally, I'd rename the method if possible. In this case, perhaps something a little more descriptive would be better, such as CreateSettingsFile()?

[Edited by - Evil Steve on November 6, 2009 4:29:48 AM]
Quote:Original post by Evil Steve
So long as you #include your header before any Windows headers, you'll be fine - but you need to ensure that the users of your class do the same.

#include "MyClass.h"#include <windows.h>int main() {    MyClass c;    c.CreateFile();}
?
Quote:Original post by mattd
Quote:Original post by Evil Steve
So long as you #include your header before any Windows headers, you'll be fine - but you need to ensure that the users of your class do the same.

#include "MyClass.h"#include <windows.h>int main() {    MyClass c;    c.CreateFile();}
?
Ah yes, I'm an idiot - ignore that bit...
I guess the only way around is to not use CreateFile. What would be the purpose of your class? Maybe there are better names anyways?
You can also make sure you always #include <windows.h> before anything else (e.g. by putting in a precompiled header). That way, CreateFile will still become CreateFileW, but at least it'll be consistent :-)

You can also get around it with a non-CamelCase naming convention, but that might be a bit much to ask...
I think using a better name is the best solution.

Would it also be possible to use #undef CreateFile in your class
header file? Although this may have other repurcussions that I
am unaware of.
Thanks for the replies :), i guess i have to change my method name. Ahh i hope windows put all their functions in a namespace or something that can solve this problem =)
Quote:Original post by littlekid
Thanks for the replies :), i guess i have to change my method name. Ahh i hope windows put all their functions in a namespace or something that can solve this problem =)


You cannot solve #define aches with namespaces =(
Quote:Original post by phresnel
Quote:Original post by littlekid
Thanks for the replies :), i guess i have to change my method name. Ahh i hope windows put all their functions in a namespace or something that can solve this problem =)


You cannot solve #define aches with namespaces =(


hmm idiot me, thats true. hmm...well would something like that work??

like:

namespace windows_system_a
{
void CreateFile(char* );
};

namespace windows_system_w
{
void CreateFile(wchar_t* );
};

#ifndef UNICODE
#define windows_system windows_system_a
#else
#define windows_system windows_system_w
#endif

I am not sure if this works, but maybe it can greatly reduce the number of defines?? and there won't be so many clashes with the windows files =)

This topic is closed to new replies.

Advertisement