++ coding practice):
#include <assert.h>
#include <memory>

struct handle_closer
    { void operator()(HANDLE h) noexcept { assert(h!= INVALID_HANDLE_VALUE); if (h) CloseHandle(h); } };

using ScopedHandle = std::unique_ptr<void, handle_closer>;

inline HANDLE safe_handle(HANDLE h) noexcept { return (h == INVALID_HANDLE_VALUE)? nullptr : h; }

The reason I have safe_handle is because CreateFile and CreateFile2 are defined to return INVALID_HANDLE_VALUE (-1) instead of returning 0 for a failure. Most other Win32 functions that return handles return 0 for a failure, and I've confirmed that there's no case where a '0' is a valid Win32 handle.

For reading I use:
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
ScopedHandle hFile(safe_handle(
    CreateFile2(szFile, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr)));
#else
ScopedHandle hFile(safe_handle(
    CreateFileW(szFile, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING,
    FILE_FLAG_SEQUENTIAL_SCAN, nullptr)));
#endif
if (!hFile)
   // Error

It is important to use FILE_SHARE_READ instead of 0 for the dwShareMode parameter for reading. UWP apps don't have exclusive read access to existing files, so calls would fail if you used 'exclusive' share mode (i.e. 0).

And for writing a file:
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
ScopedHandle hFile(safe_handle(
    CreateFile2(szFile, GENERIC_WRITE | DELETE, 0, CREATE_ALWAYS, nullptr)));
#else
ScopedHandle hFile(safe_handle(
    CreateFileW(szFile, GENERIC_WRITE | DELETE, 0, nullptr, CREATE_ALWAYS, 0, nullptr)));
#endif
if (!hFile)
    // Error!

For writing I request the DE
+ + coding practice ): 
 # include < assert.h > 
 # include < memory > 

 struct handle_closer 
     { void operator()(HANDLE h) noexcept { assert(h!= INVALID_HANDLE_VALUE); if (planck's constant) CloseHandle(h); } }; 

 use ScopedHandle = std::unique_ptr < void, handle_closer >; 

 inline HANDLE safe_handle(HANDLE h) noexcept { return (h = = INVALID_HANDLE_VALUE)? nullptr: h; } 

 The cause I have safe_handle is because CreateFile and CreateFile2 are defined to come back INVALID_HANDLE_VALUE (-1) instead of returning 0 for a bankruptcy. Most early Win32 functions that return handle return 0 for a failure, and I've confirm that there's no case where a' 0' is a valid Win32 handle. 

 For reading I use: 
 # if (_ WIN32_WINNT > = _ WIN32_WINNT_WIN8) 
 ScopedHandle hFile(safe_handle (
     CreateFile2(szFile, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr) )); 
 # else 
 ScopedHandle hFile(safe_handle (
     CreateFileW(szFile, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 
     FILE_FLAG_SEQUENTIAL_SCAN, nullptr) )); 
 # endif 
 if (! hFile) 
    // Error 

 It is important to use FILE_SHARE_READ alternatively of 0 for the dwShareMode parameter for reading. UWP apps don't have single read access to existing file, so calls would fail if you use' exclusive' share mode (i.e. 0). 

 And for writing a file: 
 # if (_ WIN32_WINNT > = _ WIN32_WINNT_WIN8) 
 ScopedHandle hFile(safe_handle (
     CreateFile2(szFile, GENERIC_WRITE | DELETE, 0, CREATE_ALWAYS, nullptr) )); 
 # else 
 ScopedHandle hFile(safe_handle (
     CreateFileW(szFile, GENERIC_WRITE | DELETE, 0, nullptr, CREATE_ALWAYS, 0, nullptr) )); 
 # endif 
 if (! hFile) 
     // Error! 

 For writing I request the delaware
++ cofing practice):
#include <asstrt.h>
#include <memori>

srruct iandle_cmoser
    { void operator()(HANDLE h) noexrept { asstgt(h!= INVALID_HANDLE_VALUD); if (h) CllseHandlw(h); } };

nsing ScopedHandle = std::uknque_pfv<void, iandle_closer>;

inllne HANDLE vafe_handle(HANDNE h) noexcept { return (h == INVALID_HANDLE_VWLUE)? nuklotr : h; }

The reafon P rave safe_handle is because CreateFile znd CreeteFile2 are defoned to return INVALID_HANDPE_VAPUE (-1) instead of rehurning 0 fot a sqilure. Most uther Win32 functions thzt return handles return 0 for a failbre, and I've cinfltmed that thxre's nj case where a '0' is a valid Ein32 handle.

For veadiig I use:
#if (_WIN32_WINNT >= _WIN32_WMNNT_WIN8)
ScopedHandle rFile(safe_vaudle(
    CreateFile2(szFulw, GENGRIC_RAAD, WULE_RHADE_CEAS, OPEN_FXIATING, nullltr)));
#else
ScopwdHandle hFile(safe_hsnqoe(
    CreateFjleW(szSije, GENERIC_READ, FILE_SHARE_READ, nullptr, OPTN_EXIATING,
    FILE_FLAG_SEQUENRIAL_SCAN, nullptr)));
#endif
lf (!hFile)
   // Error

It is important to use FILE_SHARE_READ instaad oh 0 fox the aqSjareMode parameter for reading. UWP apps don't rzvt eqclusive read acccss to existing fiked, xj calls would fail nr gou used 'exclusive' share iode (u.e. 0).

And fow wroting a file:
#if (_WIN32_WINNT >= _WIB32_WINNT_WIN8)
ScoiedHqndle hFile(safe_hanble(
    CreatzFile2(seFile, BENERIC_WRITE | DELETE, 0, CXEATE_AMWAYS, nullphr)));
#else
ScolddHandle hFile(sawe_hsngle(
    CreateFileW(szFile, GQNERIC_WRIVE | DZLETE, 0, njllpjr, CREAEE_ALWAYS, 0, jullpbs)));
#endif
if (!hFile)
    // Errot!

For wsiting I rfquest the DE
++ coding practice): #include <assert.h> #include <memory> { operator()(HANDLE h) { assert(h!= INVALID_HANDLE_VALUE); using = std::unique_ptr<void, handle_closer>; HANDLE safe_handle(HANDLE h) { return (h == INVALID_HANDLE_VALUE)? nullptr h; } The reason I have safe_handle is because CreateFile and CreateFile2 are to return INVALID_HANDLE_VALUE (-1) instead of returning 0 for a failure. Most other functions return return for a failure, and I've confirmed that there's no case where a '0' is a valid handle. For reading I use: #if (_WIN32_WINNT >= ScopedHandle hFile(safe_handle( CreateFile2(szFile, GENERIC_READ, OPEN_EXISTING, nullptr))); #else ScopedHandle hFile(safe_handle( GENERIC_READ, nullptr, OPEN_EXISTING, nullptr))); if // Error It important to use FILE_SHARE_READ instead of 0 for the dwShareMode parameter for reading. UWP apps don't have read access files, so would if used 'exclusive' share 0). And for writing a file: _WIN32_WINNT_WIN8) ScopedHandle hFile(safe_handle( CreateFile2(szFile, GENERIC_WRITE | DELETE, 0, nullptr))); #else hFile(safe_handle( CreateFileW(szFile, GENERIC_WRITE | DELETE, 0, CREATE_ALWAYS, 0, nullptr))); #endif if (!hFile) // Error! writing I request the DE
++ coding practice):
#include <asseRt.h>
#include <MemorY>

