uLib  User mode C/C++ extended API library for Win32 programmers.
_Internal.cpp
Go to the documentation of this file.
1 // Project: uLib
2 // Module: A handful of small internal subroutines used by various implementation modules.
3 // Note: These function id's have two leading underscores to mark them as global internals.
4 // Parameter validation is generally absent. It is assumed callers constrain their arguments.
5 // Author: Love Nystrom
6 
7 // ADDED May 2021 due to their need in various modules.
8 // TODO: During revisions !in the whole code base!, try to
9 // use these instead of local variants wherever applicable,
10 // to homogenize and lean out implementation.
11 
12 #include <uLib/UtilFunc.h>
13 #include <uLib/StrFunc.h>
14 #include <uLib/_Internal.h>
15 
16 bool __EnableProcPrivilege( IN CSTR Privilege, OUT LUID_AND_ATTRIBUTES* pPrv, OUT HANDLE* pToken )
17 //
18 // Enable a privilege for the current process and return the token handle and previous state.
19 //
20 {
21  ACCESS_MASK Access = TOKEN_ADJUST_PRIVILEGES| TOKEN_QUERY;
22  BOOL ok = OpenProcessToken( GetCurrentProcess(), Access, pToken );
23  if ( ok ) ok = EnablePrivilege( *pToken, Privilege, true, pPrv );
24  else *pToken = NULL;
25  return bool_cast( ok );
26 }
27 
28 bool __GetRoot( OUT TSTR Root, IN UINT ccRoot, IN CSTR PathName )
29 //
30 // Return the root component of a filepath.
31 //
32 {
33  TCHAR szRoot[ MAX_PATH ];
34  _tcscpy_s( szRoot, MAX_PATH, SkipPathPrefix( PathName ));
35  // PathStripToRoot handles both DOS paths and UNC paths (\\server\share\path).
36  // Alas, UNC paths w/o leading \\ and NT device paths are *not* handled.
37  BOOL ok = PathStripToRoot( szRoot );
38  if (ok) _tcsncpyz( Root, szRoot, ccRoot );
39  return bool_cast( ok );
40 }
41 
42 typedef TCHAR _ROOT_BUF[ 80 ]; // Should be sufficient for sane share names as well.
43 
45 //
46 // Get the file system ability flags of PathName's containing volume.
47 //
48 {
49  DWORD Flags; _ROOT_BUF szRoot = S_BLANK;
50  BOOL ok = __GetRoot( szRoot, dimof(szRoot), PathName );
51  if (ok) ok = GetVolumeInformation( szRoot, NULL,0, NULL, NULL, &Flags, NULL, 0 );
52  if (!ok) Flags = 0;
53  return Flags;
54 }
55 
57 //
58 // Get the S/N of PathName's containing volume.
59 //
60 {
61  DWORD VolSN; _ROOT_BUF szRoot = S_BLANK;
62  BOOL ok = __GetRoot( szRoot, dimof(szRoot), PathName );
63  if (ok) ok = GetVolumeInformation( szRoot, NULL,0, &VolSN, NULL, NULL, NULL, 0 );
64  if (!ok) VolSN = 0;
65  return VolSN;
66 }
67 
68 DWORD __GetHandleVolumeId( HANDLE hObj )
69 //
70 // Get the S/N of the volume of an open FS object.
71 //
72 {
73  BY_HANDLE_FILE_INFORMATION info; // Since Win2k
74  return GetFileInformationByHandle( hObj, &info ) ? info.dwVolumeSerialNumber : 0;
75 }
76 
78 //
79 // F.ex. when creating relative links, it's best to work in the link dir.
80 //
81 {
82  TCHAR Dir[ MAX_PATH ];
83  _tcscpy_s( Dir, MAX_PATH, PathName );
84  PathRemoveFileSpec( Dir );
85  return ChangeDirectory( Dir );
86 }
87 
88 // EOF
unsigned long DWORD
Definition: Common.h:414
bool __GetRoot(OUT TSTR Root, IN UINT ccRoot, IN CSTR PathName)
Definition: _Internal.cpp:28
#define CSTR
Definition: Common.h:329
DWORD __GetVolumeId(CSTR PathName)
Definition: _Internal.cpp:56
#define S_BLANK
Definition: Common.h:1235
DWORD __GetHandleVolumeId(HANDLE hObj)
Definition: _Internal.cpp:68
#define TSTR
Definition: Common.h:328
#define dimof(x)
Definition: Common.h:949
bool EnablePrivilege(HANDLE hToken, CSTR Privilege, bool Enable, OPTOUT PLUID_AND_ATTRIBUTES pSave)
bool __EnableProcPrivilege(IN CSTR Privilege, OUT LUID_AND_ATTRIBUTES *pPrv, OUT HANDLE *pToken)
Definition: _Internal.cpp:16
BOOL(WINAPI *SysImgList::Shell_GetImageLists)(HIMAGELIST *pimlLarge
TCHAR _ROOT_BUF[80]
Definition: _Internal.cpp:42
bool __forceinline bool_cast(BOOL B52)
Definition: Common.h:767
DWORD __GetVolumeFlags(CSTR PathName)
Definition: _Internal.cpp:44
#define SkipPathPrefix
Definition: UtilFunc.h:2375
CSTR __ChangeToDirOf(CSTR PathName)
Definition: _Internal.cpp:77
CSTR ChangeDirectory(CSTR Dir)
Definition: IoUtil.cpp:1026
#define _tcsncpyz
Definition: StrFunc.h:77