2

In my project I declared a function named ReplaceFile, yet when referencing it in my cpp, I get the strangest error. It appends a 'W' to the end of the method I wrote.

enter image description here

Why on earth would it think I wrote ReplaceFileW? I already did a project search for ReplaceFileW and it comes up with nothing.

If you need anything else just comment, otherwise, is this a simple fix?

Here is the declaration in the header for ReplaceFile and it's overload:

 // Description: replace an existing file into the package
void ReplaceFile(std::string path, std::string pathInPackage, void(*replaceProgress)(void*, DWORD,
        DWORD) = NULL, void *arg = NULL);

// Description: replace an existing file into the package
void ReplaceFile(std::string path, StfsFileEntry *entry, std::string pathInPackage,
        void(*replaceProgress)(void*, DWORD, DWORD) = NULL, void *arg = NULL);

Thanks for your time.

mrg95
  • 2,371
  • 11
  • 46
  • 89
  • See https://stackoverflow.com/questions/2161770/visual-c-automatically-appending-a-or-w-to-end-of-function and https://stackoverflow.com/questions/7424383/what-is-the-difference-between-the-a-and-w-functions-in-the-win32-api – sashoalm Aug 05 '16 at 08:18
  • 1
    @sashoalm: I think the first is close as a duplicate, but it's a negative-quality question (the OP relating fantasy as facts, instead of the facts). – Cheers and hth. - Alf Aug 05 '16 at 08:25
  • @Cheersandhth.-Alf Do believe me that I did try to search for my issue. But yes that first one does look like a duplicate to me but doesn't include a clear solution – mrg95 Aug 05 '16 at 08:29
  • 1
    @mc360pro I posted them so they appear in the Linked section (and your own question will appear in them). – sashoalm Aug 05 '16 at 08:51

1 Answers1

3

It's because of the Windows function ReplaceFile. As just about all WINAPI functions it comes in two variants:

  1. ReplaceFileW for wide-character strings
  2. ReplaceFileA for ASCII strings

Which one is used depends on the UNICODE macro. And here is the problem: The symbol ReplaceFile is simply a preprocessor macro which expands to ReplaceFileW or ReplaceFileA depending on the UNICODE macro. And as all macros it is expanded unconditionally.

The solution: Undefine the ReplaceFile macro after including the Windows system header file(s):

#include <windows.h>
#undef ReplaceFile
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 3
    One could also employ the ancient power of using a different name. – molbdnilo Aug 05 '16 at 08:16
  • Hmmm, I put `#undef ReplaceFile` under every windows.h include and it's made no difference. Let me tinker for a bit.... – mrg95 Aug 05 '16 at 08:21
  • Ah, so I had to put it in the cpp that `ReplaceFile()` was being called even though I already put it under the windows includes within my project. Looks like it was still hiding somewhere outside my project tree but still included through a library. Thanks! :) – mrg95 Aug 05 '16 at 08:24
  • 2
    @molbdnilo Not always possible, it could be from a third-party library. – Some programmer dude Aug 05 '16 at 08:27
  • **−1** For the low quality of the solution. As far as I recall this is only the second time I've ever downvoted on SO for other than technical error. But the advice here, to `#undef` each conflicting Windows API function name macro, is really ungood, even if it can work *in particular special cases*. – Cheers and hth. - Alf Aug 05 '16 at 08:28
  • @Cheersandhth.-Alf So is the better solution to use a different name? – mrg95 Aug 05 '16 at 08:30
  • @mc360pro: It depends. In the worst case that's one option. But it can also and hopefully be possible to employ the compiler firewall technique, to put that function and the code that uses it in a translation unit where `` isn't included. – Cheers and hth. - Alf Aug 05 '16 at 08:31
  • @JoachimPileborg Funny enough because it is, I just recently used the source from the lib instead of compiling as a lib. So yeah changing the name wouldn't have been an option. – mrg95 Aug 05 '16 at 08:32
  • 1
    @Cheersandhth.-Alf I agree with you, but like I said in my previous comment it's not always possibly to just rename symbols. Even if the source is available and one is building the library from source, renaming a symbol and have it pushed upstream could not be feasible which means one have to maintain a special branch of the library just because of that. – Some programmer dude Aug 05 '16 at 08:46
  • @Cheersandhth.-Alf Well think about it like this... nowhere in my project did I include windows.h where this function call could have seen it (I know this because I undeffed in every windows include I wrote.) So windows.h must have been included in a botan library or another library I didnt make. And since this function originally WAS from a 3rd party library, changing the name wouldnt have been an option either. This solution worked well when applied to my cpp. – mrg95 Aug 05 '16 at 08:50
  • 1
    @mc360pro: As I understand if from your comments the function is provided by a 3rd party library. That information should better be added to the question (make sure that it's clear that it has been added after this answer was posted). And in that case, just do as I suggested, namely, compiler firewall. Export a `replace_file` wrapper function from that translation unit. The magical inclusion of `` is probably via the precompiled header ``. But it can also be a forced include. Check your project settings. Fix as necessary. – Cheers and hth. - Alf Aug 05 '16 at 08:57