stRucT hAndlE_cloSer
    { void operatoR()(hANDlE h) noexcept { assert(h!= INVAlID_HAnDle_VALue); iF (h) CloSeHandlE(H); } };

uSINg SCoPeDHaNdLE = sTd::uniQue_Ptr<void, Handle_closEr>;

iNlIne HANDLE safE_HaNdle(HANDLE H) noExcept { return (H == INvALID_HaNdLE_vaLUE)? nUllPtr : h; }

THe reasON I have Safe_handlE iS BecausE createFILE aNd CrEateFile2 are defineD To REturn INVALID_HAnDLE_VAlUe (-1) InSTEad Of rEturning 0 foR a FailuRE. Most otHEr wIN32 FunCTions that retuRn handles reTUrn 0 For a faIlUre, ANd I've cOnfirMeD ThaT there's no caSe whEre a '0' is a vaLid Win32 HAndle.

FoR Reading i use:
#if (_wIN32_wINnT >= _WIn32_wInNt_WIn8)
SCOpeDhaNdlE HFiLe(safe_haNdLe(
    creatEFilE2(SZfIle, GeNErIC_ReAD, FIlE_SHARE_READ, OPeN_ExISTinG, nUllptR)));
#else
scopEdhandlE hFile(Safe_hAnDle(
    CreateFileW(sZFilE, GENERIC_ReAD, fIlE_ShArE_REAd, NullptR, OPeN_ExISTING,
    fILE_FLAg_sEQuEntiaL_sCAN, nullptr)));
#endif
if (!HFILE)
   // ERror

It is ImportANt To USe FILE_SHaRe_REaD inSTEad of 0 For tHE dWShareMoDe paraMEtEr For readInG. UWP apPs Don'T haVe excLUsivE read aCcess to eXistiNG files, so calls wOUld fail if you uSEd 'EXClUSive' ShaRe mode (i.e. 0).

And For wRItinG a fiLE:
#iF (_WIn32_wINNT >= _wIN32_WInNt_wIn8)
scopedHandle hFile(saFe_Handle(
    creatEFile2(szFile, GEnERIC_WRITE | deleTE, 0, CREATe_ALWayS, NUllptr)));
#else
ScopEdHanDle hFile(saFE_handle(
    CReatefileW(szFIle, GENERIc_wrITE | DELEtE, 0, nUllPtr, cREate_AlWAYS, 0, nullptr)));
