WIN API GetFileSizeEx() function, was not declared in this scope - winapi

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 ?

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?

Related

Can't use TerminateProcess with msys2 on Win7

I'm making in gtk3 app that behaves like a launcher. I'm developing in linux and windows7 with msys2 and msvc2010. Everyhing is fine except that I can't use TerminateProcess because I get this warning:
implicit declaration of function 'TerminateProcess'
This is part of the code:
#if defined(G_OS_WIN32)
#if defined(__GNUC__)
#include <w32api.h>
#define WINVER WindowsXP
#define _WIN32_WINNT WindowsXP
#define _WIN32_WINDOWS WindowsXP
#define _WIN32_IE IE7
#endif
#include <processthreadsapi.h>
#endif
#include <gtk/gtk.h>
// ...
void kill_process (GPid pid)
{
#if defined(G_OS_WIN32)
TerminateProcess (pid, 0);
#else
kill (pid, SIGTERM);
#endif
g_spawn_close_pid (pid);
}
The above function kill_process works as expected in linux and in windows (msvc 2010), but it doesn't with msys2 mingw64... any ideas?

_far _near _cdecl _pascal in migration from VC6 to VS2019

I'm migrating a project from VC6 to VS2019. As far as I know, _far _near are for the segmented memory model access, and _cdel _ pascal are for calling convention. All of them seem only existed in VC6 and older versions. For migration, is it safe to remove them like the #else block? Thanks!
Code:
/* 関数タイプ コンパイラ依存 */
#ifdef MSDOS
#ifdef _MSC_VER /* MS-C Ver.6.0以上 */
#define FAR _far
#define NEAR _near
#define CDECL _cdecl
#define LeafFunc _fastcall _near /* nearコール */
#define NearFunc _pascal _near /* nearコール */
#define FarLeafFunc _fastcall _far /* farコール */
#define FarFunc _pascal _far /* farコール */
#define register /* register宣言は無視 */
#else /* その他 */
#define FAR
#define NEAR
#define CDECL
#define LeafFunc
#define NearFunc
#define FarLeafFunc
#define FarFunc
#endif
NareFunc is used like the following code:
void NearFunc mmInit PT0() {
//...
}
EDIT: Thanks to 9dan. I've corrected _far and _near in the post.

Cross-platform definition of _byteswap_uint64 and _byteswap_ulong

Visual Studio defines _byteswap_uint64 and _byteswap_ulong in stdlib.h.
Am I right to assume, that this is not standard and won't compile on Linux or Darwin?
Is there a way to define these includes in a cross-platform way?
Google's CityHash source code uses this code:
https://github.com/google/cityhash/blob/8af9b8c2b889d80c22d6bc26ba0df1afb79a30db/src/city.cc#L50
#ifdef _MSC_VER
#include <stdlib.h>
#define bswap_32(x) _byteswap_ulong(x)
#define bswap_64(x) _byteswap_uint64(x)
#elif defined(__APPLE__)
// Mac OS X / Darwin features
#include <libkern/OSByteOrder.h>
#define bswap_32(x) OSSwapInt32(x)
#define bswap_64(x) OSSwapInt64(x)
#elif defined(__sun) || defined(sun)
#include <sys/byteorder.h>
#define bswap_32(x) BSWAP_32(x)
#define bswap_64(x) BSWAP_64(x)
#elif defined(__FreeBSD__)
#include <sys/endian.h>
#define bswap_32(x) bswap32(x)
#define bswap_64(x) bswap64(x)
#elif defined(__OpenBSD__)
#include <sys/types.h>
#define bswap_32(x) swap32(x)
#define bswap_64(x) swap64(x)
#elif defined(__NetBSD__)
#include <sys/types.h>
#include <machine/bswap.h>
#if defined(__BSWAP_RENAME) && !defined(__bswap_32)
#define bswap_32(x) bswap32(x)
#define bswap_64(x) bswap64(x)
#endif
#else
#include <byteswap.h>
#endif
I'm not aware of a cross-platform and efficient way of doing that. If you use GCC you can use the builtin byteswap like:
uint32_t __builtin_bswap32 (uint32_t x)
Those are fast but certainly not portable... unless you wrap the various versions under the appropriate ifdefs
Cheers
Francesco

#define _UNICODE not working with MinGW + CodeBlocks

usually i use visual studio, but i switched to mingw, i like to make my apps easily changeable from unicode and multi byte, in my mingw project i have my defines and includes like this:
#define WIN32_LEAN_AND_MEAN
#define WINVER 0x0700
#define _UNICODE
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#define WND_MAIN_CLASS _T("MainWindowFrame")
then i register and create my window e.g.
WNDCLASSEX wc;
...
wc.lpszClassName = WND_MAIN_CLASS;
RegisterClassEx(&wc);
hwnd = CreateWindowEx(0, WND_MAIN_CLASS, _T("Main Window"),
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInst, NULL);
but when i go to compile i get errors that it cannot convert wchar_t to CHAR* on the WNDCLASSEX lpszClassName and the CreateWindowEx on the Class name and window title.
if i right click and go to declaration of createwindowex and WNDCLASSEX, it comes up with these from winuser.h:
typedef WNDCLASSEXW WNDCLASSEX,*LPWNDCLASSEX,*PWNDCLASSEX;
#define CreateWindowEx CreateWindowExW
if i comment out the define _UNICODE it compiles and works with no problems
When compiling unicode apps you should probably define both UNICODE and _UNICODE. The windows headers use UNICODE and the MS C runtime uses _UNICODE

Trying to use SHCreateShellItem, having #include issues

There is a function in called SHCreateShellItem which is declared in <shlobj.h>, but it has been #ifdef'd out based on whether or not _WIN32_IE is greater than or equal to 0x601 (if it is, then the declaration is present). However, even when I define _WIN32_IE to 0x601 before I include <shlobj.h>, MSVC++ still complains that SHCreateShellItem is undeclared.
For example, I cannot get the following to compile:
#define _WIN32_IE 0x601
#include <shlobj.h>
int someFunction (LPITEMIDLIST parent, LPITEMIDLIST child)
{
HRESULT result;
IShellItem *shellObj;
result = SHCreateShellItem (parent, NULL, child, &shellObj);
if (SUCCEEDED(result))
{
// do stuff
}
return SUCCEEDED(result);
}
Do I need to define _WIN32_IE in a different way?
_WIN32_IE is usually defined in your stdafx.h file. You must change it there.
Don't define _WIN32_IE directly, do it implicitly with _WIN32_WINNT (which sets the target platform for the SDK includes)
#define _WIN32_WINNT 0x0600
#include <windows.h>
#include <shlobj.h>

Resources