0

In my c++ class, I want use WIN API GetFileSizeEx() function. When I compile my code, the compilator said:

"error: 'GetFileSizeEx' was not declared in this scope".

However, others functions like CreateFile() or WriteFile() work perfectly.

In my class header, I declare this :

#if defined(WINVER) && WINVER==0x0602 /* windows 8 */
#define WINVER 0x0602
#define _WIN32_WINNT 0x0602
#elif defined(WINVER) && WINVER==0x0601 /* windows 7 */
#define WINVER 0x0601
#define _WIN32_WINNT 0x0601
#elif defined(WINVER) && WINVER==0x0600 /* windows vista and server 2008 */
#define WINVER 0x0600
#define _WIN32_WINNT 0x0600
#elif defined(WINVER) && WINVER==0x0502 /* server 2003 */
#define WINVER 0x0502
#define _WIN32_WINNT 0x0502
#elif defined(WINVER) && WINVER==0x0501 /* windows xp */
#define WINVER 0x0501
#define _WIN32_WINNT 0x0501
#endif

#include <windows.h>
#include <winbase.h>
#include <string>

In my .cpp class:

Test::Test()
{
hFile = CreateFile(TEXT("conf/configure_tool.txt"),
                           GENERIC_READ | GENERIC_WRITE,
                           0,
                           NULL,
                           OPEN_EXISTING,
                           0,
                           NULL);

if (hFile == INVALID_HANDLE_VALUE)
{
    canAcces = false;
}else
{
    if(GetFileSizeEx(hFile,&sized) != 0)
    {
        canAcces = true;
    }
}
}

Have you an idea to resolve my problem ?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
ExecAssa
  • 177
  • 11
  • 1
    That header seems to try to solve a chicken-and-egg problem. Probably never arriving at the egg. Get rid of it and just define _WIN32_WINNT to a suitable version. 0x502 is the minimum you should pick if you want to make the mistake to still support XP, 0x600 otherwise. – Hans Passant Jan 19 '15 at 16:00
  • Hmm ok, I think I begin to understand. If I use only : #define WINVER 0x0601 #define _WIN32_WINNT 0x0601 The compiler responsed : "error: "WINVER" redefined" Perhaps I have a problem with this Macro and this is why my previous code don't work? – ExecAssa Jan 19 '15 at 16:33
  • Is there a particular reason why you are defining `WINVER` and `_WIN32_WINNT` in code instead of in your project settings? – Remy Lebeau Jan 19 '15 at 18:16
  • Yeah, because "GetFileSizeEx()" function, don't work and i wanted to try to fix that with WINVER and _WIN32_WINNT. – ExecAssa Jan 19 '15 at 19:09
  • Why would defining in code change anything? – David Heffernan Jan 19 '15 at 19:52

1 Answers1

2

From the documentation:

Minimum supported client Windows XP [desktop apps only]

So you need to ensure that you have defined WINVER to be 0x0501 or greater.

If that doesn't solve the problem then the likely cause is that you are using a deficient SDK. Perhaps from an old version of a non-MS compiler. Make sure that you have an up-to-date SDK.

It must be said that the conditional code in the question that attempts to define _WIN32_WINNT is a little odd. Why don't you define _WIN32_WINNT at the same time as you define WINVER?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Oki, thx for your help. I try to use simple macro for testing : #define WINVER 0x0601 #define _WIN32_WINNT 0x0601 But the compiler responded : "error: "WINVER" redefined" An idea ? – ExecAssa Jan 19 '15 at 16:31
  • Well, find out where you defined `WINVER`, and see what value it has. Also, do you have any idea where your SDK came from? Do you know which compiler you use? – David Heffernan Jan 19 '15 at 17:07
  • Of course, I use MinGW tdm-gcc 4.8.1 compiler. For the SDK, I use MinGW win32 "windows.h" And I define the WINVER after the windows header include. – ExecAssa Jan 19 '15 at 17:20
  • So I guess the problem is here then: *If that doesn't solve the problem then the likely cause is that you are using a deficient SDK. Perhaps from an old version of a non-MS compiler. Make sure that you have an up-to-date SDK.* – David Heffernan Jan 19 '15 at 17:28
  • I have tried with MinGW tdm-gcc 4.9.2 and same problem. Maybe I will use an another solution than 'GetFileSizeEx' function. Thanks for you assistance. – ExecAssa Jan 19 '15 at 19:14
  • I think I answered the question that you asked – David Heffernan Jan 19 '15 at 19:52