GetFileAttributesEx.c 3.3 KB
Newer Older
S
Shengliang Guan 已提交
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/*****************************************************************************\
*                                                                             *
*   Filename	    GetFileAttributesEx.c				      *
*									      *
*   Description:    UTF-8 version of WIN32's GetFileAttributesEx()	      *
*                                                                             *
*   Notes:	    							      *
*                                                                             *
*   History:								      *
*    2016-09-12 JFL Created this file.					      *
*                                                                             *
*          Copyright 2016 Hewlett Packard Enterprise Development LP          *
* Licensed under the Apache 2.0 license - www.apache.org/licenses/LICENSE-2.0 *
\*****************************************************************************/

#ifdef _WIN32	/* Automatically defined when targeting a Win32 application */

#include <windows.h>	/* Also includes MsvcLibX' WIN32 UTF-8 extensions */
#include <limits.h>

/*---------------------------------------------------------------------------*\
*                                                                             *
|   Function:	    GetFileAttributesExU				      |
|                                                                             |
|   Description:    Get file information for a UTF-8 pathname.		      |
|                                                                             |
|   Parameters:     See WIN32's GetFileAttributesEx()			      |
|                                                                             |
|   Return value:   1 = done, or 0 if error				      |
|                                                                             |
|   Notes:	    Sets the file length, dates, attributes, etc.	      |
|                                                                             |
|   History:								      |
|    2016-09-12 JFL Created this routine.				      |
*                                                                             *
\*---------------------------------------------------------------------------*/

BOOL WINAPI GetFileAttributesExU(
  LPCTSTR                lpFileName,
  GET_FILEEX_INFO_LEVELS fInfoLevelId,
  LPVOID                 lpFileInformation
) {
  WIN32_FILE_ATTRIBUTE_DATA *lpFileData = lpFileInformation;
  WCHAR *pwszName;
  BOOL bDone;
  int n;

  /* Allocate a buffer large enough for any Unicode pathname */
  pwszName = (void *)LocalAlloc(LMEM_FIXED, UNICODE_PATH_MAX * sizeof(WCHAR));
  if (!pwszName) return 0;

  /* Convert the pathname to a unicode string, with the proper extension prefixes if it's longer than 260 bytes */
  n = MultiByteToWidePath(CP_UTF8,		/* CodePage, (CP_ACP, CP_OEMCP, CP_UTF8, ...) */
    			  lpFileName,		/* lpMultiByteStr, */
			  pwszName,		/* lpWideCharStr, */
			  UNICODE_PATH_MAX	/* cchWideChar, */
			  );
  if (!n) {
    LocalFree((HLOCAL)pwszName);
    return 0;
  }

  /* Get the file information, using the Unicode version of the function */
  bDone = GetFileAttributesExW(pwszName, fInfoLevelId, lpFileData);

  /* Cleanup and return */
  LocalFree((HLOCAL)pwszName);
  return bDone;
}

#endif /* defined(_WIN32) */