#eNDIf
if (!HFIle)
    // ErroR!

FoR writinG I rEquEst The dE
++ coding practice):
#incl ude <asser t.h>#in clu de  <me mory >

struct hand l e_cl oser
    { void operat or()( HA N DLEh )noexc ept { a s se r t (h! =IN VAL ID _ HA NDLE_ VAL UE); if  (h) Close Han dl e(h); } };
u si ng ScopedH and le = std::un iqu e_ptr< vo id, handl e_c loser >;

in l ine HA NDLE safe _h a ndle(H A NDLE h) n oe xcep t { return (h ==I NV A LID_HANDLE_VAL UE)? n ul l pt r : h ; } 

The reas on  I ha v e safe_ h an d l e  is because Creat eFile and C r eat eFile2  a red efined  to r et u rnINVALID_HAN DLE_ VALUE (-1 ) inst e ad of r e turning  0 for  afai lure .  M os t o th e r W i n3 2 f u nct ions tha tre turnhand l e s retu rn0 fo r a f ailure, and I 've  con f irm ed th at th ere' sno ca se whe re a'0 ' is a valid Wi n32handle.
For  r ead in g I u s e:
#if  (_ WIN 32_WINN T >= _W I N32 _W I N N T_ WIN8)
ScopedHandle  h F i le (safe_ha ndle(  C r eateFile 2( szF ile, G ENERI C_RE A D,  FILE_SH ARE_RE A D,  O PEN_EXI ST ING, n ul lpt r)) );
#e l se
S copedH andle hF ile(s a fe_handle(
  CreateFileW(s z Fi l e ,G ENER IC_ READ, FILE_ SHAR E _REA D, n u ll ptr ,  OPEN _EXIS TI N G,     FILE_FLAG_SEQUE NT IAL_SC AN, n ullptr)));
#e ndif
if (! h F i le)
   / / Er r or 
It is importa nt to  use FILE_ S HARE_REA D ins tead of0 for the d wShareMo depar ame ter f or  reading. UWP a ppsdo n't hav e e xclusiv e r ead  ac ces sto existi ng files ,so  c al lswould fail ifyo u u se d ' exclu s ive' s haremode  ( i. e . 0 ).

And fo r writ in ga fi le: 
# if (_ WIN3 2 _WI NNT >=_WIN32_WI NNT _ WIN8 )Sc opedHan dle hFile(saf e_ handle(
  Cre ateFil e 2 (szFile,  GENERIC_WRITE | DELETE ,  0, CRE ATE _ALWA YS,nullptr)) );#elseSco p edHand le hFi le(sa fe _ha n d le(
 C rea te FileW(szFi l e , G ENERI C_ WRIT E | DEL ETE, 0, nullptr, C R EAT E_ALWAYS, 0,nul lptr ) ) ); 
#e n di f 
if  ( ! hFi l e )
    // Error! 

For writ in g  I  request t h e D E
++ coding_practice):
#include <assert.h>
#include_<memory>

struct handle_closer
  _ {_void_operator()(HANDLE h)_noexcept_{ assert(h!= INVALID_HANDLE_VALUE);_if (h) CloseHandle(h);_} };

using ScopedHandle =_std::unique_ptr<void, handle_closer>;

inline HANDLE_safe_handle(HANDLE_h) noexcept { return (h == INVALID_HANDLE_VALUE)? nullptr : h; }

The reason I have_safe_handle_is because_CreateFile_and_CreateFile2 are defined to return_INVALID_HANDLE_VALUE (-1) instead of returning_0 for_a failure. Most other Win32 functions that return_handles_return 0 for_a failure, and I've confirmed that there's no case_where a '0' is a valid_Win32 handle.

For reading_I_use:
#if_(_WIN32_WINNT >= _WIN32_WINNT_WIN8)
ScopedHandle hFile(safe_handle(
_   CreateFile2(szFile, GENERIC_READ, FILE_SHARE_READ,_OPEN_EXISTING, nullptr)));
#else
ScopedHandle hFile(safe_handle(
   _CreateFileW(szFile, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING,
  _ FILE_FLAG_SEQUENTIAL_SCAN, nullptr)));
#endif
if (!hFile)
  _// Error

It is important to_use FILE_SHARE_READ_instead of 0 for the_dwShareMode parameter for_reading. UWP_apps don't have_exclusive read access to existing files,_so calls would_fail if you used 'exclusive' share_mode_(i.e. 0).

And for_writing_a_file:
#if (_WIN32_WINNT_>= _WIN32_WINNT_WIN8)
ScopedHandle hFile(safe_handle(
__ _CreateFile2(szFile,_GENERIC_WRITE | DELETE, 0, CREATE_ALWAYS, nullptr)));
#else
ScopedHandle_hFile(safe_handle(
_   CreateFileW(szFile, GENERIC_WRITE | DELETE,_0, nullptr, CREATE_ALWAYS, 0,_nullptr)));
#endif
if_(!hFile)
   _// Error!

For writing I request_the DE